Conditional Statements Lec 2
Conditional Statements Lec 2
• Use the else if statement to specify a new condition if the first condition is
false.
• Syntax
• if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}
Example
• #include <iostream>
• using namespace std;
• int main() {
• int time = 22;
• if (time < 10) {
• cout << "Good morning.";
• } else if (time < 20) {
• cout << "Good day.";
• } else {
• cout << "Good evening.";
• }
• return 0;
• }
• Output: Good evening
Short Hand If...Else (Ternary Operator)
• int main() {
• int time = 20;
• string result = (time < 18) ? "Good day." : "Good evening.";
• cout << result;
• return 0;
• }
• Output: Good evening
C++ Switch Statements
• Use the switch statement to select one of many code blocks to be executed.
• Syntax
• switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
How switch statements works?
• The switch expression is evaluated once
• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
#include <iostream>
using namespace std;
int main() {
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;
}
return 0;
} output: thursday
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.
• Advantage of break statement
• A break can save a lot of execution time because it "ignores" the
execution of all the rest of the code in the switch block.
The default Keyword
• The default keyword specifies some code to run if there is no case match:
• #include <iostream>
• using namespace std;
• int main() {
• int day = 4;
• switch (day) {
• case 6:
• cout << "Today is Saturday";
• break;
• case 7:
• cout << "Today is Sunday";
• break;
• default:
• cout << "Looking forward to the Weekend"; }
• return 0; }
• Output:
• Looking forward to the Weekend
Flowchart of switch Statement
Example: Create a Calculator using the switch Statement
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
Half program…..
• switch (oper) {
• case '+':
• cout << num1 << " + " << num2 << " = " << num1 + num2;
• break;
• case '-':
• cout << num1 << " - " << num2 << " = " << num1 - num2;
• break;
• case '*':
• cout << num1 << " * " << num2 << " = " << num1 * num2;
• break;
• case '/':
• cout << num1 << " / " << num2 << " = " << num1 / num2;
• break;
• default:
• // operator is doesn't match any case constant (+, -, *, /)
• cout << "Error! The operator is not correct";
• break;
• }
• return 0;
if...else if...else Statement
• if(boolean_expression 1) {
• // Executes when the boolean expression 1 is true
• } else if( boolean_expression 2) {
• // Executes when the boolean expression 2 is true
• } else if( boolean_expression 3) {
• // Executes when the boolean expression 3 is true
• } else {
• // executes when the none of the above condition is true.
• }
• #include <iostream>
• using namespace std;
• int main () {
• // local variable declaration:
• int a = 100;
• // check the boolean condition
• if( a == 10 ) {
• // if condition is true then print the following
• cout << "Value of a is 10" << endl;
• } else if( a == 20 ) {
• // if else if condition is true
• cout << "Value of a is 20" << endl;
• } else if( a == 30 ) {
• // if else if condition is true
• cout << "Value of a is 30" << endl; } else {
• // if none of the conditions is true
• cout << "Value of a is not matching" << endl; }
• cout << "Exact value of a is : " << a << endl;
• return 0; }
output
• Value of a is not matching
• Exact value of a is : 100