{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

day03 - The for loop and scope 1 Repetition How can we...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 The  for  loop and scope
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    2 Repetition How can we eliminate this redundancy? printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n"); printf("I will not throw the principal’s toupee down the toilet\n");
Background image of page 2
    3 Looping via the  for  loop for loop : A block of C code that executes a group of statements repeatedly until a given test fails. General syntax: for ( <initialization> ; <test> ; <update> ) { <statement> ; <statement> ; ... <statement> ; } Example: for (int i = 1; i <= 30; i++) { printf("I will not throw...\n"); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    4 The  for  loop is  NOT  a function The for loop is a control structure —a syntactic structure that controls the execution of other statements. Example: “Shampoo hair. Rinse. Repeat .”
Background image of page 4
    5 for  loop over range of  int s We'll write for loops over integers in a given range. The <initialization> declares a loop counter variable that is used in the test, update, and body of the loop. for (int <name> = 1; <name> <= <value> ; <name> ++) { Example: for (int i = 1; i <= 4; i++) { printf("%d squared is %d\n", i, (i * i)); } "For each int i from 1 through 4, ...“ Output : 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    6 for  loop flow diagram for ( <init> ; <test> ; <update> ) { <statement> ; <statement> ; ... <statement> ; }
Background image of page 6
    7 Loop walkthrough Code: for (int i = 1; i <= 3; i++) { printf("%d squared is %d\n", i, (i * i)); } Output : 1 squared is 1 2 squared is 4 3 squared is 9 i:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    8 Loop example Code: printf("+----+\n"); for (int i = 1; i <= 3; i++) { printf("\\ /\n"); printf("/ \\\n"); } printf("+----+\n"); Output : +----+ \ / / \ \ / / \ \ / / \ +----+
Background image of page 8
    9 Varying the  for  loop The initial and final values for the loop counter variable can be arbitrary expressions: Example: for (int i = -3 ; i <= 2; i++) { printf("%d\n", i); } Output : -3 -2 -1 0 1 2 Example: for (int i = 1 + 3 * 4 ; i <= 5248 % 100 ; i++) { printf("%d squared is %d\n", i, (i * i)); }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    10 Varying the  for  loop The update can be a -- (or any other operator).
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}