



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Name: _____________________________________________ Roll No: _____________
Score: ____________Signature of the Lab Tutor: _____________ Date: 17/01/ ________________________________________________________________________
Upon successful completion of this experiment, the student will be able to learn: CONTROL STRUCTUERE, WHILE, DO-WHILE AND FOR LOOPS IN C++
A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between each of these is how they control the repetition.
The While Loop: The while loop has two important parts: (1) an expression that is tested for a true or false value, and (2) a statement or block that is repeated as long as the expression is true.
Let’s look at each part of the while loop. The first line, sometimes called the loop header, consists of the key word while followed by a condition to be tested enclosed in parentheses. The condition is expressed by any expression that can be evaluated as true or false. Next comes the body of the loop. This contains one or more C++ statements. Here’s how the loop works. The condition expression is tested, and if it is true, each statement in the body of the loop is executed. Then, the condition is tested again. If it is still true, each statement is executed again. This cycle repeats until the condition is false.
while (condition) { statement; statement; // Place as many statements // here as necessary }
Program Output Hello Hello Hello Hello Hello That's all!
Each execution of a loop is known as an iteration. This loop will perform five iterations before the expression number <= 5 is tested and found to be false, causing the loop to terminate. The program then resumes execution at the statement immediately following the loop. A variable that controls the number of time a loop iterates is referred to as a loop control variable. In the example we have just seen, number is the loop control variable.
While loop can also be used for input validation. For example we know that marks of a subject have to be between 1 and 100. This is how while loop can be used in this case:
cout << "Enter a number in the range 1 - 100: "; cin >> number; while ((number < 1) || (number > 100))
// This program demonstrates a simple while loop.
void main() { int number = 1;
while (number <= 5) { cout << "Hello "; number++; } cout << "\nThat's all!\n"; return 0; }
In essence, the loop counts to twelve and asks the user to enter a sales amount each time it makes a count. A count-controlled loop must possess three elements:
for (initialization; test; update) { statement; statement; // Place as many statements // here as necessary. } As with the other loops you have used, if there is only one statement in the loop body, the braces may be omitted.
Program OutpuT Number Squared
1 1 2 4 3 9 4 16 5 25
Let’s say user entered 5: XXXXX XXXXX XXXXX XXXXX XXXXX
// This program uses a for loop to display the numbers 1- 5 // and their squares. #include
void main() { int num;
cout << "Number Square\n"; cout << "--------------\n";
for (num = 1; num <= 5; num++) cout << num << \t << (num * num) << endl; return 0; }