Conditional Statements in C++ || Control Structures in C++ || C++



WHAT ARE CONDITIONAL STATEMENTS ?


Conditional Statements are those statements which checks the condition whether it is True or False and then executes certain code as per the instruction.



The above is an image of the conditional statement in real life about a bulb i.e. if a lamp doesn't work what to do, first we will check whether it is plugged in or not if yes then we will check if it burns out or not ?
If yes then we will replace bulb and if not then we will repair the lamp. As we saw in the example the same way a conditional statement will check for the conditions and perform action as per the result.

Conditional Statements are one of the important aspects in Programming Language, it decides the flow of program according to the conditions and the response of the user on that condition.

There are different types of Conditional statements in Programming Language :
- If statement
- If else statement 
- Else statement
- Nested if statement
- Switch Case

if Statement:
if statement is a conditional statement which will execute the code inside this only when the conditions given are true or satisfied.

Syntax:


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




if-else Statement:
if-else statement is a conditional statement which will execute the code only when the condition of if statement is not satisfied or True.

Syntax:-

 if (condition)
 {
	
    //code 1
    //code 1
 }
 else if (condition)
 {
 	//code 2 
    //code 2
 }



else Statement:
else statement is a conditional statement which will execute the code only when the condition of if statement and also of if else statement is not satisfied or True, which means when if condition is true then neither 'if else' nor 'else' statement will be executed but when it is False then if else will get checked and if it's not True either then at-last else statement will be executed

Syntax:-

//else
//code

//For else statements its our choice to use brackets or not

else
{
	//code
    //code
}




Nested if Statement:
Nested if statement is a conditional statement which check for the condition of outer 'if statement' first,if its true then it will check for the inner 'if condition' and then executes the code further. If the outer 'if condition' is not true then it will not process further.

Syntax:-

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




switch case
Switch Case is a special type of conditional statement. In this we will switch a variable to perform variety or different different tasks in the form of cases, the user will select which case he/she wants to execute and in every case it is important to write a break statement. Break statement will ensure that when the condition is satisfied it stops the further execution. In switch the use of default case is optional, it will get executed only when none of the above cases are chosen by the user.

Syntax:-

 switch(variable);
 {
    case 1:
    	//code
        //code
        break;
    case 2:
    	//code
        //code
        break;
    case 3:
    	//code
        //code
        break;
    default:
    	//code
        //code
 }


Now let us see some examples of conditional statements.


if Statement in C++ Language :


 #include <iostream>
using namespace std;

int main()
{
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    if (a > b)
    {
        cout << a << " is greater than " << b;
    }
    if (b > a)
    {
        cout << b << " is greater than " << a;
    }
    return 0;
}

Switch case in C++ :


#include <iostream>
using namespace std;

int main()
{
    int a;
    cout << "Enter a number for Greetings: ";
    cin >> a;
    switch (a)
    {
    case 1:
        cout << "Hello";
        break;
    case 2:
        cout << "Hello Users";
        break;
    case 3:
        cout << "Thankyou Users";
        break;
    case 4:
        cout << "Good Byee !!";
        break;
    default:
        cout << "Invalid Input";
    }

    return 0;
}

else statement in C++:


#include <iostream>
using namespace std;

int main()
{
    int time = 24;

    if (time < 13)
    {
        cout << "Good Morning";
    }
    else
    {
        cout << "Good Night";
    }

    return 0;
}
  

else-if statement in C++:


#include <iostream>
using namespace std;

int main()
{
    int time = 24;

    if (time < 13)
    {
        cout << "Good Morning";
    }
    else if (time < 16)
    {
        cout << "Good Afternoon";
    }
    else if (time < 20)
    {
        cout << "Good Evening";
    }
    else
    {
        cout << "Good Night";
    }

    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post