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:

#include<iostream>
using namespace std;
int main(){
    cout<<(5>3)<<endl;
    cout<<(20<=18)<<endl;
    cout<<('a'!='c')<<endl;
    return 0;
} 

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:

#include<iostream>
using namespace std;
int main(){
    cout<<("Hello">"HellO")<<endl;
    cout<<("Hello"=="Hello")<<endl;
    return 0;
}

Output:





Now that we know how expressions are evaluated, we can start to learn about conditional statements in C++ that will help us to execute certain code only under certain situations.

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:

if (/* condition */)
    {
        /* code */
    }

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
Ex:

#include<iostream>
using namespace std;
int main(){
    int number;
    cout<<"Enter number:";
    cin>>number;
    if(number==7){
        cout<<"Lucky number "<<number<<endl;
    }
}

Output:










'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:

if (/* condition */)
    {
        /* code1 */
    }
    else
    {
        /* code2 */
    }

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

#include<iostream>
using namespace std;
int main(){
    int number;
    cout<<"Enter number:";
    cin>>number;
    if (number%2==0)
    {
        cout<<"Entered number is even.\n";
    }
    else
    {
        cout<<"Entered number is odd.\n";
    }
    
}

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:

if (/* condition1 */)
    {
        /* code1 */
    }
    else if (/* condition2 */)
    {
        /* code2 */
    }
    else
    {
        /* code3 */
    }

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:

#include<iostream>
using namespace std;
int main(){
    int number;
    cout<<"Enter number:";
    cin>>number;
    if (number<0)
    {
        cout<<"Number entered is negative\n";
    }
    else if (number>0)
    {
        cout<<"Number entered is positive\n";
    }
    else
    {
        cout<<"You entered 0\n";
    }
    
}

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.

if (/* condition */)
    {
        if (/* condition */)
        {
            /* code */
        }
        else
        {
            /* code */
        }
        
    }
    else if (/* condition */)
    {
        /* code */
    }
    else
    {
        /* code */
    }

Ex: finding the greatest of three input numbers

#include<iostream>
using namespace std;
int main(){
    int num1,num2,num3;
    cout<<"Enter the numbers:";
    cin>>num1>>num2>>num3;
    if(num1==num2 && num2==num3){
        cout<<"All are equal.\n";
    }
    else if (num1>=num2){
        if(num1>num3){
            cout<<num1<<" is greatest.\n";
        }
        else{
            cout<<num3<<" is greatest.\n";
        }
    }
    else if(num1<num2){
        if(num2>num3){
            cout<<num2<<" is greatest.\n";
        }
        else{
            cout<<num3<<" is greatest.\n";
        }
    }   
}

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:

variable = (condition)?val1:val2;

here, condition is evaluated; if it is true then val1 is stored in variable, otherwise val2 is stored in variable.

Ex:

#include<iostream>
using namespace std;
int main(){
    char c1,c2;
    cout<<"Enter two letters:";
    cin>>c1>>c2;
    char answer;
    answer=(c1>c2)?c1:c2;
    cout<<answer<<" has greater ASCII value.\n";
}

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 truecond2 and cond3 is not checked and a true value is returned.


That was about conditions in C++ and how decisions can be made using 'if', 'if-else', 'if else-if else' statements. To keep learning, click here.

THANK YOU!

Comments

Popular posts from this blog

What Is Functions In C++ ? || Explained By Examples || Function Types

Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||

Reference and Pointers in C++