6. Repetition: While And For Loops

2y ago
19 Views
2 Downloads
571.60 KB
27 Pages
Last View : 22d ago
Last Download : 3m ago
Upload by : Gannon Casey
Transcription

Computer Science ICS 1356. Repetition: While and For LoopsRené DoursatDepartment of Computer Science & EngineeringUniversity of Nevada, RenoFall 2005

Computer Science ICS 1350. Course Presentation1. Introduction to Programming2. Functions I: Passing by Value3. File Input/Output4. Predefined Functions5. If and Switch Controls6. While and For Loops7. Functions II: Passing by Reference8. 1-D and 2-D Arrays10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops2

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structuresb. While Loopsc. Do/While Loopsd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops3

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structures9 Reminder: three types of control structures9 Why is repetition needed?9 While, do/while and for loopsb. While Loopsc. Do/While Loopsd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops4

6.a Repetition StructuresReminder: three types of control structures¾ Sequence, selection and repetition structures10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops5

6.a Repetition StructuresReminder: three types of control structures¾ Structure theorem9 it is possible to write any computer program by using only threebasic control structures that are easily represented inpseudocode: sequence structures selection structuresintroduce branching (“jumps”)in the sequential logic repetition structures¾ Sequence structures9 straightforward execution of one processing step after another9 sequence of pseudocode statements: do this, do that, thenthis, then that, etc.10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops6

6.a Repetition StructuresReminder: three types of control structures¾ Selection structures9 condition and choice between two actions, depending onwhether the condition is true or false9 represented by the pseudocode keywords IF, THEN, ELSE,and ENDIF¾ Repetition structures9 block of statements to be executed repeatedly, as long as acondition is true9 represented by the pseudocode keywords WHILE andENDWHILE (or DOWHILE and ENDDO)10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops7

6.a Repetition StructuresWhy is repetition needed?¾ A computer can repeat a group of actions repetition structures9 examples: calculate 100 student grades pour water in the saucepan until it is full cook the pasta until it is “al dente”9 pseudocode example:WHILE water level pan heightAdd 1 tablespoon to water volumewater level water volume / pan surfaceENDWHILE10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops8

6.a Repetition StructuresWhy is repetition needed?¾ Repetition allows to efficiently use variables9 for example, repetition allows to input, add, and averagemultiple numbers using a limited number of variables9 adding four numbers without a loop (the old-fashioned way): declare a variable for each number, input all the numbersand add all the variables together9 adding four numbers with a loop (the high-tech way): create a loop that iteratively reads a number and adds itto a variable holding the sum of the numbers10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops9

