Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.

Similar presentations


Presentation on theme: "Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops."— Presentation transcript:

1 Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops

2 Sesi 0607EKT120/4 Computer Programming Outline Introduction While loops  Three types of while loops Counter-controlled (definite repetition) Sentinel-controlled (indefinite repetition) Flag-controlled (uses Boolean variable) Do-while loops For loops Nested loops Math.h

3 Sesi 0607EKT120/4 Computer Programming Review  Problem that can be solve with computers generally consist of three: Computational – problem with mathematical processing Logical – problem involving with relational or logical processing. This is kind of processing involve in decision making. Repetitive – problem involving repeating a set of mathematical or logical instructions.

4 Sesi 0607EKT120/4 Computer Programming Suppose we want to add five numbers and find the average. scanf(“%d %d %d %d %d”, &num1, &num2, &num3, &num4, &num5); Why Need Loops? sum = num1 + num2 + num3 + num4 + num5; average = sum / 5; But what if…………………………. 100 numbers, 1000 numbers?

5 Sesi 0607EKT120/4 Computer Programming Repetition (Loop) We used to control the flow of a program. Loops are basically used to repeat a segment instructions. (Refer diagram Instruction 1 & 2 being repeated until the condition is false) Condition usually is a counter, flag status or a sentinel value.  Counter is a definite number of how many loops we need to execute the segment of code  Flag status is a logic operator status  Sentinel usually use if the number of loops to be execute is indefinite.

6 Sesi 0607EKT120/4 Computer Programming Repetition (Loop) In C language there are three types of loops that we can use.  while loop  do-while loop, this is equivalent with repeat … until loop in Pascal and other programming language.  for loop

7 Sesi 0607EKT120/4 Computer Programming The while loop structure The general syntax of while loop statement is: while (condition) statement;  If we have more than one statements, While (condition) { statement1; statement2; }

8 Sesi 0607EKT120/4 Computer Programming The while loop – continue To avoid an infinite loop, make sure that the loop’s body contains statement(s) that assure the exit condition i.e. the expression in the while statement will eventually be false. While loop repeated until condition becomes false

9 Sesi 0607EKT120/4 Computer Programming The while loop There are basically three types of while loops:  Counter-controlled while loop  Sentinel-controlled while loop  Flag-controlled while loop

10 Sesi 0607EKT120/4 Computer Programming Counter-Controlled while Loops Definite repetition: know how many times loop will execute. if we know how many loops to execute this is a counter control loop. Example Let consider this problem – To display number 1 to 10 on screen Refer flowchart for the solution

11 Sesi 0607EKT120/4 Computer Programming Counter control while loop Below is the example of C code for the solution Counter control variable used to count repetitions Example: int counter = 1; // declare and initialize while ( counter <= 10 ) // condition { printf( "%d\n", counter ); ++counter; // increase counter } Requirement: 1.Declare and initialize control variable value (or loop counter) 2. A condition that tests for the final value of the control variable (i.e., whether looping should continue) 3. Update control variable (incr/decr)

12 Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops Indefinite repetition – we not sure how many loops will be executed. Used when number of repetitions not known and the termination of the loop needs to be input value repeatedly for each iteration Sentinel value indicates "end of data“

13 Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops Example: int main () { int number, count, sum; printf(“To stop enter -999. Enter positive numbers : " ); scanf(“%d”, &number); while (number != -999) { sum = sum + number; // find sum of numbers entered count++; // count how many numbers entered printf(“To stop enter -999. Enter positive numbers : " ); scanf(“%d”, &number); } printf(“\nThe sum of %d numbers is %d“, count, sum); } Requirement: 1.Read control variable value before enter loop 2.A condition that tests control variable’s validity (i.e., whether looping should continue) 3.Read again control variable before end of loop Sentinel value

