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

Info iconThis preview shows pages 1–9. 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 DocumentRight 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 DocumentRight 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 DocumentRight 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 DocumentRight 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
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/29/2009 for the course EE 142 taught by Professor Bensollimketkai during the Spring '09 term at Washington State University .

Page1 / 56

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

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

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