Lecture5_handout

Lecture5_handout - Overview EE140: Introduction to...

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

View Full Document Right Arrow Icon
1 E140: Introduction to EE140: Introduction to Programming Concepts for Engineers Lecture 5 1 Program Repetition Overview while loop while (expression) { statements; } do-while loop do { statements; } while (expression); 2 for loop for (expression1; expression2; expression3) { statements; } while loop Entry Expression Statements True False Exit while (expression) { statements; } 3 Example 1 #include <stdio.h> 000 int main( void ) { int x = 0; while( x <= 10 ) { printf( "%d\t%d\t%d\n", x, x*x, x*x*x ); x++; } return 0; } 1 1 1 248 392 7 41 6 6 4 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 0 00 000 4 10 100 1000 \t --- Tab character, create a fixed number of spaces.
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 Example 2: Sentinel-Controlled Loops A sentinel value is used to terminate the loop (such as -1). Sentinel-controlled loops are also called indefinite loops. #include <stdio.h> int main( void ) { int grade; //variable grade int counter=0; //set variable counter to 0 printf("Enter grade (-1 to end): "); scanf("%d", &grade); /* input first grade */ while( grade != -1 ) { 5 g { counter ++; /*increase the counter by 1 each iteration*/ printf("Enter grade (-1 to end): "); scanf("%d", &grade); /* get next grade */ } printf("You entered %d grades.\n", counter); /*display counter*/ return 0; } do-while loop Entry do { Expression Statements True False { statements; } while (expression); 6 Exit
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/04/2011 for the course EE 150 taught by Professor Lanxiang during the Spring '11 term at Montgomery College.

Page1 / 5

Lecture5_handout - Overview EE140: Introduction to...

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