14 Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops-cont Another example: int main (){ char choice=‘y’; float grade_pt; while ( choice == ‘y’) {printf(“\nEnter grade point:”); scanf(“%f”, &grade_pt); if(grade_pt > 2.0) printf(“\nPass); printf(“Continue?y-yes n-no :”); scanf(“%c”, &choice); } read control variable test condition

15 Sesi 0607EKT120/4 Computer Programming Flag-Controlled while Loops Uses a boolean variable to control the loop. Loop exit when expression is evaluated to false. Requirement: 1.Set control variable to false before loop 2. A condition that tests control variable. If expression evaluated to true, loop continue 3. A decision structure to test value validity 4. Set control variable to true to indicate found bool found = false; while (!found) { printf(“Enter number between 1 and 200:”); scanf(%d”, &guess); if ((guess>= 88) &&(guess <=128)) {found = true; printf(“\nBINGO!”); } Set to false test condition Decision structure Set to true

16 Sesi 0607EKT120/4 Computer Programming The do - while Repetition Structure Similar to the while structure Condition for repetition tested after the body of the loop is performed All actions are performed at least once Expression can be written as count- controlled or sentinel-controlled Format: do{ statement; } while ( condition );

17 Sesi 0607EKT120/4 Computer Programming The do - while Repetition Structure Counter control example : prints the integers from 1 to 10 int counter = 1; do { printf( "%d ", counter ); } while (++counter <= 10);  Sentinel control example: do{ printf(“\nEnter grade point:”); scanf(“%f”, &grade_pt); if(grade_pt > 2.0) printf(“\nPass); printf(“Continue?y-yes n-no :”); scanf(“%c”, &choice); }while ( choice == ‘y’) count-controlled sentinel-controlled

18 Sesi 0607EKT120/4 Computer Programming The do-while Repetition Structure To avoid an infinite loop, make sure that the loop body contains a statement that ultimately makes the expression false and assures that it exits Another example: int i = 0; do { printf(“%d”,i); i = i + 5; } while (i <= 20); The output is: 0 5 10 15 20

19 Sesi 0607EKT120/4 Computer Programming The do-while Looping Structure Example Consider the following two loops i = 11; while (i<=10) { printf(“%d”,i); i = i + 5; } ---------------------------- i = 11; do {  printf(“%d”,i);  i = i + 5;  } while (i<=10); Is it same???? (a) (b)

20 Sesi 0607EKT120/4 Computer Programming The do-while Looping Structure - Example In (a), the while loop, produces nothing In (b) the do...while loop, outputs the number 11 Comment : The do while loop will permit at least once to enter the loop. Even though the condition is false. This is because the test condition is at the end of the loop

21 Sesi 0607EKT120/4 Computer Programming The for loop structure The for loop format  for ( counter init; end condition; increment statement) We use for loops structure when we already know how many times to repeat Example: for (counter = 1; counter <= 10;counter++ ) printf( "%d\n", counter );

22 Sesi 0607EKT120/4 Computer Programming The for Repetition Structure Initialization and increment  Can be comma-separated lists  Example 1: for (i=0; i<100; i++) printf(“%d\n”,i);  Example 2: for (int i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i );

23 Sesi 0607EKT120/4 Computer Programming Flow of a for statement For loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest ) { statement; increment statement; }

24 Sesi 0607EKT120/4 Computer Programming Nested loop When loop within a loop we called it nested loop (same as nested if(if within if statement)) Inner loop will be performed first E.g. for(i=1;i<4;i++){ for(j=1;j<5;j++) printf(“%d ”, j); printf(“\n”);} Usually we use in matrix operations. Output 1 2 3 4

25 Sesi 0607EKT120/4 Computer Programming Nested loop Another example:  Program find and print avg of three test scores, then asks whether want to continue do{ for(count=0; count <3;count++) {printf(“\nEnter test marks:”); scanf(“%d”, marks); total=total+marks; } avg=total/count; printf(“\nAverage:%5.2f”, avg); printf(“\nContinue?”); scanf(“%c”, &choice); }while(choice == ‘y’);

26 Sesi 0607EKT120/4 Computer Programming End Week 5 - Loops


Download ppt "Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops."

Similar presentations


Ads by Google