6.a Repetition StructuresWhy is repetition needed?void main(){// declare variablesint num1, num2, num3, num4, sum;// prompt usercout "Entercin num1;cout "Entercin num2;cout "Entercin num3;cout "Entercin num4;void main(){// declare variablesint num, sum 0;for 4 numbersnumber: ";// prompt and increment sum 4 timescout "Enter number: ";cin num; sum num;number: ";cout "Enter number: ";cin num; sum num;number: ";number: ";cout "Enter number: ";cin num; sum num;// calculate sumsum num1 num2 num3 num4;cout "Enter number: ";cin num; sum num;// display resultcout "The sum is: ";cout sum endl;// display resultcout "The sum is: ";cout sum endl;}}Adding four numbers by repeating four times the same task10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops10

6.a Repetition StructuresWhy is repetition needed?void main(){// declare variablesint num, sum 0;void main(){// declare variablesint num, sum 0;int counter 0;// prompt and increment sum 4 timescout "Enter number: ";cin num; sum num;// prompt and increment sum 4 timeswhile (counter 4) {cout "Enter number: ";cin num; sum num;cout "Enter number: ";cin num; sum num;counter ;cout "Enter number: ";cin num; sum num;}// display resultcout "The sum is: ";cout sum endl;cout "Enter number: ";cin num; sum num;}// display resultcout "The sum is: ";cout sum endl;}Repeating four times the same task with a loop10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops11

6.a Repetition StructuresWhy is repetition needed?void main(){// declare variablesint num, sum 0;int counter 0;void main(){// declare variablesint num, sum 0;int counter 0;int num count;// prompt and increment sum 4 timeswhile (counter 4) {cout "Enter number: ";cin num; sum num;// prompt user for number of inputscin num count;// increment sum num count timeswhile (counter num count) {cout "Enter number: ";cin num; sum num;counter ;}// display resultcout "The sum is: ";cout sum endl;counter ;}}// display resultcout "The sum is: ";cout sum endl;}Repeating the same task with a loop a variable number of times10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops12

6.a Repetition StructuresWhy is repetition needed?¾ Benefits of repetition9 you can vary the number of inputs: instead of just four, it canbe any user-specified variable9 this number can be very large: you can repeat 10 times, 100times or 10,000 times the same task without changing the code example: calculating students’ grades for the whole classusing the same formula9 you can also repeat an action while a specific condition is valid(or until its contrary has been met) example: reading lines from a file until the end of the file example: asking the user for their choice, until it is a validselection10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops13

6.a Repetition StructuresWhile, do/while and for loops¾ Three types of loops9 while loops (a) evaluate the logical expression first; (b) if true, executethe body, otherwise exit; (c) go to (a)9 do while loops(a) execute the body first; (b) then, evaluate the logicalexpression; (c) if true, go to (a)9 for loops for loops are a specialized form of while loopsthat simplifies the writing of counter-controlled loops 10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops14

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structures9 Reminder: three types of control structures9 Why is repetition needed?9 While, do/while and for loopsb. While Loopsc. Do/While Loopsd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops15

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structuresb. While Loops9999General form of a while loopCase 1: counter-controlled while loopsCase 2: sentinel-controlled while loopsCase 3: flag-controlled while loopsc. Do/While Loopsd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops16

6.b While LoopsGeneral form of a while loop¾ General flowchart of a while loop9 (a) evaluate the logical expression first; (b) if true, execute thebody, otherwise exit; (c) go to (a)10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops17

6.b While LoopsGeneral form of a while loop¾ Syntax of a while loopwhile (expression)statementwhile (!end of file)read line();9 while is a reserved keyword9 expression is a logical expression (or variable, or function) expression provides an entry condition into the loop9 statement follows expression and can be any C statement statement executes if expression initially evaluates to true then expression is reevaluated and statement executesagain multiple times as long as the expression is still true when expression becomes false, statement is bypassedand the program goes to the next statement directly10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops18

6.b While LoopsGeneral form of a while loop¾ While loop with compound statement9 the body of a while loop can contain multiple C statements9 a block of statements is called a “compound statement” and mustbe surrounded with curly braces { }while (expression) {statement1statement2statement3}10/10-12/2005while (!end of file) {read line();display line();.}CS 135 - Computer Science I - 6. Repetition: While and For Loops19

6.b While LoopsCase 1: counter-controlled while loops¾ A counter-controlled loop repeats statements a fixednumber of times9 if you know exactly how many pieces of data need to beprocessed, the while loop can be a counter-controlled loopint n 17;// set number of timesint counter 0;while (counter n) {.counter ;.}// init loop variable// test loop variable10/10-12/2005// update loop variableCS 135 - Computer Science I - 6. Repetition: While and For Loops20

6.b While LoopsCase 2: sentinel-controlled while loops¾ A sentinel-controlled loop repeats statements untilsome value is reached9 you do not know how many pieces of data need to beprocessed, but you will know the last one when you see itcin var;// init loop variablewhile (var ! sentinel) { // test loop variable.cin var;// update loop variable.}10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops21

6.b While LoopsCase 3: flag-controlled while loops¾ A flag-controlled loop repeats statements until aboolean flag becomes false9 here too, you do not know how many pieces of data need to beprocessed; this time, you carry over the value of a complexexpression into a flag that will stop the loopfound false;// init loop variablewhile (!found) {// test loop variable.if (expression)found true; // update loop variable.}10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops22

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structuresb. While Loops9999General form of a while loopCase 1: counter-controlled while loopsCase 2: sentinel-controlled while loopsCase 3: flag-controlled while loopsc. Do/While Loopsd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops23

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structuresb. While Loopsc. Do/While Loops9 General form of a do/while loopd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops24

6.c Do/While LoopsGeneral form of a while loop¾ General flowchart of a do/while loop9 (a) execute the body first; (b) then, evaluate the logicalexpression; (c) if true, go to (a)10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops25

6.c Do/While LoopsGeneral form of a while loop¾ General syntax of a do/while loopdo {statement1statement2statement3} while (express.);do {cin ans;switch (ans) {. }} while (ans ! 'q');9 do is a reserved keyword9 don’t forget the semicolon ; after the while expression!9 the statements execute first, then the expression is evaluated9 if expression evaluates to true, statements execute again, etc.9 a do/while loop always iterates at least once10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops26

Computer Science ICS 1356. Repetition: While and For Loopsa. Repetition Structuresb. While Loopsc. Do/While Loops9 General form of a do/while loopd. For Loops10/10-12/2005CS 135 - Computer Science I - 6. Repetition: While and For Loops27

Case 1: counter-controlled while loops ¾A counter-controlled loop repeats statements a fixed number of times 9if you know exactly how many pieces of data need to be processed, the while loop can be a counter-controlled loop int n 17; // set number of times int counter 0; // init loop variable while (counter n) { // test loop variable.

Related Documents:

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

Sentinel-controlled repetition –Indefinite repetition. –Used when number of repetitions not known. –Sentinel value indicates end of data _. Programming and Data Structure 34 Counter-controlled Repetition Counter-controlled repetition requires: –name of a control variable (or loop c

10 tips och tricks för att lyckas med ert sap-projekt 20 SAPSANYTT 2/2015 De flesta projektledare känner säkert till Cobb’s paradox. Martin Cobb verkade som CIO för sekretariatet för Treasury Board of Canada 1995 då han ställde frågan

service i Norge och Finland drivs inom ramen för ett enskilt företag (NRK. 1 och Yleisradio), fin ns det i Sverige tre: Ett för tv (Sveriges Television , SVT ), ett för radio (Sveriges Radio , SR ) och ett för utbildnings program (Sveriges Utbildningsradio, UR, vilket till följd av sin begränsade storlek inte återfinns bland de 25 största

Hotell För hotell anges de tre klasserna A/B, C och D. Det betyder att den "normala" standarden C är acceptabel men att motiven för en högre standard är starka. Ljudklass C motsvarar de tidigare normkraven för hotell, ljudklass A/B motsvarar kraven för moderna hotell med hög standard och ljudklass D kan användas vid

LÄS NOGGRANT FÖLJANDE VILLKOR FÖR APPLE DEVELOPER PROGRAM LICENCE . Apple Developer Program License Agreement Syfte Du vill använda Apple-mjukvara (enligt definitionen nedan) för att utveckla en eller flera Applikationer (enligt definitionen nedan) för Apple-märkta produkter. . Applikationer som utvecklas för iOS-produkter, Apple .

This presentation and SAP's strategy and possible future developments are subject to change and may be changed by SAP at any time for any reason without notice. This document is 7 provided without a warranty of any kind, either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a .

och krav. Maskinerna skriver ut upp till fyra tum breda etiketter med direkt termoteknik och termotransferteknik och är lämpliga för en lång rad användningsområden på vertikala marknader. TD-seriens professionella etikettskrivare för . skrivbordet. Brothers nya avancerade 4-tums etikettskrivare för skrivbordet är effektiva och enkla att