The if else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. An if statement executes code conditionally depending on the result of the condition in parentheses.
When condition in parentheses is true it will enter to the block of if statement which is defined by curly braces like open braces and close braces. Opening bracket till the closing bracket is the scope of the if statement. The else block is optional and can be omitted. It runs if the if statement is false and does not run if the if statement is true because in that case if statement executes.
Syntax :
if ( condition )
{
// block of statement to be executed if condition is true ;
}
else
{
block of statement to be executed if condition is false ;
}
The program takes an integer input from the user and determines the number of digits in the input number.
#include <iostream> using namespace std; int main() { int no; cout<<"\nEnter an integer number between 1 & 99999: "; cin>>no; if(no<100 && no>=1) { cout<<"\nIts a two digit number"; } else if(no<1000 && no>=100) { cout<<"\nIts a three digit number"; } else if(no<10000 && no>=1000) { cout<<"\nIts a four digit number"; } else if(no<100000 && no>=10000) { cout<<"\nIts a five digit number"; } else { cout<<"\nNumber is not between 1 & 99999"; } return 0; }To download raw file Click Here
Enter an integer number between 1 & 99999: 56 Its a two digit number
The program uses an if-else statement to print a greeting message based on the value of the integer variable time. The program starts by declaring an integer variable time and initializing it to 22.
Then, the program uses an if-else statement to check the value of time. If time is less than 10, the program outputs the message "Good morning." . If time is less than 20, the program outputs the message "Good day." . If time is greater than or equal to 20, the program outputs the message "Good evening.". Since time is initialized to 22, it is greater than 10 and 20, so the program will output the message "Good evening." .
#include<iostream> using namespace std; int main() { int time = 22; if(time < 10) { cout<<"\nGood morning."; } else if(time < 20) { cout<<"\nGood day."; } else { cout<<"\nGood evening."; } return 0; }To download raw file Click Here
Good evening.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions