Upgrade to Pro — share decks privately, control downloads, hide ads and more …

08-Loops.pdf

Avatar for William Albritton William Albritton
September 04, 2015
150

 08-Loops.pdf

Avatar for William Albritton

William Albritton

September 04, 2015
Tweet

Transcript

  1. Memory Allocation  Loops  while Loops  for Loops

     do Loops  break Statement  continue Statement  Input/Output Redirection
  2. Counting Loop  Loops that use counters to control the

    number of repetitions  The counter value is increased or decreased each time the loop body is executed  The loop terminates when the counter reaches a certain value  Example: print “hello world” ten times
  3. Sentinel Loop  Loops that use special values called sentinels

    to terminate the loop  The body of the loop is executed until the sentinel is found  For example, a program that continues calculating the interest rate and stops after the last customer  We can choose the sentinel to be the characters ‘n’ or ‘N’
  4. Pre Test and Post Test Loops  When is the

    condition tested? false true condition statement(s) to be repeated false true condition statement(s) to be repeated
  5. C Loops  C features three different kinds of loops

     while loop  for loop  do loop
  6. while Loop  May be a sentinel or counting loop

     Pre-test loop  The flowchart representation looks like this: false true condition initialization loop body update
  7. while Loop  Conditions are written the same way as

    in if statements  The difference in the flowchart is that the loop goes back to evaluating the condition false true condition initialization loop body update
  8. Infinite loop  Infinite loops happen when the condition in

    the while loop header is always true  It is important to verify in the body of the loop that there is a way for the condition to become false at some point  Sentinel or counting value
  9. for Loop Structure  The most commonly used looping statement

    in C  Includes  An initial value  The condition  An update statement  Usually a counting loop  Pre-test loop
  10. for(x=1; x < 11; x++){ printf(“%i\n”, x); } for Loop

    Execution  Executes ONCE, Initialization
  11. for(x=1; x < 11; x++){ printf(“%i\n”, x); } for Loop

    Execution  Condition Executes in each iteration
  12. do Loop Structure  It is like the while loop

    but the condition is tested at the end  The do loop will ALWAYS EXECUTE AT LEAST ONCE  Sentinel or counting loop  Post test loop
  13. The Break Statement  The break statement is used to

    exit from a switch statement or to terminate loops
  14. The Continue Statement  The continue statement is used to

    bypass the remainder of the current pass through a loop  The loop does not terminate because a continue statement is encountered  The loop goes back to evaluate the condition  This statement can be included within any of the loops that we know
  15. int x = 1; while(x <= 10){ printf(“ %i \n”,

    x); x++; if(condition){ other statements; continue; } other statements; }
  16. Nested Statements  C allows structures inside other structures 

    Any of these inside any other  if  if/else  while  for  do  switch (case)
  17. //generate multiplication table int number = 0, count = 0,

    product = 0; //nested for loops for(number=1; number<=MAX;number++){ for(count=1; count<=MAX; count++){ product = number * count; printf("%i * %i = %i\n", number, count, product); } printf("\n"); }
  18. I/O Redirection in UNIX  Instead of typing input for

    a program, you can use I/O redirection in UNIX to use a file as input or output to your program
  19. I/O Redirection in UNIX  Use less-than sign (<) for

    input  Use greater-than sign (>) for output  Can use both as well % ./program > output.txt % ./program < input.txt % ./program < input.txt > output.txt
  20. Example Program  See example code at: counting.c  Use

    EOF (end of file) to end the program  EOF is Ctrl-D on UNIX or Mac, Ctrl-Z on PC  Can do this with I/O redirection from a file % ./program < electricity.txt
  21. Memory Management  Loops  while Loops  for Loops

     do Loops  break Statement  continue Statement  Input/Output Redirection