The if – else statement used for two conditions, if statement will process when the condition true and it’s will choose the else statement automatically when the if condition false. Syntax 2: The two conditions with multiple statement must be include { Opening Brace and } Closing Brace Example 1: The two conditions with single statement Example 2: The two conditions with multiple statement must be include { Opening Brace and } Closing Brace 4.2.The if – else statement
Syntax 1: The two conditions with single statementif(condition)
statement 1;
else
statement 2;
if(condition)
{
statement 1;
...
statement n;
}
else
{
statement 2;
...
statement n;
}
#include<iostream.h>
#include<conio.h>
void main()
{
float score;
cout<<"Enter Score: "; cin>>score;
if(score>=50)
cout<<"Passed";
else
cout<<"Failed";
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
float score;
cout<<"Enter Score: "; cin>>score;
if(score>=50)
{
cout<<"Passed\n";
cout<<"Congratulations";
}
else
{
cout<<"Failed\n";
cout<<"Sorry, Please try again!";
}
getch();
}