Conditional statements in c++

In computer programming, we make decisions based on different conditions to write programs. We either execute a given set of instructions when a certain condition is met or omit some instruction if the condition is false. These statements have boolean conditions that are either evaluated as true or false.C++ has a variety of methods to do the decision making. In this post, we will discuss different conditional statements in c++ with different code examples.


Conditional statements in c++
Conditional statements in c++

Conditions

C++ has simple conditions that allow us to make decisions and pursue our logic. These simple conditions need two operands and a comparison operator. These conditions are boolean expressions that are either true or false. The working of six comparison operators is as follows.

Comparison operators and their use in decision making

==
It is evaluated  true if left and right operands are equal
!=
It is evaluated  true if left and right operands are not equal
It is evaluated  true if the left operand is less than the right operand
It is evaluated  true if the left operand is greater than the right operand
<=
It is evaluated  true if the left operand is less than or equal to the right operand
>=
It is evaluated  true if the left operand is greater than or equal to the right operand


If statement c++

if statement is a statement that executes its block of code only if its condition is satisfied. Its block is ignored if its condition is not true and the program starts the execution of the instructions following this block.


if (a>10) {//either true of false
cout<<"value of a is greater than 10";
//this code will run when condition is true
}

If else statement c++

It is a statement with a boolean condition that is either evaluated as true or false. When the condition is true then the set of instructions following if statement is executed. If the condition is false then all the instructions following if statement is omitted and the control of the program goes to else. Then the program executes all the instructions inside the braces of else. 


if (a>10) {//either true of false
cout<<"value of a is greater than 10";
//this code will run when condition is true
}
else{
cout<<"value of a is not greater than 10";
//this code will run when condition is not true
}

If else if statement in c++

In certain circumstances, we need to check more than one condition in a code. If else if statement is used in such circumstances. In this method of decision making, we provide multiple options or restrictions using else if statements. Else if statement also has a boolean condition that is either evaluated as true or false. Unlike if statement, else if statement can be used multiple times to check multiple conditions in a given scenario. In c++, if-else-if statements are executed from top to bottom.


if (a>10) {//either true of false
cout<<"value of a is greater than 10";
//this code will run when condition is true
}
else if(a>20){//either true of false
cout<<"value of a is greater than 20";
//this code will run when else if condition is true
}
else if(a>30){//either true of false
cout<<"value of a is greater than 30";
//this code will run when else if condition is true
}
else if(a>40){//either true of false
cout<<"value of a is greater than 40";
//this code will run when else if condition is true
}
else{
cout<<"value of a is not greater than 10";
//this code will run when all conditions are not false
}


Nested if else in c++

When we write if-else statement inside the braces of a parent if or else statement than it is called nested if-else in c++. Nested if-else means writing if statement inside the braces of a parent if or else statement.  It is used to apply some minor or less important conditions when a major condition is satisfied.


if (a>0) {// parent if else statement with outer major(most important) condition

 
 if (a%2==0) {//child if else statement with inner minor(less important) condition
  cout << "The number positive even number" << endl;
 }
 else {
  cout << "The number positive odd number" << endl; }
} 
else {
 cout << "the number is negative" << endl;
}



The concept of nested if-else can be better understood through grade calculating program.

Switch Statement in c++

We can also use the switch statements for decision making instead of if-else-if(ladder) statements in some cases. In the switch statement, the switch expression(with a variable being switched) is executed and evaluated first. then the value of the switch expression /variable is matched with the values of switch cases. If the value matches then the associated switch case is executed. The break statement should be used at the end of the case's block to avoid the execution of other cases. The default case runs when the expression doesn't equal any case and it's optional. 


switch (num) {// switch expression
case 1: //switch case 
 cout << "english language" << endl;
 break;
case 2: {//switch case
 cout << "computer studies" << endl;
 cout << "seats full";
 break;
 }
case 3://switch case 
 cout << "Medicine" << endl;
 break;
case 4://switch case 
 cout << "Economics" << endl;
 break;
default://runs when there is no match
 cout << "Invalid course code" << endl;
}


Ternary operator in c++

Instead of writing an if-else statement, we can also use a ternary operator. It is a conditional operator that operates on three operands. It is used for decision-making and it also diminishes your lines of code. Ternary operator has many benefits over simple if-else statements but it cannot execute multiple instructions when the condition is met.  


int main() {
 int num = 3;
 string ans = (num % 2 == 0) ? "Even." : "Odd.";
 cout << ans;
 return 0;
}



Compound conditional statements and logical operators

We can write compound conditional expressions by using the logical operators AND, OR, and NOT in c++. Compound conditional statements can be used to replace many lines nested if-else statements with few lines. We can use multiple boolean conditions through a logical operator to write a compound conditional expression using if statement. let's see how it happens.

Logical operator AND:
&& is the symbol of AND operator. The braces of compound if statement is only executed if all the conditions are true. If any of the conditions is false then the block of code is not executed.

Logical operator OR:
|| is the symbol of OR operator. The block of code is executed if one of the conditions is true. If both conditions are false then the braces of the compound if statement will not run.

Logical operator NOT:
! is the symbol of NOT operator. The block is only executed if the condition is false.

void main() {
 int a = 15;
 if (a > 10 && a < 100) { //AND operator used
  cout << "a is greater than 10 and less than 100";
 }
 else if (a > 10 || a < 100) { //OR operator used
  cout << "a is either greater than 10 or less than 100 or both";
 }
 else if (!(a > 10 && a < 100)) {//NOT operator used
  cout << "a is either less than 10 or greater than 100";
 }
 else {
  cout << "All conditions are false";
 }
}


Practice programs:
These programs are written to compare different methods of decision making to find which method is good and why? Obviously, every method has pros and cons. You can understand when to use which method through these practice program.
Find the largest value between three variables
c++ program to check whether a character is vowel or consonant

Post a Comment

0 Comments