if 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 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.

Syntax of if statement:

if(condition)
{

Statement 1....n;  // this block is executed if condition is true.
}

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

Find the largest number from three given number by the user during run-time using if Statement:

#include <iostream>
using namespace std;
int main()
{    
    int  x, y, z;
    cout << "Enter first number x  = ";
    cin >> x;
    cout<<endl;
    cout << "Enter second number y  = ";
    cin >> y;
    cout<<endl;
    cout << "Enter third number z  = ";
    cin >> z;
    cout<<endl;
    if(x >= y && x >= z)
    {
        cout << "Largest number is = " << x;
    }
    if(y >= x && y >= z)
    {
        cout << "Largest number is = " << y;
    }
    if(z >= x && z >= y) 
    {
        cout << "Largest number is = " << z;
    }
    return 0;
}

Output of the above program:

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: