LAB6 - While Statement
LAB6 - While Statement
College of Engineering
Computer Engineering Department
Experiment No.6
While Statement
Score
Submitted by
Last Name, First Name, MI.
2:00 – 5:00 / WF / CL15
Submitted to
Engr. Kaerius Mendoza
Professor
Date Performed
November 21, 2018
Date Submitted
November 21, 2018
Experiment No. 5
LOOPING while STATEMENT
I. OBJECTIVES
II. DISCUSSION
i = i + 1;
The postfix version will use the old (not incremented) value in the expression, then
once the expression has ended (shown by a semicolon) only then is the value incremented.
The prefix form increments the value straight away, so the new value is used.
So if you set i = 10; and then print out its value, the expression
will print out the value of 10. Once this expression is finished (once the endl; has been
executed) then i is incremented to give the new value of 11. So if you now gave the
instruction:
cout<< i <<endl;
On the other hand, if you used cout<< ++i <<endl; then the first thing that happens is that i
gets incremented, so the value printed out is 11.
Expression Meaning
++i // increment straight away
// increment, but use old value if i++
i++
is part of an existing expression
--i // decrement straight away
// decrement, but use old value if i--
i--
is part of an existing expression
increment/decrement;
}
and its function is simply to repeat statement while the condition is true.
// custom countdown using while statement Enter the starting number >8
#include <iostream.h> 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
int main ()
{
int n;
cout<< "Enter the starting number > ";
cin>> n;
while (n>0)
{
cout<< n << ", ";
--n;
}
cout<< "FIRE!";
return 0;
}
When the program starts the user is prompted to insert a starting number for the
countdown. Then the while loop begins, if the value entered by the user fulfills the condition
n > 0 (that n be greater than 0), the block of instructions that follows will execute an
indefinite number of times while the condition(n > 0) remains true.
Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.
1.
2.
OBSERVATION:
CONCLUSION: