04a intro while

Preview:

Citation preview

CSC128 Fundamentals of Computer Problem Solving

In this chapter, you will:• Learn about repetition (looping) control

structures• Explore how to construct and use count-

controlled, sentinel-controlled, flag-controlled

• Discover how to form and use nested control structures

2

• Repetition allows you to efficiently use variables

• Can input, add, and average multiple numbers using a limited number of variables

• For example, to add five numbers:–Declare a variable for each number, input the

numbers and add the variables together– Create a loop that reads a number into a variable

and adds it to a variable that contains the sum of the numbers

3

• The general form of the while statement is:

while is a reserved word• Statement can be simple or compound• Expression acts as a decision maker and is

usually a logical expression • Statement is called the body of the loop • The parentheses are part of the syntax

4

• Infinite loop: continues to execute endlessly– Avoided by including statements in loop body that

assure exit condition is eventually false

5

6

Example 5-1◦ Variable i – Loop Control Variable (LCV)◦ Within the loop, i becomes 25 but is not printed because

the entry condition is false.◦ What would happen if expression i = i + 5; is omitted?◦ What would happen if the statement i=0; is omitted?◦ i=0;while (i <= 20){ i = i+5; cout << i << “ “;}Produce semantic error; we rarely want a condition to be true for i<=20 and yet produce results for i>20.

◦ Putting a semicolon at the end of the while loop (after the logical expression), then the action is empty. The statement within the braces do not form the body of the while loop.

7

8

The LCV must be properly initialized before the while loop. LCV should eventually make the expression evaluate to

false by updationg or reinitializing the LCV in the body of the while loop.

The form of writing the while loops.

//initialize the LCVwhile (expression) //expression tests the LCV{ . . . //update the LCV . . .}

9

1. What is the output for the following while statement. Use tracing table to trace the output.

int n = 1;while (n <= 5){ n = n + 1; cout << n << “ “;}

2. What is the output for the following while statement by using the data below as input for variable item. 10 3 4 2 5 11 int item, data = 1; sum = 0; while (sum <= 250){ cin >> item; data = data * item; sum = sum + data; cout << sum << endl;}

10

3. Write a program to display a series of number as shown below:40 38 36 34 32 30

3. Write a program using while loop to find the sum of integers 73 through 415 inclusive..

4. Write a program using while loop to display the even numbers of integers 1 through 20 inclusive.

11

/* Write a program to display a series of number as shown below: 40 38 36 34 32 30

*/int i;

i=40;

while (i >= 30){

cout << i << " " ;i = i - 2 ;

}

C++ Programming: From Problem Analysis to Program

Design, Fifth Edition 12

C++ Programming: From Problem Analysis to Program

Design, Fifth Edition 13

/* Write a program using while loop to display the even numbers of integers 1 through 20 inclusive.*/

int i;

i=1;

while (i <= 20){

if (( (i % 2) == 0)|| (i == 1))cout << i << " " ;

i = i + 1 ;}

C++ Programming: From Problem Analysis to Program

Design, Fifth Edition 14