if-else condition Statement in C++ programming

When we want to execute a program on base of some particular condition is known as conditional statements. So, C++ provides some statements for implementing selection control structure. In conditional statement the condition is checked first and after that the block of if condition is executed if condition is true otherwise else block is executed.   
if-else statement: It is the basic conditional statement which have the single entry and single exit statement. If the condition of if statement is true the the block of the condition is executed otherwise else block is executed.

Syntax of if-else statement:
if(condition)
{
Statement 1....n;  // this block is executed if condition is true.
}
else
{
Statement 1....n;  // this block is executed if condition is false.
}

Let's understand if statement with the help of a C++ program:

Program Code:
#include <iostream>
using namespace std;
int main()
{   
int x, y, z; 
cout << "Enter First number : "; 
cin >> x;
cout<<endl; 
cout << "Enter Second number : "; 
cin >> y;
cout<<endl;
cout << "Enter Third number : "; 
cin >> z;
cout<<endl;
if((x >= y) && (x >= z))
{     
cout << "Largest number is : " ;
cout<< x;

else if ((y >= x) && (y >= z))
{     
cout << "Largest number is: ";
cout<< y;

else 
{   
cout << "Largest number is : ";
cout<< z;
}   
return 0;
}

The program output is tested on www.jdoodle.com 
Output :
Enter first number x  = 10
Enter second number y  = 40
Enter third number z  = 60
Largest number is = 60


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: