0% found this document useful (0 votes)
30 views19 pages

Conditional Statements Lec 2

The document discusses different conditional statements in C++ including if-else statements, else if statements, ternary operators, switch statements, and if-else if-else statements. It provides syntax and examples for each statement to demonstrate their usage and explains concepts like break and default keywords in switch statements.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views19 pages

Conditional Statements Lec 2

The document discusses different conditional statements in C++ including if-else statements, else if statements, ternary operators, switch statements, and if-else if-else statements. It provides syntax and examples for each statement to demonstrate their usage and explains concepts like break and default keywords in switch statements.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Conditional statements lec 2

The else if Statement

• 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)

• There is also a short-hand if else, which is known as the ternary


operator because it consists of three operands. It can be used to
replace multiple lines of code with a single line. It is often used to
replace simple if else statements:
• Syntax
• variable = (condition) ? expressionTrue : expressionFalse;
Example
• #include <iostream>
• #include <string>
• using namespace std;

• 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

• An if statement can be followed by an optional else


if...else statement, which is very usefull to test various conditions
using single if...else if statement.
• When using if , else if , else statements there are few points to keep in
mind.
• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the
else.
• Once an else if succeeds, none of he remaining else if's or else's will
be tested.
Syntax

• The syntax of an if...else if...else statement in C++ is −

• 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

You might also like