0% found this document useful (0 votes)
78 views6 pages

LAB6 - While Statement

This document contains an experiment report on using while loops in C++ programs. It discusses increment and decrement operators, the format of a while loop, and the break and continue instructions. The objectives are to use different looping statements and differentiate their applications. The procedures section instructs the student to encode 3 programs demonstrating while loops, capture the outputs, and discuss their observations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views6 pages

LAB6 - While Statement

This document contains an experiment report on using while loops in C++ programs. It discusses increment and decrement operators, the format of a while loop, and the break and continue instructions. The objectives are to use different looping statements and differentiate their applications. The procedures section instructs the student to encode 3 programs demonstrating while loops, capture the outputs, and discuss their observations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Adamson University

College of Engineering
Computer Engineering Department

Computer Programming and Fundamentals

Experiment No.6
While Statement

Score

Exceeds Meets Needs


CRITERIA Unsatisfactory
Expectations Expectations Improvement
Completeness
20 15 10 5
Accuracy of
20 15 10 5
Results
Observation /
20 15 10 5
Conclusion
Spelling and
20 15 10 5
Grammar
Presentation and
20 15 10 5
Format

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

1. To employ different looping while statement in C++ programs.


2. To differentiate and use looping while statement in different applications.

II. DISCUSSION

Increment and Decrement Operators


Increment and decrement operators are special operators which are unique to C and
C++. The effect of these is to increase or decrease the value of the variable by 1. The
increment operator is denoted by ++ (now you can guess why the updated version of C was
called C++)and the decrement operator by --.

They can be used simply as in

i++; (the postfix form)


or
++i; (the prefix form)

both of which are equivalent to

i = i + 1;

or they can be part of an expression.

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

cout<< i++ <<endl;

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.

Computer Fundamentals and Programming I Page 2


Increment and Decrement Operators

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

The while loop


Its format is:
initialization;
while (condition)
{
statements;

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.

Computer Fundamentals and Programming I Page 3


The break instruction
Using break we can leave a loop even if the condition for its end is not fulfilled. It can
be used to end an infinite loop, or to force it to end before its natural end. For example, we
are going to stop the count down before it naturally finishes:
// break loop example 10, 9, 8, 7, 6, 5, 4, 3, countdown
#include <iostream.h> aborted!
#include <conio.h>
int main ()
{
clrscr();
int n;
n = 10;
while (n > 0)
{
cout<< n << ", ";
if (n==3)
{
cout<< "countdown aborted!";
break;
}
n--;
}
getch();
return 0;
}
The continue instruction
The continue instruction causes the program to skip the rest of the loop in the present
iteration as if the end of the statement block would have been reached, causing it to jump to
the following iteration. For example, we are going to skip the number 5 in our countdown:
// continue loop example 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
#include <iostream.h>
#include <conio.h>
int main ()
{
clrscr();
int n = 10;
while (n > 0)
{
if(n == 5)
continue;
cout<<n<<”, “;
}
cout<< "FIRE!";
getch();
return 0;
}

Computer Fundamentals and Programming I Page 4


III. Procedures:
1. Encode the C++ program below.
2. Save as the file name indicated.
3. Compile and run the program by pressing Ctrl + F9.
4. Capture the output of the program by using snipping / print screen tool.
5. Discuss the output of the program.
6. Upload your work in E-Learning, filename: LASTNAME.DOC. (Title page and
Output/Analysis pages only).
7. Do not turn off your terminal. Programs encoded will be checked by your instructor.
Run all programs at one time for faster checking.

Computer Fundamentals and Programming I Page 5


Laboratory Exercise 5
LOOPING while STATEMENT

Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.

PROGRAM OUTPUT ANALYSIS / OBSERVATION

1.

2.

OBSERVATION:

CONCLUSION:

Computer Fundamentals and Programming I Page 6

You might also like