IT CS 200: R EPEATATION Lect. Napat Amphaiphan
T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2
Overview (Midterm) Midterm Examination 30 % Data Type, Declaration, Display Interactive Input SelectionRepetition Analyze Problem Solving Problem Solving int float double char printf() int float double char printf() scanf() if…else switch…case if…else switch…case while statement for statement do-while statement while statement for statement do-while statement
While LoopWhile Loop 1 For LoopFor Loop 2 Do While LoopDo While Loop 3 Today’s Overview 4
Introduction Flow of control refers to the order in which a program’s statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: – Normal flow of control for all programs is sequential – Selection is used to select which statements are performed next based on a condition – Repetition is used to repeat a set of statements – Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function
Introduction A section of code that is repeated is called a loop, because after the last statement in the code is executed, the program loops back to the first statement and starts another repetition through the code Each repetition is also called an iteration or pass through the loop
Basic Loop Structures – Repetition statement while statement for statement do-while statement – Loop elements Condition (relational expression: x<10, x<=Total) Initialization (i = 0; ) Alteration (count++, i++)
The while Statement The general form of the while statement is while (expression) statement; Or while (expression) { statement1; statement2; }
The while Statement (cont.) Process used by the computer in evaluating a while statement is 1. test the expression 2. if the expression has a nonzero (true) value a. execute the statement following the parentheses b. go back to step 1 (loop) else exit the while statement
The while Statement (cont.)
Output is:
The while Statement (cont.) Output is:
The while Statement (cont.) What is the Output ? Write a Flow Chart
Examples: Receive a number ‘n’ times
Examples: Accumulate a Sum Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement
Examples: Finding Average Calculating an average
scanf( ) & End of File (EOF) End of File (EOF) mark the end of the program
scanf( ) & End of File (EOF)
break Statements A break forces an immediate exit from while, switch, for, and do-while statements Example of while loop is immediately terminated if a number greater than 76 is entered count = 0; while (count <= 10) { printf(“Enter a number: “); scanf(“%f”, &num); if (num > 76) { printf(“You lose!”); break; /* break out of the loop */ } else printf(“Keep on trucking!”); } /* break jumps to here */
continue Statements The continue applies to loops only; when a continue statement is encountered in a loop, the next iteration of the loop begins immediately while (count < 5) { printf("Enter a grade: \n"); scanf("%d", &grade); if(grade 100) { continue; } total = total + grade; count = count + 1; }
The for Statement General form for (initializing list; expression; altering list) statement; while statement count = 1; while (count <= 10) { printf(“%d”, count); ++count; } for statement for (count = 1; count <= 10; ++count) printf(“%d”, count);
The for Statement Output is:
The for Statement: Finding Average
The do-while Statement The general form of the do statement is do statement; while (expression); do-while is a posttest loop One type of application is ideally suited for a posttest loop: – Input data validation application
The do-while Statement
The do-while Statement Validity Checks do { printf("\nEnter an ID number: "); scanf("%f", &idNum); } while (idNum 1999); Example An operator is required to enter a valid customer identification number 1000 through A number outside this range is to be rejected, and new request for a valid number made. Code provides the necessary data filter to verify the entry of a valid identification number.
Nested Loops
What is the Output ?
Loop Programming Techniques Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations
Technique 1: Selection within a Loop
Technique 2: Input Data Validation
Technique 3: Interactive Loop Control
Technique 4: Evaluating Equations
Lab Assignment In lab sheet 35