0% found this document useful (0 votes)
283 views16 pages

CS 102 Lab 4

(1) This document is a lab manual for C++ programming that covers repetition (looping) control structures such as while,
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
283 views16 pages

CS 102 Lab 4

(1) This document is a lab manual for C++ programming that covers repetition (looping) control structures such as while,
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

C++ Programming Lab Manual

LAB4

Lab 4 Control Structure (Repetition)


Objectives
In this lab, you will:

Learn about repetition (looping) control structures with while controlled, flag-controlled, and EOFcontrolled repetition structures

Explore how to construct and use count-controlled, sentinel-

Examine break and continue statements Discover how to use the do-while loop structure

C++ Programming Lab Manual

LAB4

Assignment Cover Sheet Lab 4


Name: -----------------------------------Section: -----------------------------------Student ID: ---------------------------Date: -----------------------------

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

Completed Submitted Grade /10 /5 /5 /5 /5 /5 /5 /5 /10 /10 /10 /10

PreLab
1. What is the difference between while loop and do-while loop
2. What are the types of while loop structure? 2

C++ Programming Lab Manual

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

C++ Programming Lab Manual

LAB4

Lesson 4-4: Flag-Controlled while loops Lesson 4-5: Do-While loop Lesson 4-6: Break and Continue

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:
4

C++ Programming Lab Manual

LAB4

while(expression) statement

Start

expression false End

true

Statement

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) 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

// expression tests the loop control variable(s)

C++ Programming Lab Manual

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.

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 countercontrolled loop.
6

C++ Programming Lab Manual

LAB4

Syntax: counter = value; // initialize the loop control variable while (counter < N) // test the loop control variable { . counter ++; // update the loop control variable }

Example4-2: Write the following program and show the output


#include <iostream> using namespace std; //Program Computing factorials with a while loop int main() { int number, factorial, counter; cout << "Enter a positive integer: "; cin >> number; factorial = 1; counter = 1; while(counter <= number) { factorial *= counter; counter += 1; } cout << "The factorial of " << number << " is " << factorial << 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 see the output. The output:

C++ Programming Lab Manual

LAB4

Example4-3: Write the following code and show the output

#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:

Sentinel-Controlled while loops Lesson 4-3


Sentinel variable is tested in the condition and loop when Sentinel is encountered. cin>> variable; //initialize the loop control variable while(variable != sentinel) // test the loop control variable { 8 cin>> variable; // update the loop control variable } Syntax:

C++ Programming Lab Manual

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:

Flag-Controlled while loops Lesson 4-4


A flag-controlled while loop uses a Boolean variable to control the loop. Syntax:

C++ Programming Lab Manual

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:

Do-While loop Lesson 4-5

10

C++ Programming Lab Manual

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

Fig4-2: The Do-While loop flow chart

Consider the following two loops:


a. i = 11; Example4-6: while(i <= 10) { cout << i << i = i + 5; } cout << endl;

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.

C++ Programming Lab Manual

LAB4

Break and Continue Lesson4-6


Break statement is used for two purposes:
a. i. b.

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

C++ Programming Lab Manual

LAB4

Example4-7: Write the following code and show the output


#include<iostream> using namespace std; void main() { int i = 1; for( ; ; ) { cout<<"A"; if(++i == 5) break; cout<<"B"; } cout << endl <<endl; for(int j = 10; j>0; j--) { if(j-- == 6) continue; cout<< j << " "; } cout<< endl; //line1 //line2 //line3 //line4 //line5 //line6 //line7 //line8 //line9 //line10 //line11 //line12 //line13 //line14 //line15 //line16 //line17 //line18 //line19 //line20 //line21

The output:

Template Assignments for InLab to Practice:


13

C++ Programming Lab Manual

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

C++ Programming Lab Manual

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

Error sheet Lab4


15

C++ Programming Lab Manual

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

Student ID: --------------------------------------Date:


--------------------------------------

Corrections

16

You might also like