Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.

Similar presentations


Presentation on theme: "© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The."— Presentation transcript:

1 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled Repetition 4.4The for Repetition Statement 4.5The for Statement: Notes and Observations 4.6Examples Using the for Statement

2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand multiple selection using the switch selection statement. –To be able to use the break and continue program control statements –To be able to use the logical operators.

3 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 4.1 Introduction This chapter introduces –Additional repetition control structures for Do … while –switch multiple selection statement –break statement Used for exiting immediately and rapidly from certain control structures –continue statement Used for skipping the remainder of the body of a repetition structure and proceeding with the next iteration of the loop

4 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 4.2The Essentials of Repetition Loop –Group of instructions computer executes repeatedly while some condition remains true Counter-controlled repetition –Definite repetition: know how many times loop will execute –Control variable used to count repetitions Sentinel-controlled repetition –Indefinite repetition –Used when number of repetitions not known –Sentinel value indicates "end of data"

5 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 4.3Essentials of Counter-Controlled Repetition Counter-controlled repetition requires –The name of a control variable (or loop counter) –The initial value of the control variable –An increment (or decrement) by which the control variable is modified each time through the loop –A condition that tests for the final value of the control variable (i.e., whether looping should continue)

6 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 4.3Essentials of Counter-Controlled Repetition Example: int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n", counter ); ++counter; // increment } –The statement int counter = 1; Names counter Defines it to be an integer Reserves space for it in memory Sets it to an initial value of 1

7 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 fig04_01.c Program Output 1 2 3 4 5 6 7 8 9 10

8 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 4.3Essentials of Counter-Controlled Repetition Condensed code –C Programmers would make the program more concise –Initialization, repetition condition, and increment are all included in the for statement header. for ( counter = 1; counter <= 10; counter++ ) printf( “%d\n, counter );

9 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 fig04_02.c

10 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 4.4The for Repetition Statement

11 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 4.4The for Repetition Statement Format when using for loops for ( initialization; loopContinuationTest; increment ) statement Example: for( int counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter ); –Prints the integers from one to ten No semicolon ( ; ) after last expression

12 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 4.4The for Repetition Statement For loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest ) { statement; increment; } Initialization and increment –Can be comma-separated lists –Example: for (int i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i );

13 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 4.5The for Statement : Notes and Observations Arithmetic expressions –Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Notes about the for statement: –"Increment" may be negative (decrement) –If the loop continuation condition is initially false The body of the for statement is not performed Control proceeds with the next statement after the for statement –Control variable Often printed or used inside for body, but not necessary

14 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 4.5The for Statement : Notes and Observations counter = 1 counter <= 10 true false counter = 1 counter++ Establishinitial value of control variable Determine iffinal value of control variable has been reached Body of loop (this may be many statements) Increment the control variable printf( "%d", counter );

15 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 fig04_05.c Program Output Sum is 2550


Download ppt "© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The."

Similar presentations


Ads by Google