Académique Documents
Professionnel Documents
Culture Documents
This makes much easier as we can track our clean up code at one
section in code.ie after Exit label.
b. While
Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
return 0;
}
When the above code is compiled and executed, it produces the following
result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
}
cout<<"Sum = "<<sum;
return 0;
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
7 8
10
d. While True
This is infinite loop. We must change the word in bracket.
while( true ) {
doSomething();
if( condition() ) {
break;
}
doSomethingElse();
}
e. Do / While
for Loop
while Loop
do...while Loop
Output
Enter a positive integer: 4
Factorial of 4 = 24
return 0;
Output
Enter a number: 4.5
Enter a number: 2.34
Enter a number: 5.63
Enter a number: 6.34
Enter a number: 4.53
Enter a number: 5
Enter a number: 0
f. Jump/loop
Jump/loop
Jump statements allow altering the flow of a program by performing jumps to specific locations.
10, 9, 8, 7, 6, 5, 4, 3, countdown
aborted!
10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!
g. If/Else
If/Else Statement
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
Example:
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
return 0;
}