ACLC College Computer Programming1 C SESSION10
ACLC College Computer Programming1 C SESSION10
(C++)
Gliceria S. Tan
Instructor
Session 10 Topics
Syntax:
while(condition){
statement1;
statement2;
...
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Example:
int x = 0;
while (x<10){
cout<<x;
x++;
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The do-while statement
Syntax:
do{
statement1;
statement2;
...
}while(condition);
int x = 0;
do{
cout<<x;
x++;
}while (x<10);
Syntax:
for(initialization; condition; inc/dec){
statement1;
statement2;
...
}
Example:
int i;
for(i = 1; i<=10; i++){
cout<<i;
}
The break statement
Causes an immediate exit from any loop. For
example,
int i = 4;
while ( i > 0 ){
cout<<i;
i--;
if(i==1)
break;
}
This causes the loop to exit (or terminate)
when i is 1. As a result, the loop only outputs
the numbers 432.
The continue statement
Used to skip an iteration (ignores executing the rest of
the loop). For example:
int i = 4;
while ( i > 0 ){
if(i==3){
i--;
continue;
}
cout<<i;
i--;
}
The output of the program is 421.
The Sentinel
What is a sentinel?
Sample output:
Enter a number: 1
POSITIVE
Enter a number: -3
NEGATIVE
Enter a number: 0
Bye!
In this flowchart, you START
do not know how
many numbers will be
entered. In fact, the INPUT n
number of values
entered could be n>0
T OUTPUT
different each time “POSITIVE”
F
the flowchart is
executed. n<0
T OUTPUT
“NEGATIVE”
By using a sentinel F
T OUTPUT
Enter a number: 1 n<0
“NEGATIVE”
POSITIVE
F
Enter a number: -1
NEGATIVE T
n == 0 STOP
Enter a number: 0
Bye! F
“QUOTE OF THE DAY!”