0% found this document useful (0 votes)
27 views

While and Do While Loop

While and do-while loops in C++ allow programmers to repeat a block of code. The while loop evaluates a condition and repeats the code block if true, until the condition becomes false. The do-while loop executes the code block first before evaluating the condition, repeating until false. Examples demonstrate using loops to print numbers, calculate sums and averages, and get input from the user to perform calculations. Loops improve efficiency over writing repeated code and are a fundamental part of programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

While and Do While Loop

While and do-while loops in C++ allow programmers to repeat a block of code. The while loop evaluates a condition and repeats the code block if true, until the condition becomes false. The do-while loop executes the code block first before evaluating the condition, repeating until false. Examples demonstrate using loops to print numbers, calculate sums and averages, and get input from the user to perform calculations. Loops improve efficiency over writing repeated code and are a fundamental part of programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

WHILE AND DO…WHILE LOOP

Loops in C++

PREPARED BY:
DR.NOURHAN MOHSEN
DR.REHAM NASSER
While and do….while loops
➢ In computer programming, loops are used to repeat a block of code.
For example, let's say we want to show a message 100 times. Then instead of writing the print
statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our
programs by making effective use of loops.

➢ C++ while Loop


The syntax of the while loop is:

while (condition) {
// body of the loop
}

➢ How it works??

1. A while loop evaluates the condition


2. If the condition evaluates to true, the code inside the while loop is executed.
3. The condition is evaluated again.
4. This process continues until the condition is false.
5. When the condition evaluates to false, the loop terminates.

➢ Example 1: Display Numbers from 1 to 5


// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main()’
{
int i = 1;
// while loop from 1 to 5
while (i <= 5)
{
cout << i << " ";
++i;
}
return 0;}
➢ Here is how the program works.

Iteration Variable i <= 5 Action


1st i=1 true 1 is printed and i is increased to 2.
2nd i=2 true 2 is printed and i is increased to 3.
3rd i=3 true 3 is printed and i is increased to 4
4th i=4 true 4 is printed and i is increased to 5.
5th i=5 true 5 is printed and i is increased to 6.
6th i=6 false The loop is terminated

➢ Example 2: Sum of Positive Numbers Only

// program to find the sum of positive numbers


// if the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
while (number >= 0)
{
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum is " << sum << endl;

return 0;
}
• In this program, the user is prompted to enter a number, which is stored in the
variable number.
• In order to store the sum of the numbers, we declare a variable sum and initialize it to
the value of 0.
• The while loop continues until the user enters a negative number. During each iteration,
the number entered by the user is added to the sum variable.
• When the user enters a negative number, the loop terminates. Finally, the total sum is
displayed.

➢ Example 3
// program that takes marks of 10 students as input. It calculates the class average and displays
it on the screen Using while loop.

#include <iostream>
using namespace std;
int main()
{
int marks, sum = 0, count = 0;
double avg;
while (count < 10)
{
cout << "Enter marks for student " << count + 1 << ": ";
cin >> marks;
sum += marks;
count++;
}
avg = double(sum) / count;
cout << "Class average is: " << avg << endl;
return 0;
}
➢ How it works??
1. We first declare variables for marks, sum, count, and avg, where sum and count are
initialized to 0.
2. We use a while loop to take input marks for 10 students. Inside the loop, we prompt
the user to enter marks for each student and add it to the sum variable. We also
increment count by 1.
3. After the loop, we calculate the class average by dividing the sum by the count
(converted to a double to obtain a decimal value).
4. Finally, we display the class average on the screen using the cout statement.
➢ C++ do...while Loop
The do...while loop is a variant of the while loop with one important difference: the body
of do...while loop is executed once before the condition is checked.
Its syntax is:

do {
// body of loop;
}
while (condition);

➢ how it works??
1. The body of the loop is executed at first. Then the condition is evaluated.
2. If the condition evaluates to true, the body of the loop inside the do statement is
executed again.
3. The condition is evaluated once again.
4. If the condition evaluates to true, the body of the loop inside the do statement is
executed again.
5. This process continues until the condition evaluates to false. Then the loop stops.

➢ Example 1: Display Numbers from 1 to 5


// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// do...while loop from 1 to 5
do {
cout << i << " ";
++i;
}
while (i <= 5);

return 0;
}

➢ Here is how the program works.


Iteration Variable i <= 5 Action
i=1 not checked 1 is printed and i is increased to 2

1st i=2 true 2 is printed and i is increased to 3

2nd i=3 true 3 is printed and i is increased to 4

3rd i=4 true 4 is printed and i is increased to 5

4th i=5 true 5 is printed and i is increased to 6

5th i=6 false The loop is terminated

➢ Example 2: Sum of Positive Numbers Only


// program to find the sum of positive numbers
// If the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
➢ Example 3 :
// program for Calculating the product of two numbers entered by the user using a do-while
loop
#include <iostream>
using namespace std;
int main()
{
int num1, num2, product;
char choice;
do {
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
product = num1 * num2;
cout << "Product of " << num1 << " and " << num2 << " is " << product << endl;
cout << "\nDo you want to calculate another product? (y/n): ";
cin >> choice;
}
while(choice == 'y' || choice == 'y');
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy