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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 The for loop and scope 2 Repetition s 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"); 3 Looping via the for loop s for loop : A block of C code that executes a group of statements repeatedly until a given test fails. s General syntax: for ( <initialization> ; <test> ; <update> ) { <statement> ; <statement> ; ... <statement> ; } s Example: for (int i = 1; i <= 30; i++) { printf("I will not throw...\n"); } 4 The for loop is NOT a function s The for loop is a control structure —a syntactic structure that controls the execution of other statements. s Example: b “Shampoo hair. Rinse. Repeat .” 5 for loop over range of int s s We'll write for loops over integers in a given range. b 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> ++) { s 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 6 for loop flow diagram for ( <init> ; <test> ; <update> ) { <statement> ; <statement> ; ... <statement> ; } 2 7 Loop walkthrough s 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: 8 Loop example s Code: printf("+----+\n"); for (int i = 1; i <= 3; i++) { printf("\\ /\n"); printf("/ \\\n"); } printf("+----+\n"); Output : +----+ \ / / \ \ / / \ \ / / \ +----+ 9 Varying the for loop s The initial and final values for the loop counter variable can be arbitrary...
View Full Document

Page1 / 10

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online