4.2.The if – else statement

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 1: The two conditions with single statement

if(condition)
	statement 1;
else
	statement 2;

Syntax 2: The two conditions with multiple statement must be include { Opening Brace and } Closing Brace

if(condition)
{
	statement 1;
	...		
	statement n;
}
else
{
	statement 2;
	...		
	statement n;
}

Example 1: The two conditions with single statement

#include<iostream.h>
#include<conio.h>
void main()
{
	float score;
	cout<<"Enter Score: ";            cin>>score;
	if(score>=50)
		cout<<"Passed";
	else
		cout<<"Failed";
	getch();
}

Example 2: The two conditions with multiple statement must be include { Opening Brace and } Closing Brace

#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();
}
Suggest Edit