If and Switch Statement in C++(Selection Construct)(part-1)

If and Switch Statement in C++ (Selection Construct) The selection statements allow to choose set of instructions to be executed depending on a condition test. If the condition evaluates to true, then a set of statement(s) are executed, and if the condition evaluates to false, then another set of statement(s) may or may not be executed. The selection statements are also called conditional statements or  decision statements.  The if and switch statements in C++ are the two types of selection statements.

The if-else statement of C++

1) Syntax of if:
if (expression)                            //if statement without else
   {
    statement(s);
   } 
This is the syntax of if statement. The expression must be closed in parenthesis. The expression is actually a condition test, which tells whether the block of statement(s) will be executed or not. If the condition or the expression evaluates to true, then the block of statement in { .. } will executed, otherwise not. Example of C++ program –
#include<iostream.h>
int main()
    {
    int a,b,c;
    cout<<"Enter the value of a =";
    cin>>a;
    cout<<"\nEnter the value of b =";
    cin>>b;
    cout<<"\nEnter the value of c =";
    cin>>c;
    if(a>=b && a>=c)
       cout<<"\nLargest number = "<<a;
    if(b>=a && b>=c)
       cout<<"\nLargest number = "<<b;
    if(c>=a && c>=b)
       cout<<"\nLargest number = "<<c;
    return 0;
    }
Output :
Enter the value of a =20
Enter the value of b =50
Enter the value of c =30
Largest number = 50
2)Syntax of if-else :
if (expression)                            //if statement with else
   {
    statement-1;
   }
else
   {
    statement-2;
   }
It is the another form of if that allows to a user to execute another course of statement(s) if the expression evaluates to false. This is done with the help of else part. So, if the expression evaluates to true, statement-1 will be executed, otherwise statement-2 will be executed. The statement-1 and statement-2 can be a single statement, or a compound statement, or a null statement. Example of C++ program for if else(print the largest between 2 numbers) –
#include<iostream.h>
int main()
    {
    int a,b,c;
    cout<<"Enter the value of a=";
    cin>>a;
    cout<<"\nEnter the value of b=";
    cin>>b;
    cout<<"\nEnter the value of c=";
    cin>>c;
    if(a>=b)
        {
        if(a>=c)
           cout<<"\n Largest number = "<<a;
        else
           cout<<"\n Largest number = "<<c;
        }
    else 
        {
        if(b>=c)
           cout<<"\nLargest number = "<<b;
        else
           cout<<"\nLargest number = "<<c;
        }
    return 0;
    }
Output :
Enter the value of a =20
Enter the value of b =50
Enter the value of c =30
Largest number = 50

3) Syntax of if-else-if ladder
if(expression1) 
     statement 1;
else if (expression2)
     statement 2;
else if (expression3)
     statement 3;
         .
         .
else statement 4;
This is called if-else-if ladder. Working : As soon as the expression evaluates to true, the statement associated with it is executed and the rest of the ladder will not executed. If none of the expression evaluates to true, then the final else part will get executed, and if there is no else part specified or mention in the end, then no action will takes place. Example of C++ program for if-else-if ladder :(print the largest of 3 numbers)
#include<iostream.h>
int main()
    {
    int a,b,c;
    cout<<"Enter the value of a =";
    cin>>a;
    cout<<"\nEnter the value of b =";
    cin>>b;
    cout<<"\nEnter the value of c =";
    cin>>c;
    if(a>=b && a>=c)
        cout<<"\nLargest number = "<<a;
    else if(b>=a && b>=c)
        cout<<"\nLargest number = "<<b;
    else
        cout<<"\nLargest number = "<<c;
    return 0;
    }
Output :
Enter the value of a =20
Enter the value of b =50
Enter the value of c =30
Largest number = 50
 ]]>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top