Conditional Statements in C++
If Statement :
Syntax:
if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
As the name suggests if condition is used to execute a particular block of statement only if a certain condition is satisfied. Let's understand it with a code
A program to understand the if statement
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b; //variable declaration
cin>>a>>b; //scanning input from user
if(a>b) //if condition
{
cout<<a; //printing output
return 0; //sending 0 to the compiler to end the program
}
cout<<"wrong input"; //printing output
return 0; //sending 0 to the compiler to end the program
}
Inputs: a=9 b=10
Output: wrong input
Input: a=10 b= 9
Output: 10
In the above program the if condition will check whether a is greater than b if it is yes so if block will be executed.
Previous Posts :
If – Else:
Syntax:
if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
else
{
Statement1;
Statement2;
...
...
Statement n;
}
You have learned the if block earlier. In the if-else condition the if block is executed if the condition is satisfied but if the condition is not satisfied then the else block is executed. It is used when you want the compiler to do something if the condition is fulfilled else execute the other block. Let's understand it by finding a number is even or odd.
A program to understand the if-else statement
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a; //variable declaration
cin>>a; //scanning input from user
if(a%2==0) //check weather the number is divisible by 2
{
cout<<"even"; //since the number is divisible by 2 it is even number
}
else
{
cout<<"odd"; // as the number is not divisible by 2 it is odd
}
return 0;
}
Input: 9
Output: odd
Input: 8
Output: even
Else if (Ladder):
Syntax:
if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
else if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
else if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
else
{
Statement1;
Statement2;
...
...
Statement n;
}
Now sometimes you have to check multiple conditions at that time you use ladder if condition or if-else if-else condition. Let us see the code to find whether a number is greater than or equal to or less than 5 for better understanding.
A program to understand the if-else if-else statement
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a; //variable declaration
cin>>a; //scanning input from user
if(a>5) //check whether the number is Greater than 5
{
cout<<"Greater";
}
else if(a<5) //check weather the number is less than 5
{
cout<<"Less";
}
else
{
cout<<"equal"; // since it is not less than or Greater than 5 it must be equal to 5
}
return 0;
}
Input: 5
Output: equal
Input: 7
Output: greater
Input: 3
Output: less
Next Post:
Nested if:
if(condition)
{ if(condition)
{
Statement1;
Statement2;
...
...
Statement n;
}
}
else
{
Statement1;
Statement2;
...
...
Statement n;
}
Sometimes in the program, you have to check a condition only if the previous condition is satisfied at that time we use nested if condition. It means using if condition inside another if condition.
A program to understand the nested if statement
In this program, we will find whether a year is a leap year or not. A year is a leap year if it is divisible by 4 or if the year comes in centuries like 2000 or 1900 then it should be divisible by 400
#include<bits/stdc++.h>
using namespace std;
int main()
{
int year; //variable declaration
cin>>year; //scanning input from user
if(year%100==0) //check weather the year is divisible by 100
{
if(year%400==0) // as the year is divisible by 100 it is a century year so check whether the year is divisible by 400
{
cout<<"leap year"; // as the year is divisible by 400 it is a leap year
}
else
{
cout<<"not a leap year"; // as the year is not divisible by 400 it is not a leap year
}
}
else if(year%4==0) // as the year is not divisible by 100 it is not a century year so check whether the year is divisible by 4
{
cout<<"leap year"; // as the year is divisible by 4 it is a leap year
}
else
{
cout<<"not a leap year"; // as the above condition is not satisfied the year is not a leap year
}
return 0;
}
Input: 1999
Output: not a leap year
Input: 2000
Output: leap year
Input: 2024
Output: leap year
0 Comments