24
Lect 10 P. 1 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition Repetition Structures Lecture 10

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Embed Size (px)

Citation preview

Page 1: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 1

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Repetition Structures

Lecture 10

Page 2: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 2

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Repetition Structures

• Repetition structures are used to repeat statements or blocks of code.

• The decision whether to repeat the code is based on the evaluation of a logical expression. If the expression is true, the code is executed. If false, the code is not executed.

• Repetition structures may repeat the code a definite number of times (usually for loops) or an indefinite number of times (usually while or do while loops).

Page 3: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 3

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Repetition Structures

• Both for loops and while loops are top test loops. They evaluate the logical expression before executing any of the code in the loop. If the expression is false, the block of code does not get executed.

• A do while loop is a bottom test loop. It executes the block of code once and then evaluates the logical expression to determine whether or not to execute it again.

Page 4: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 4

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

While Loops (Syntax)

The syntax of a while loop is:

while ( logical expression is true ) statement ;

or

while ( logical expression is true ){ statement ; statement ;}

Page 5: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 5

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

While Loops

/* This program associates letter grades with

numeric test scores. It continues to request

input until the user types in a 0 and then it

terminates with a message. */

#include <stdio.h>

int main ( )

{

int score ;

Page 6: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 6

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

While Loops

score = -1 ;

while (score != 0)

{

printf ("enter a test score (0 to quit) >") ;

scanf ("%d", &score) ;

if (score >= 90)

printf ("Your score of %d is a A\n", score) ;

else if (score >= 80 && score <90)

printf ("Your score of %d is a B\n", score) ;

else if (score >= 70)

printf ("Your score of %d is a C\n", score) ;

Page 7: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 7

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

While Loops

else if (score >= 60) printf ("Your score of %d is a D\n", score) ;

else if (score != 0)

printf ("Your score of %d is an E\n", score) ;

else

printf ("Terminating at your request.\n\n") ;

} /* End of While loop */

} /* End of "main" function */

Page 8: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 8

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Do-While Loops (Syntax)

do statement ; while ( logical expr is true ) ;

or, more commonly

do

{

statement ;

statement ;

} while ( logical expression is true ) ;

Page 9: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 9

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Do-While Loops

/* This program associates letter grades with

numeric test scores. It continues to request

input until the user types in a 0 and then it

terminates with a message. */

 

#include <stdio.h>

int main ( )

{

int score;

Page 10: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 10

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Do-While Loops

do

{

printf ("enter a test score (0 to quit) >") ;

scanf ("%d", &score) ;

if (score >= 90)

printf ("Your score of %d is a A\n", score) ;

else if (score >= 80 && score <90)

printf ("Your score of %d is a B\n", score) ;

else if (score >= 70)

printf ("Your score of %d is a C\n", score) ;

Page 11: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 11

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Do-While Loops

else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else if (score != 0)

printf ("Your score of %d is an E\n", score) ;

else

printf ("Terminating at your request.\n\n") ;

} while (score != 0) ;

}

Page 12: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 12

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

For Loops (Syntax)

for (expr1 ; expr2 ; expr3){

statement(s) ; }

Expression1 sets initial conditions. It can be a single math expression or multiple math expressions separated by commas(,). It gets executed only once before the loop executes.

Page 13: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 13

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

For Loops

for ( expr1 ; expr2 ; expr3 ){

statement(s) ;}

Expression2 is the logical expression to be evaluated as true or false. It does NOT need to contain any of the variables that are in expression1. It gets evaluated after expression1 and again after expression3.

Page 14: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 14

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

For Loops

for ( expr1 ; expr2 ; expr3 ){

statement(s) ;}

Expression3 defines changes to conditions. It can be multiple expressions separated by commas. It gets executed after the statements in the loop are executed. The expressions do NOT have to be related to either those in expression1 or in expression2 (but they often are).

Page 15: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 15

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Expressions in For Loops

• Any (or all) of the expressions (i.e., expression1, expression2, or expression3) can be eliminated, but the semicolons MUST be used.

• Eliminating expression1 and expression3 will cause the for loop to behave like a while loop since only expression2 (the logical one) will be left.

• Eliminating expression2 will create a loop that will never terminate unless there is a break or exit statement inside the loop (or someone pulls the plug.)

Page 16: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 16

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Simple For Loop Example

int counter;

for (counter = 1; counter <= 10; counter++)

{

printf ("The counter value is: %2d\n", counter);

}

Page 17: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 17

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Equivalency of While Loop vs. For LoopThe Loop:

expression1 ;while ( expression2 ){

statement ; expression3 ;

}

Is Equivalent to:

for ( expr1 ; expr2 ; expr3 ) statement ;

Page 18: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 18

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Equivalency of While Loop vs. For Loop

The Loop:expression1 ;while ( expression2 )

expression3 ; Is Equivalent to: expression1 ;

for ( ; expression2 ; ) expression3 ;

Page 19: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 19

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Simple While Loop Example

int counter ;

counter = 1 ;while ( counter <= 10 ){ printf ("The counter value is: %2d\n", counter) ; counter++ ;}

Page 20: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 20

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Example of For Loop

#include <stdio.h>

int main ( )

{

int k, n ;

for (k = 1, n = 12 ; k < 9 && n > 6 ; k++, n--)

printf ("k=%d, n=%d\n" , k , n ) ;

}

Page 21: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 21

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Another Example of For Loop

#include <stdio.h>

int main ( )

{

int k = 1, n = 12 ;

for ( ; k < 9 && n > 6 ; )

printf ("k=%d, n=%d\n" , k++ , n-- ) ;

}

Page 22: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 22

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

A More Complex Example of For Loop

#include <stdio.h>#include <stdlib.h>int main ( ){ int rand_num, seed, k = 1 ; printf ("Enter a seed for the generator >") ; scanf ("%d", &seed) ; srand (seed) ; for ( ; ; ) { rand_num = 0.5 + 1000.0 * rand ( ) / RAND_MAX ; printf ("The number is %d and k = %d\n", rand_num, k) ; if (rand_num == 6 || k == 2000) break ; k++ ; }}

Page 23: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 23

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Problem G09

• Problem G09 combines the while loop, the for loop, and the switch/case structure plus other program elements that we have used previously.

• From the algorithm develop your own pseudo code for this program.

• Break the program into parts and then write, compile, and test the individual parts.

• Do NOT try to write the entire program without compiling

• Make variables mnemonic using three or more characters

Page 24: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures

Lect 10 P. 24

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

General Structure of Program

• #include <xxx>• int main( )• {

– /*declare variables */

– /* start loop */

• }