Conditional Statements in C++
by - Yash Ukalkar |
Introduction -
Condition evaluation is an important part of programming regardless of the language and lies at the core of solutions to decision making problems. Condition evaluation and conditional statements help us to perform certain actions in case a certain criteria is met, and some other actions otherwise. This makes it easier for a programmer to make the program versatile and have a solution for various scenarios it faces.
Conditions -
Conditions are simple statements or expressions whose value, when evaluated, comes out to be either true or false. To get these boolean values(true/false) from any given expression, we use the comparison operators(<,==,!,....) provided by the programming language.
Ex:
Output:
here, 0 signifies a false value and 1 signifies a true value.
In the third example, we compared two characters. In such cases, the ASCII values for the characters are used to compare the characters. Since ASCII value of 'a' is 97 and that of 'c' is 99, we can see that 97 is not equal to 99, hence we get the output value as true.
Similar is the case if we want to compare the values of strings, the ASCII value of every character is compared in order(left to right) till the first mismatch of characters is found and the string with greater ASCII value of mismatched character has the greater value. In case no mismatch is found, the strings compared are equal.
Ex:
Conditional statements -
'if' statement
The if conditional statement is used to execute a block of code only when the provided condition is evaluated as true.
Syntax:
here,
- if is a keyword that signifies an 'if' conditional block
- condition is the expression to be evaluated and is written inside parentheses() after the if keyword
- code is the block of code written inside curly braces{} that is to be executed in case condition is evaluated to be true
'else' statement
The else statement is used after an if statement, and the block of code inside else statement is executed in case the condition given for the if statement is evaluated as false.
Syntax:
here, condition is evaluated first; if it is evaluated as true, code1 under the if statement is executed; if it is evaluated as false, code2 under the else block is executed.
Ex: program to check if entered number is even or odd
Output:
'else if' statement
The else if statement is used to include additional conditions to be checked after the if statement in case one wants to execute different blocks of codes based on different criteria.
Syntax:
here, condition1 is evaluated first; if it is true, code1 is executed; if it is false, condition2 is evaluated; if condition2 is true, code2 is executed; if condition2 is false, code3 is executed.
Ex:
Output:
Nested conditional statements -
Nesting refers to the process where one if or else or else if block contains another if or else or else if block.
Ex: finding the greatest of three input numbers
Output:
Short-hand if-else statement -
C++ provides us with a ternary operator that acts as a single line if-else statement and shortens the code.
Syntax:
here, condition is evaluated; if it is true then val1 is stored in variable, otherwise val2 is stored in variable.
Ex:
Output:
Short-circuit evaluation -
Short-circuit evaluation is one of the optimization processes in the C++ compiler that helps us reduce unnecessary logical comparisons. This takes place where many &&(logical AND) or ||(logical OR) operators are present.
In case && is used in the expression, the evaluation stops and returns a false value when the first false value is encountered because an expression with logical AND gives a true value only when all the conditions involved are true.
Ex: cond1&&cond2&&cond3
here, if cond2 is false, cond3 is not checked and a false value is returned.
In case || is used in the expression, the evaluation stops and returns a true value when the first true value is encountered because an expression with logical OR gives a true value whenever any one of the conditions involved are true.
Ex: cond1||cond2||cond3
here, if cond1 is true, cond2 and cond3 is not checked and a true value is returned.
Comments
Post a Comment