If else statement in C++
If else statement Definition :
The If else statement in C++ is used to carry out operations depending on a particular situation. If and only if the provided condition is true, the operation described in the if block are carried out otherwise else block is carried out.
Syntax:
if(condition){ // statements to be executed if condition is true; } else{ // statements to be executed if condition is false; }
Working of if else statement :
- Step 1 : In the first step, condition/expression is checked whether it is true or false.
- Step 2 : If statement is True, if block will be executed for performing the operations.
- Step 3 : If the condition is false , else block will be executed for performing the executions
Implementation of If else statement in C++
Example 1:
#include <iostream> using namespace std; int main() { int i=5; if(i>1){ // checking the condition and take decision cout<<"PrepInsta is one of the best for placement preparation"; } else{ cout<<"PrepInsta"; } return 0; }
Output:
In the above example , the value of i initialize as 5 and when the condition of if (i>1) is checked , this is taken as true and block if is executed and “PrepInsta is one of the best for placement preparation” will be print on the output screen.
Example 2:
#include <iostream> using namespace std; int main() { int i=5; if(i<1){ // checking the condition and take decision cout<<"PrepInsta is one of the best for placement preparation"; } else{ cout<<"PrepInsta"; } return 0; }
Output:
PrepInsta
In the above example , the value of i initialize as 5 and when the condition of if (i<1) is checked , this is taken as false and block else is executed and “PrepInsta” will be print on the output screen.
For more related content :Click-Here
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment