{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture5_handout

# Lecture5_handout - Overview EE140 Introduction to...

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

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.

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online