1 / 12

Repetition

Repetition. Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured programming concepts. An action or a series of actions.

kay
Download Presentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured programming concepts. An action or a series of actions The above flowchart would be considered an infinite loop because its repeats the action over and over again and never stops. To make a loop end we must have a condition that controls the loop. In other words, the loop must be designed so that before or after each iteration, it checks to see if it is done. false Condition Body (action) true true Body (action) Condition false Pretest loop Posttest loop

  2. Repetition In a pretest loop, the body can be done zero or more times. In a posttest loop, the body must be done at least once. Before a loop can start, a variable must be initialized. The value of a variable or expression is tested. Then something must happen inside the loop to update the variable, otherwise you could have an infinite loop. Initialize Initialize false Condition Action true Loop update Action true Loop update Condition false Pretest loop Posttest loop • Two general categories of loops: • event-controlled loop - this is where an event will stop the loop from executing (e.g. reading the end of a file.) • counter-controlled (conditional) loop - this is used if the number of times an action to be repeated is known in advance.

  3. Repetition Loops in C There are three loop statements in C: the while, the for, and the do…while. The first two are pretest loops and the do…while is a posttest loop. All of them can be used for even-controlled and counter-controlled loops. The while and do…while are most commonly used for event-controlled loops and the for is usually used for counter-controlled loops. All three of these loop constructs continue looping when the condition is true and terminate when it is false. The while loop The while loop uses an expression to control the loop. Since it is a pretest loop, it tests the expression before every iteration of the loop. Syntax: while (expression) statement no semicolon false expression true statements If the expression evaluates to true ( any nonzero value) the statements are executed and if it evaluates to false ( 0 ) the loop body is skipped and execution continues after the loop construct.

  4. Repetition Example of the while statement: loop repetition condition count_emp is tested before the start of each loop repetition. If the condition is true the loop body is executed again loop control variable initializedbefore entering while statement count_emp = 0; while( count_emp < 7) { printf(“Enter Hours >”); scanf(“%d”, &hours); printf(“Enter rate >”); scanf(“%lf”, &rate); pay = hours * rate; printf(“Pay is $%6.2f\n”, pay); count_emp = count_emp + 1; } printf(“\nAll employees processed\n”); loop body executed once for each value of count_emp that is less than 7 control variable is incremented ( updated ) in the body of the loop when count_emp becomes 7, the condition evaluates to false and control passes to the statement that follows the loop body

  5. Repetition The for loop The for statement is a pretest loop that uses three expressions. The first expression contains any initialization statements, the second contains the terminating expression (test ) and the third contains the updating expression. The for loop is used when your loop is to be executed a known number of times. You can do the same thing with a while loop. But a for loop is easier to read and more natural for counting loops. Syntax: for ( expr 1; expr 2; expr 3 ) statement expr1 false expr3 expr2 true expr 1 statement false expr 2 true statement expr 3 Flowchart Expanded flowchart

  6. Repetition 2nd Example of the for statement: semicolons total_pay = 0.0 ; for( count_emp = 0 ; count_emp < number_emp ; count_emp++ ) { printf(“Enter Hours >”); scanf(“%d”, &hours); printf(“Enter rate >”); scanf(“%lf”, &rate); pay = hours * rate; printf(“Pay is $%6.2f\n”, pay); total_pay = total_pay + pay; } printf(“\nAll employees processed\n”); printf(“Total payroll is $%8.2f\n”, total_pay); exp 2 loop repetition condition count_emp is tested before the start of each loop repetition if the condition is true the loop body is executed again if the condition is false, the loop is exited and the next program statement after the for statement is executed 1st exp 1 initialization expression - initializes the loop control variable - only done once 4th expr 3 update expression the value of the control variable is changed and then the repetition condition is tested again 3rd loop body if the condition is true, the statements in the loop body are executed. After the last statement in the loop body is executed, the update expression is executed.

  7. Repetition Example 2 of the for statement:

  8. Repetition The do…while loop The do…while statement is a posttest loop. Like the while and for loops, it also uses an expression to control the loop, but it tests the expression after the execution of the body. Syntax: • do • statement • while (expression) ; statements semicolon expression true false

  9. Repetition Example of the do…while statement:

  10. Repetition • Results: • while loop: 1 2 3 4 5 6 7 8 9 10 • Loop Count: 11 • Number of tests: 11 • do…while loop: 1 2 3 4 5 6 7 8 9 10 • Loop Count: 11 • Number of tests: 10 The Comma expression A comma expression is a complex expression made up of two expressions separated by commas. Although it can be used in many places, it is generally used only in for statements. The expressions are evaluated left to right. The value and type of the expression are the value and type of the right expression - the other expression is included for its side effect. The comma expression has the lowest priority of all expressions, priority 1. Example: for ( sum = 0, i = 1; i <= 20; i++ ) { scanf ( “%d”, &a ); sum += a ; } Comma expressions can be nested. When they are, all expression values other than the last are discarded.

  11. Repetition The nested Loop A nested loop is a loop within a loop. Within each iteration of the first or outer loop and inner loop is executed one or more times. Example: #include <stdio.h> void main(void) { int i, j, k, m = 0; for ( i = 1; i <= 5 ; i += 2 ) for ( j = 1 ; j <= 4 ; j++ ) { k = i + j ; printf( “ i = %3d, j = %3d, k = %3d\n”, i, j, k); } m = k + i; } } Output: i = 1, j = 1, k = 2 i = 1, j = 2, k = 3 i = 1, j = 3, k = 4 i = 1, j = 4, k = 5 i = 3, j = 1, k = 4 i = 3, j = 2, k = 5 i = 3, j = 3, k = 6 i = 3, j = 4, k = 7 i = 5, j = 1, k = 6 i = 5, j = 2, k = 7 i = 5, j = 3, k = 8 i = 5, j = 4, k = 9

  12. Repetition Other statements related to looping ( the break & continue) The break statement causes a loop to terminate. It is the same as setting the loop’s limit test to false. break can be used in any of the loop statements - while, for and do…while and in the selection switch statement. Good structured programming limits its use to the switch statement. The continue statement does not terminate the loop, but simply transfers to the testing statement in the while and do…while statements and transfers to the updating expression in a for statement. The use of the continue statement is also considered unstructured programming.

More Related