Conditions and Loops
Conditions and Loops
and Loops
C++ Conditions
▪ C++ supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
▪ You can use these conditions to perform different actions for different
decisions.
C++ Conditions…
C++ Conditions…
▪ int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
C++ Switch Statements…
▪ The break Keyword
▪ When C++ reaches a break keyword, it breaks out of the switch block.
▪ This will stop the execution of more code and case testing inside the
block.
▪ When a match is found, and the job is done, it's time for a break.
There is no need for more testing.
▪ Syntax
switch(ch1) {
case 'A':
cout << "This A is part of outer switch";
switch(ch2) {
case 'A':
cout << "This A is part of inner switch";
break;
case 'B': // ...
}
break;
case 'B': // ...
}
C++ nested switch statements…
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
int a = 100;
int b = 200;
switch(a) {
case 100:
cout << "This is part of outer switch" << endl;
switch(b) {
case 200:
cout << "This is part of inner switch" << endl;
}
}
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;
return 0;
}
C++ Loops
▪ Loops can execute a block of code as long as a specified
condition is reached.
▪ Loops are handy because they save time, reduce errors,
and they make code more readable.
▪ C++ has the following loops:
• Use while loop when you what to loop through a block of
code as long as a specified condition is true.
• Use the for loop when you know exactly how many
times you want to loop through a block of code.
C++ Loops…
C++ While Loop
▪ The while loop loops through a block of code as long as a
specified condition is true:
▪ Syntax
▪ while (condition) {
// code block to be executed
}
C++ While Loop…
▪ In the example below, the code in the loop will run, over
and over again, as long as a variable (i) is less than 5:
▪ int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
▪ Note: Do not forget to increase the variable used in the
condition, otherwise the loop will never end!
C++ While Loop - The Do/While Loop
▪ The do/while loop is a variant of the while loop. This loop
will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the
condition is true.
▪ Syntax
▪ do {
// code block to be executed
}
while (condition);
C++ While Loop - The Do/While Loop…
▪ The example below uses a do/while loop. The loop will always be
executed at least once, even if the condition is false, because the code
block is executed before the condition is tested:
▪ int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
while(condition) {
while(condition) {
statement(s);
}
statement(s); // you can put more statements.
}
C++ nested loops…
▪ The syntax for a nested do...while loop statement in
C++ is as follows:
do {
statement(s); // you can put more statements.
do {
statement(s);
} while( condition );
} while( condition );
C++ nested loops…
#include <iostream>
using namespace std;
int main () {
int i, j;
return 0;
}