Ch3 repetition


Published on

Published in: Technology
  • Be the first to comment

Ch3 repetition

  2. 2. REPETITION STRUCTURES <ul><li>Programmer specifies an action to be repeated while some condition remains true </li></ul><ul><li>Essential of Repetition : </li></ul><ul><li>• Loop </li></ul><ul><li>– Group of instructions computer executes repeatedly while some condition remains true </li></ul><ul><li>• Counter-controlled repetition </li></ul><ul><li>– Definite repetition: know how many times loop will execute </li></ul><ul><li>– Control variable used to count repetitions </li></ul><ul><li>• Sentinel-controlled repetition </li></ul><ul><li>– Indefinite repetition </li></ul><ul><li>– Used when number of repetitions not known </li></ul><ul><li>– Sentinel value indicates &quot;end of data &quot; </li></ul>
  4. 5. The while Repetition Structure <ul><li>The while statement in C specifies that a section of code should be executed while a certain condition holds true. </li></ul><ul><li>The syntax is: </li></ul><ul><ul><ul><li>while (condition) </li></ul></ul></ul><ul><ul><ul><li>statement ; </li></ul></ul></ul><ul><li>The condition is an </li></ul><ul><li>expression that has a true or false value. </li></ul><ul><li>The statement is the statement to be repeatedly executed while the condition is true. </li></ul>
  5. 6. <ul><li>Example 3.12 This program will print number 1 to 3. </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int count = 1; </li></ul><ul><li>while (count < 4) </li></ul><ul><li>{ </li></ul><ul><li>printf (“%d ”, count)&quot;; </li></ul><ul><li>count++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  6. 7. The do...while Repetition Structure <ul><li>There is another repetition control structure which is very similar to the while statement. </li></ul><ul><li>The only difference is that the expression which determines whether to carry on looping is evaluated at the end of each loop. The syntax is: </li></ul><ul><ul><ul><li>do </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>statement ; </li></ul></ul></ul><ul><ul><ul><li>} while (condition); </li></ul></ul></ul>
  7. 8. The do...while Repetition Structure <ul><li>This structure is used much more rarely than the while statement, </li></ul><ul><li>but is occasionally useful if we want to ensure that the loop statement is executed at least once. </li></ul>
  8. 9. Example 3.13: This program will print “This is cycle ?” 6 times. <ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int ctr = 1; </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>printf(“This is cycle %dn”, ctr); </li></ul><ul><li>ctr++; </li></ul><ul><li>} while (ctr<= 6); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>This is cycle 1 </li></ul><ul><li>This is cycle 2 </li></ul><ul><li>This is cycle 3 </li></ul><ul><li>This is cycle 4 </li></ul><ul><li>This is cycle 5 </li></ul><ul><li>This is cycle 6 </li></ul>
  9. 10. The for Repetition Structure <ul><li>Syntax : </li></ul><ul><li>for (initialization ; loop_control_expression ; update_expression ) </li></ul><ul><li>statement ; </li></ul><ul><li>for repetition structure handles all the details of counter-controlled repetition automatically. </li></ul><ul><li>In executing a for statement, the computer does the following: </li></ul><ul><li>(a) initialization is executed. </li></ul><ul><li>(b) then, the loop_control_expression is evaluated. </li></ul><ul><li>If it computes to zero, the loop is exited. </li></ul><ul><li>(c) if the loop_control_expression yields a nonzero value, the loop body is </li></ul><ul><li>executed and then the update_expression is evaluated. </li></ul><ul><li>(d) The loop_control_expression is again tested. </li></ul><ul><li>Thus, the loopbody is repeated until the loop_control_expression computes to a zero value. </li></ul>
  10. 11. <ul><li>Example 3.14: This program will print “Hello” 5 times. </li></ul><ul><ul><li>#include <stdio.h> </li></ul></ul><ul><ul><li>void main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>f or(i=1; i<=5; i=i+1) </li></ul></ul><ul><ul><li>printf(&quot;Hellon&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  11. 12. <ul><li>Example 3.15: This program will print “Hello” and “Goodbye” twice. </li></ul><ul><ul><li>#include <stdio.h> </li></ul></ul><ul><ul><li>void main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>for(int i=1; i<=2; i=i+1) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>printf(&quot;Hellon&quot;); </li></ul></ul><ul><ul><li>printf(&quot;Goodbyen&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 13. The break statement <ul><li>A break statement inside the body of a loop breaks completely out of the loop. </li></ul><ul><li>No more instructions in the body of the loop are executed, and the next statement after the loop will be executed. </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>for (int x = 1 ; x <= 10 ; x++) </li></ul><ul><li>{ </li></ul><ul><li>if( x == 5) </li></ul><ul><li> break; /*break loop only if x== 5 */ </li></ul><ul><li>printf(“%dt”,x); </li></ul><ul><li>} </li></ul><ul><li>printf(“nBroke out of loop at x==%dn”,x); </li></ul><ul><li>} </li></ul>// Output 1 2 3 4 Broke out of loop at x == 5
  13. 14. The continue statement <ul><li>The continue statement just skips any instructions after it on that iteration of the loop. </li></ul><ul><li>The current iteration of the loop is terminated, and the loop statement is executed again as if the last instruction of the loop body has been reached. </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>for (int x=1 ; x<=10 ; x++) </li></ul><ul><li>{ </li></ul><ul><li> if ( x == 5) </li></ul><ul><li> continue ; /* skip remaining code in loop only if x==5 */ </li></ul><ul><li> printf ( “%dt”, x) ; </li></ul><ul><li>} </li></ul><ul><li>printf(“nBroke out of loop at x==%dn”,x) ; </li></ul><ul><li>} </li></ul>//output : 1 2 3 4 6 7 8 9 10
  14. 15. The exit function <ul><li>The exit function forces a program to terminate as if it executed normally. </li></ul><ul><li>The function often is used to terminate a program when an error is detected in the input or if file to be processed by the program cannot be opened. </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>#include<stdlib.h> </li></ul><ul><li>main ( ) </li></ul><ul><li>{ </li></ul><ul><li>exit (1); /* program will end here*/ </li></ul><ul><li>printf (“ C programming is funn”); </li></ul><ul><li>printf (“ C is a powerful language”); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  15. 16. TUTORIAL