In this lesson, we would learn how to use use conditional statement in C++. Conditional statements allow you to alter the direction of the program flow based n certain conditions.
We’l cover the following:
- if statement
- if..else statement
- nested if statements
- switch statement
- nested switch statement
- The Conditional Operator(?:)
1. if statement
This consists of a single expression followed by one or more statements. If the expression evaluates to true, then the statement body is executed. Otherwise, the statement body is skipped. The statement body is enclosed in curly braces.
An example is given below:
#include <iostream> using namespace std; int main () { // local variable : int x = 20; // check if the expression is true if( x < 100 ) { // the statement body cout << "x is less than 20 \n"; } cout << "value of x is : " << x; return 0; }
Output
a is less than 20; value of a is : 10
2. if..else statement
This is an if statement followed by an else part. The else part is executed if the conditional expression is false.
An example is given below:
int main () { int age; cout << "How old are you? "; cin >> age; if(age > 16) { cout << "You are an adult!"; } else { cout << "You are not yet an adult! "; } cout << "Because you are " << age << " years old"; return 0; }
3. nested if statements
In this case, we use on if..else statement inside another on. It will be best understood with an example. The previous example is modified to include a nested if statement
int main () { int age; cout << "How old are you? "; cin >> age; if(age > 16) { cout << "You are an adult! /n"; // Nested if statement if(age < 18){ cout << "But you cannot enlist!" } } else { cout << "You are not yet an adult! "; } cout << "Because you are " << age << " years"; return 0; }
4. switch statement
The switch statement enables you to test a particular variable against a list of values. When the variable matches any of the items in the list, a particular statement or statements are executed.
The syntax for the switch statement is given below:
switch(expression) { case expression_1 : statements; break; case expression_2 : statements; break; ... ... case expression_n : statements; break; default : //Optional statements; }
A typical example would be to display a gradesheet where students are assigned a grade based on their score in an exam.
int main () { char grade; cout << "Please enter your grade: "; //Read the grade value from user input cin >> grade; switch(grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : cout << "Very Good!" <<endl; case 'C' : cout << "Good" << endl; break; case 'D' : cout << "Pass" << endl; break; case 'F' : cout << "Fail" << endl; break; default : cout << "Invalid grade" << endl; } cout << "Your grade: " << grade << endl; return 0; }
5. nested switch statement
You can also have a switch statement nested within another switch statement. This works the same way as nested if statement but in this case, it’s switch statement.
switch(char1) { case 'C': cout << "This C is part of outer switch"; switch(char2) { case 'C': cout << "This C is part of inner switch"; break; case 'D': // ... } break; case 'B': // ... }
6. The Conditional Operator (? 🙂
We touched on the conditional operator previously in the C++ Operators lesson. This operator is also called ‘ternary operator‘. It takes three operands like so:
Expr1 ? Expr2 : Expr3;
How it works:
- the first expression is evaluation and results in a boolean(true or false)
- if true, then evaluation the second expression (Expr2)
- if false, then evaluate the third expression (Expr3)
The result of the conditional operation is the result of whichever expression is evaluated between Expr2 and Expr3
An example is given below:
int main () { int x = 10; int y = 20; int z; z = (x < y) ? 10 : 40; cout << "value of x: " << z << endl; return 0; }
Output
value of z: 10
Here, z is assigned the value 10. This is because when the expression (x < y) is evaluated, the result is true. So the conditional operation returns 10 which is assigned to z
One thought on “C++ Conditional Statements”