0% found this document useful (0 votes)
33 views7 pages

IP Manual#10

The document discusses while loops in C++. A while loop repeats a statement or block of code as long as a given condition is true. It has two parts: 1) an expression that is tested for true/false each repetition, and 2) a statement or block that is executed if the expression is true. Several examples are given of while loops to display a name multiple times, take user input until a condition is met, calculate a factorial, display the Fibonacci series, and check if a number is Armstrong.

Uploaded by

Kamran
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)
33 views7 pages

IP Manual#10

The document discusses while loops in C++. A while loop repeats a statement or block of code as long as a given condition is true. It has two parts: 1) an expression that is tested for true/false each repetition, and 2) a statement or block that is executed if the expression is true. Several examples are given of while loops to display a name multiple times, take user input until a condition is met, calculate a factorial, display the Fibonacci series, and check if a number is Armstrong.

Uploaded by

Kamran
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

UET Taxila Introduction to Programming Electrical Engineering Dept

Objective
Make use of
While loop
The loop is part of a program that repeats.
The for loop does something a fixed number of times. What happens if you
dont know how many times you want to do something before you start the
loop? In this case a different kind of loop may be used: 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.

Here is the general format of the while loop:

In the general format, expression in any that can be evaluated as true and
false, and statement is any valid C++ statement. The first line shown is the
format is sometimes called the loop header. It consists of the key word
while followed by an expression in parentheses.

Heres how the loop works, the expression is tested, and if it is true, the
statement is executed. Then, the expression is tested again. If it is true, the
statement is executed. This cycle repeats until the expression is false.

45
UET Taxila Introduction to Programming Electrical Engineering Dept

Notice there is no semicolons after the expression in parentheses. Like the


is statement, the while loop is not complete without the statement that
follows it:

If you wish the while loop to repeat a block of statement, its format is:

Flow chart of while Loop

46
UET Taxila Introduction to Programming Electrical Engineering Dept

Program No.31
Write a program in C++ to display your name 5 times on output screen,
using while loop.
Output of Program No.31

47
UET Taxila Introduction to Programming Electrical Engineering Dept

Program No.32
write a program in C++ that take the integer type values for user and the
program terminates when the user enter 0, using while loop.
Output of Program No.32

48
UET Taxila Introduction to Programming Electrical Engineering Dept

Program No.33
Write a program in C++ to find the factorial of number using while loop.
The number must be user defined.

Output of Program No.33

49
UET Taxila Introduction to Programming Electrical Engineering Dept

Program No.34
Write a Program in C++ that shows the Fibonacci series using while loop

Hint: Fibonacci series 0 1 1 2 3 5 8 13 ..

Output of Program No.34

50
UET Taxila Introduction to Programming Electrical Engineering Dept

Program No.35
Write a Program in C++ that check the user defined number is Armstrong
or not.
Hint: 153 is an Armstrong number
+ + =153

Output of Program No.35

51

You might also like