Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 IT CS 200: R EPEATATION Lect. Napat Amphaiphan

2 T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2

3 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

4 While LoopWhile Loop 1 For LoopFor Loop 2 Do While LoopDo While Loop 3 Today’s Overview 4

5 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

6 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

7 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++)

8 The while Statement The general form of the while statement is while (expression) statement; Or while (expression) { statement1; statement2; }

9 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

10 The while Statement (cont.)

11 Output is: 1 2 3 4 5 6 7 8 9 10

12 The while Statement (cont.) Output is: 10 9 8 7 6 5 4 3 2 1

13 The while Statement (cont.) What is the Output ? Write a Flow Chart

14 Examples: Receive a number ‘n’ times

15 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

16 Examples: Finding Average Calculating an average

17 scanf( ) & End of File (EOF) End of File (EOF) mark the end of the program

18 scanf( ) & End of File (EOF)

19 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 */

20 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; }

21 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);

22 The for Statement  Output is:

23 The for Statement: Finding Average

24 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

25 The do-while Statement

26

27 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 1999. 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.

28 Nested Loops

29 What is the Output ?

30 Loop Programming Techniques Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations

31 Technique 1: Selection within a Loop

32 Technique 2: Input Data Validation

33 Technique 3: Interactive Loop Control

34 Technique 4: Evaluating Equations

35 Lab Assignment In lab sheet 35


Download ppt "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."

Similar presentations


Ads by Google