1 / 47

Beginning C For Engineers Fall 2005

Beginning C For Engineers Fall 2005. Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina Schimanski. Homework. Read Chapter 4 HW 3 Don’t forget the classroom change next week: Section 2 (Wednesday): Sage 5101

kiona-baird
Download Presentation

Beginning C For Engineers Fall 2005

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. Beginning C For EngineersFall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina Schimanski

  2. Homework • Read Chapter 4 • HW 3 • Don’t forget the classroom change next week: • Section 2 (Wednesday): Sage 5101 • Section 4 (Thursday): Walker 5113

  3. More C Operators int age; age = 8; age = age + 1; 8 age 9 age

  4. PREFIX FORMIncrement Operator int age; age = 8; ++age; 8 age 9 age

  5. POSTFIX FORM Increment Operator int age; age = 8; age++; 8 age 9 age

  6. Decrement Operator int dogs; dogs = 100; dogs--; --dogs; 100 dogs 98 dogs

  7. Which Form to Use • When the increment (or decrement) operator is used in a “stand alone” statementsolely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form • But… when the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results USE EITHER dogs--;--dogs ;

  8. The += operator • If you wish to add a value to a variable and put the result back in the variable, use the += operator • ex: int stuff = 4; stuff += 3; /* same as stuff = stuff + 3; stuff is now 7 */ Adds the value on the right to the variable on the left.

  9. Other similar operators • As you might expect, there are also operators for -=, *=, /=, and %=. x += y; /* same as x = x + y; */ z *= 3; /* same as z = z * 3; */ y /= 2; /* same as y = y / 2; */ i %= 5; /* same as i = i % 5; */

  10. What is a loop? • A loop is a repetition control structure. • It causes a single statement or block to be executed repeatedly

  11. Loop constructs in C • while • execute block of statements repeatedly as long as some condition is true • condition is tested before block of statements are executed • for • an abbreviation for a collection of statements that use a while loop for creating counting loops • ideal when the number of iterations of the loop is “known” beforehand • do – while • similar to while • condition is tested after block of statements are executed

  12. While Statement SYNTAX while ( Expression) { . . // loop body . } NOTE: Loop body can be a single statement, a null statement, or a block.

  13. Event-controlled Loops Keep processing data until a certain event happens, for example • a special value is entered (e.g. -1) to indicate that processing should stop • there is more data in the file • until the value of a flag changes 13

  14. #include <stdio.h> int main( ) /* Example from Lecture 2 */ { int total = 0, num; printf( “Enter a number (-1 to stop ) ”); scanf(“%d”, &num); /* Read until the user enters -1 */ while (num != -1) { total = total + num; printf( “Enter a number (-1 to stop ) ”); scanf(“%d”, &num); } printf( “Total = %d”, total); return 0; }

  15. #include <stdio.h> int main( ) { int total = 0, num; printf( “Enter a number (-1 to stop ) ”); scanf(“%d”, &num); /* Read until the user enters -1 */ while (num != -1) { total += num; printf( “Enter a number (-1 to stop ) ”); scanf(“%d”, &num); } printf( “Total = %d”, total); return 0; }

  16. Flag-controlled Loops • Initialize a flag to 1(true) or 0 (false) • Use a meaningful name for the flag • A condition in the loop body changes the value of the flag • Test for the flag in the loop test expression

  17. int numpos = 0; int isSafe = 1 /* initialize Boolean flag */ /*Loop while flag is true, i.e. while only positive numbers */ while (isSafe) { scanf(“%d”, &num); if ( num < 0 ) isSafe = 0;/* change flag value */ else numpos++; } printf(“%d positive numbers were entered\n”, numpos);

  18. Count-controlled Loops contain: • An initialization of the loop control variable • An expression to test for continuing the loop • An update of the loop controlvariable to be executed with each iteration of the body • * Although count-controlled loops can be implemented using while loops, it is more common to use for loops for counting

  19. Using For loops for Counting SYNTAX for ( initialization ; test expression; update ) { 0 or more statements to repeat } update initialization statement statement . . . test true false done

  20. Example of Repetition int num; for ( num = 1; num <= 3; num++) { printf(“%d Potato\n”, num); } initialization test update

  21. Example of Repetition ? num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  22. Example of Repetition 1 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  23. Example of Repetition 1 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } true OUTPUT

  24. 1 Potato Example of Repetition 1 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  25. 1 Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  26. 1 Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } true OUTPUT

  27. 1 Potato 2 Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  28. 1 Potato 2 Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  29. 1 Potato 2 Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } true OUTPUT

  30. 1 Potato 2 Potato 3 Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  31. 1 Potato 2 Potato 3 Potato Example of Repetition 4 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } OUTPUT

  32. 1 Potato 2 Potato 3 Potato Example of Repetition 4 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } false OUTPUT

  33. Example of Repetition 4 num int num; for ( num = 1 ; num <= 3; num++) { printf(“%d Potato\n”, num); } false When the loop control condition is evaluated and has value false, control passes to the statement following the for statement.

  34. Count-controlled Loop int count ; for ( count = 0 ; count < 4 ; count++ ) { printf(“%d \n”, count); } printf( “Done\n”); OUTPUT: 0 1 2 3 Done

  35. Count-controlled Loop int count ; for ( count = 0 ; count <= 4 ; count++ ) { printf(“%d \n”, count); } printf( “Done\n”); OUTPUT: 0 1 2 3 4 Done

  36. Equivalence with while loop • The following while loop is equivalent to the previous for loop (on the previous slide) int count ; count = 0; /* initialization */ while (count <= 4) /* test expression */ { printf(“%d \n”, count); count++; /* update loop variable*/ } printf( “Done\n”);

  37. Summing Values in Loops int num, count; int total = 0; for ( count = 0; count < 10; count++) { printf(“Enter a numer:”); scanf (“%d”, &num); total += num; }

  38. For Loop Variations • The counter variable can be initialized to anything. • The test expression can be any logical expression involving the counter variable. • The increment can be any value, positive or negative. • Examples: for (i = 0; i < 10; i += 2) for (k = 20; k > 0; k -= 1) for (j = 1; j != 6; j += 2) Warning: What happens in the last example?

  39. for (i = 0; i < 10; i +=2) { printf(“%d\n”, i ); } for (j = 20; j >= 0; j -= 3) { printf(“%d\n”, j ); } What Is Displayed By Each Program Segment? 20 17 14 11 8 5 2 0 2 4 6 8

  40. for (j = 0; j <= 3; j ++) { for (k = j; k < 5; k ++) { printf(“%d %d\n”, j , k); } printf (“\n”); } Nested For Loops 0 0 0 1 0 2 0 3 0 4 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 What is printed to the screen?

  41. Multiple Selection • An algorithm may contain a series of decisions in which a variable or expression is tested • Many if…else if… else statements • Switch statement

  42. Switch Statement true case a action(s) break; case a false true case b action(s) break; case b false true case z action(s) case z break; false default

  43. Place within a while loop to capture many grades: /* count the letter grades */ switch(grade) { case ‘A’: aCount++; break; case ‘B’: bCount++; break; case ‘C’: cCount++; break; case ‘D’: dCount++; break; default: fCount++; }

  44. Switch Statement • Each case can have one or more actions • Each break statement causes control to immediately exit the switch statement • What happens if you leave out a break?

  45. /* count the letter grades */ switch(grade) { case ‘A’: aCount++; break; case ‘B’: bCount++; /* No break! */ case ‘C’: cCount++; break; case ‘D’: dCount++; break; default: fCount++; } Error: What is printed out for bCount and cCount?

  46. /* count the letter grades */ switch(grade) { case ‘A’: case ‘a’: aCount++; break; case ‘B’: case ‘b’: bCount++; break; case ‘C’: case ‘c’: cCount++; break; case ‘D’: case ‘d’: dCount++; break; default: fCount++; } More than one case can have the same action(s).

  47. The Lab • Tracing code by hand • Summing, Average • Max/Min • In <limits.h> there is INT_MAX and INT_MIN • These defined constants are useful for initializing your min and max variables

More Related