cs_102_lab_4
TRANSCRIPT
C++ Programming Lab Manual LAB4
Lab 4Control Structure
(Repetition)
Objectives
In this lab, you will:
Learn about repetition (looping) control structures with while
Explore how to construct and use count-controlled, sentinel-
controlled, flag-controlled, and EOF–controlled repetition
structures
Examine break and continue statements
Discover how to use the do-while loop structure
1
C++ Programming Lab Manual LAB4
Assignment Cover Sheet Lab 4
Name: ------------------------------------ Student ID: ----------------------------
Section: ------------------------------------ Date: -----------------------------
Activities Assigned: check or list exercise numbers
Completed
Submitted Grade
PreLab The Questions of page 5 /10
Inlab Example4-1 /5
Example4-2 /5
Example4-3 /5
Example4-4 /5
Example4-5 /5
Example4-6 /5
Example4-7 /5
Assignmnet1 /10
Assignmnet2 /10
PostLab Project1 /10
Project2 /10
2
C++ Programming Lab Manual LAB4
PreLab
1. What is the difference between while loop and do-while loop
2. What are the types of while loop structure?
3. Write a program that prompt the user to insert a number and prints stars as many as the
inserted number
3
C++ Programming Lab Manual LAB4
Outline: Lesson 4-1: While repetition statements
Lesson 4-2: Counter-Controlled while loops
Lesson 4-3: Sentinel-Controlled while loops
Lesson 4-4: Flag-Controlled while loops
Lesson 4-5: Do-While loop
Lesson 4-6: Break and Continue
4
C++ Programming Lab Manual LAB4
While repetition statements Lesson 4-1
A repetition statement (also called a looping statement or a loop) allows the programmer to
specify that a program should repeat on action while some condition remains true.
The general form of the while statement is:
fig4-1: Flow Chart for while loop structure
While the expression is true the statement will be executed repeatedly until the expression is
being false.
Designing while loop:
While loops are written in the following form:
// initialize the loop control variable(s)
5
while(expression)
statement
C++ Programming Lab Manual LAB4
while(expression) // expression tests the loop control variable(s)
{
Statement;
//update the loop control variable(s)
}
Example4-1: write this program which uses the while loop and show the output
The output:
There are four while loop types:
Counter-Controlled while loops
Sentinel-Controlled while loops
Flag-Controlled while loops
EOF Controlled while loops
The next three lessons will talk about the first three while loop types.
6
#include <iostream> //line1using namespace std; //line2int main() //line3{ //line4
int i = 0; //line5while (i <= 20) //line6{ //line7
cout << i << " "; //line8 i = i + 5; //line9} //line10
cout << endl; //line11return 0; //line12
} //line13
C++ Programming Lab Manual LAB4
Counter-Controlled while loops Lesson 4-2
If you know exactly how many pieces of data need to be read, the while loop becomes a
counter-controlled loop.
Syntax:
Example4-2: Write the following program and show the output
7
counter = value; // initialize the loop control variable
while (counter < N) // test the loop control variable
{
….
counter ++; // update the loop control variable
…
}
C++ Programming Lab Manual LAB4
Run the program with different values and see the output.
The output:
Example4-3: Write the following code and show the output
8
#include <iostream> //line1using namespace std; //line2//Program Computing factorials with a while loop //line3 int main() //line4{ //line5 int number, factorial, counter; //line6 cout << "Enter a positive integer: "; //line7 cin >> number; //line8 factorial = 1; //line9 counter = 1; //line10 while(counter <= number) { //line11 factorial *= counter; //line12 counter += 1; //line13 } //line14 cout << "The factorial of " << number << " is " << factorial << endl; //line15 return 0; //line16} //line17
#include <iostream> //line1#include <cstdlib> //line2using namespace std; //line3void main() //line4{ //line5 int count = 1; //line6
while(count <= 10) //line7{
cout << count++ <<") " <<rand()%100 <<endl; //line8 } //line9} //line10
C++ Programming Lab Manual LAB4
The output:
Sentinel-Controlled while loops Lesson 4-3
Sentinel variable is tested in the condition and loop when Sentinel is encountered.
Syntax:
Example 4-4: Write the following code and how the output
9
cin>> variable; //initialize the loop control variablewhile(variable != sentinel) // test the loop control variable{
…cin>> variable; // update the loop control variable…
}
#include "stdafx.h" //line1#include <iostream> //line2using namespace std; //line3/*Program finding maximum value using while loop and if statement.*/ //line4 int main() //line5{ //line6 int value=0; //input value //line7 int max=0; //maximum value //line8 while(value!=-1){ //line9 cout << "Enter a positive integer: " << "(-1 to stop):"; //line10 cin >> value; //line11 if(value > max) //line12 max = value; //line13
} //line14 cout << "The maximum value found is: " << max << endl; //line15 return 0; //line16} //line17
C++ Programming Lab Manual LAB4
Run the program with different values, and trace the values by adding (cout) statements.
The output:
Flag-Controlled while loops Lesson 4-4
A flag-controlled while loop uses a Boolean variable to control the loop.
Syntax:
Example4-5: Write the following program and show the output
10
found = false; //initialize the loop control variablewhile(!found) // test the loop control variable{
…if(expression) found = true; // update the loop control variable…
}
C++ Programming Lab Manual LAB4
The output:
Do-While loop Lesson 4-5
The Do-While loop is similar to while loop except one difference. The do-while loop execute at least once. The while loop execute at least zero times.
Syntax:
11
#include <iostream> //line1using namespace std; //line2 void main() //line3{ //line4 int num=7; //line5
bool b=false; //line6while(b != true) //b==false //line10{ //line12
cout<<"*"; //line13num=num+3; //line14if(num > 20) b=true; //line15
} //line16
} //line17
do
statement
while(expression);
C++ Programming Lab Manual LAB4
The statement executes first, and then the expression is evaluated. To avoid an infinite loop,
body must contain a statement that makes the expression false. The statement can be simple or
compound.
The while loop types that been discussed previously are considered as a do-while loop types
too.
Fig4-2: The Do-While loop flow chart
Example4-6:
12
Consider the following two loops:
a. i = 11; while(i <= 10) { cout << i << “ “;
i = i + 5; } cout << endl;
b. i = 11; do { cout << i << “ “;
i = i + 5; } while(i <= 10); cout << endl;
In (a) the while loop produces nothing. In (b), the do … while loop outputs the number 11 and also changes the value of i to 16.
C++ Programming Lab Manual LAB4
Break and Continue Lesson4-6
Break statement is used for two purposes:
a. To exit early from a loop
i. Can eliminate the use of certain (flag) variables
b. To skip the remainder of the switch structure
After the break statement executes, the program continues with the first statement after the structure
Continue is used in while, for, and do…while structures. When executed in a loop it skips remaining statements and proceeds with the next iteration of the loop
Example4-7: Write the following code and show the output
13
#include<iostream> //line1using namespace std; //line2void main() //line3{ //line4 int i = 1; //line5 for( ; ; ) //line6 { //line7 cout<<"A"; //line8 if(++i == 5) //line9 break; //line10 cout<<"B"; //line11
} //line12
cout << endl <<endl; //line13 for(int j = 10; j>0; j--) //line14 { //line15 if(j-- == 6) //line16 continue; //line17 cout<< j << " "; //line18
} //line19 cout<< endl; //line20} //line21
C++ Programming Lab Manual LAB4
The output:
Template Assignments for InLab to Practice: Assignment#1
Rewrite example4-2 again that computes the factorial of number with decreasing counter.
Then alter the code by adding (cout) statements to trace the values, like:
while(counter <= number){ cout << counter ;
factorial *= counter; // factorial = factorial * counter; counter += 1; // counter = counter + 1;
cout << counter ; }
14
C++ Programming Lab Manual LAB4
Assignment#2 Design and write a program that prompt the user to input a number and find if this number is a prime number or not by using while loop structure(Note: a number is prime if it is not dividable on any number unless itself and 1)The instructor will provide you with at least 2 assignments to be programmed in the lab
Post LabThe Instructor will provide you with at least two projects to be submitted before next lab.
Prepare for the next lab which is about Select statements. There are some pre lab questions that should be answered before the beginning of the next lab.
Project1 Write a program that prompt the user to insert characters (from A- D) then by using switch block find the number A’s , B’s, C’s, and D’s and how many of other symbols (note: use the while loop)Please Insert your characters ( ! to stop your program): A D G D B A @ D !
15
C++ Programming Lab Manual LAB4
A 2B 1C 0D 3Other 1
Project2 Design a program that generate a random number between 1 and 100 then ask the user to guess this number, by using switch block if the Guessed number is greater than the generated number then print HIGH and give the user another try, if the number is less than the generated number then print LOW and give the user another try until the user find the correct number
(while the Guessed number != the generated random number) Make your guess…
Error sheet Lab4
Name: ------------------------------------------------------------------ Student ID: ---------------------------------------
Section: ------------------------------- Date: --------------------------------------
# OK Type of Error Corrections
1
16
while( ch != ‘!’){switch (this character) to increment the corresponding counter// insert a character}print the result
C++ Programming Lab Manual LAB4
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Represents the line number in the program
17