CS 102 Lab 4
CS 102 Lab 4
LAB4
Learn about repetition (looping) control structures with while controlled, flag-controlled, and EOFcontrolled repetition structures
Examine break and continue statements Discover how to use the do-while loop structure
LAB4
Activities Assigned: check or list exercise numbers PreLab The Questions of page 5 Inlab Example4-1 Example4-2 Example4-3 Example4-4 Example4-5 Example4-6 Example4-7 Assignmnet1 Assignmnet2 PostLab Project1 Project2
PreLab
1. What is the difference between while loop and do-while loop
2. What are the types of while loop structure? 2
LAB4
3. Write a program that prompt the user to insert a number and prints stars as many as the
inserted number
Outline:
Lesson 4-1: While repetition statements Lesson 4-2: Counter-Controlled while loops Lesson 4-3: Sentinel-Controlled while loops
3
LAB4
Lesson 4-4: Flag-Controlled while loops Lesson 4-5: Do-While loop Lesson 4-6: Break and Continue
LAB4
while(expression) statement
Start
true
Statement
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) while(expression) { Statement; //update the loop control variable(s) } Example4-1: write this program which uses the while loop and show the output #include <iostream> //line1
using namespace std; int main() { int i = 0; while (i <= 20) { cout << i << " "; i = i + 5; } cout << endl; return 0; } //line2 //line3 //line4 //line5 //line6 //line7 //line8 //line9 //line10 //line11 //line12 //line13
LAB4
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.
LAB4
Syntax: counter = value; // initialize the loop control variable while (counter < N) // test the loop control variable { . counter ++; // update the loop control variable }
Run the program with different values and see the output. The output:
LAB4
#include <iostream> #include <cstdlib> using namespace std; void main() { int count = 1; while(count <= 10) { cout << count++ <<") " <<rand()%100 <<endl; } }
//line1 //line2 //line3 //line4 //line5 //line6 //line7 //line8 //line9 //line10
The output:
LAB4
Example 4-4: Write the following code and how the output
#include "stdafx.h" #include <iostream> using namespace std; /*Program finding maximum value using while loop and if statement.*/ int main() { int value=0; //input value int max=0; //maximum value while(value!=-1){ cout << "Enter a positive integer: " << "(-1 to stop):"; cin >> value; if(value > max) max = value; } cout << "The maximum value found is: " << max << endl; return 0; //line1 //line2 //line3 //line4 //line5 //line6 //line7 //line8 //line9 //line10 //line11 //line12 //line13 //line14 //line15 //line16 //line17
Run the program with different values, and trace the values by adding (cout) statements. The output:
LAB4
found = false; //initialize the loop control variable while(!found) // test the loop control variable { if(expression) found = true; // update the loop control variable } Example4-5: Write the following program and show the output
#include <iostream> using namespace std; void main() { int num=7; bool b=false; while(b != true) //b==false { cout<<"*"; num=num+3; if(num > 20) b=true; } } //line1 //line2 //line3 //line4 //line5 //line6 //line10 //line12 //line13 //line14 //line15 //line16 //line17
The output:
10
LAB4
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: do statement while(expression);
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.
Start
Statement
expression
true
false End
b. i = 11; do { cout << i << i = i + 5; } while(i <= 10); 11 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.
LAB4
To exit early from a loop Can eliminate the use of certain (flag) variables 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 dowhile structures. When executed in a loop it skips remaining statements and proceeds with the next iteration of the loop
12
LAB4
The output:
LAB4
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 ; }
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 Lab
14
LAB4
The 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 As , Bs, Cs, and Ds 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 ! A B C D Other 2 1 0 3 1
while( ch != !) { switch (this character) to increment the corresponding counter // insert a character } print the result
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
LAB4
Name: -----------------------------------------------------------------Section: ------------------------------# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # Represents the line number in the program OK Type of Error
Corrections
16