IP Manual#10
IP Manual#10
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.
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
If you wish the while loop to repeat a block of statement, its format is:
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.
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
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
51