if Statement in C++ Programming


The if statement is used to route program execution through two different paths. The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. The if statement is written with the if keyword, followed by a condition in parentheses, with the code to be executed in between curly brackets.

 Syntax :
   If ( condition )
   {
       // block of statement to be executed if condition is true ;
   }

The program is a C++ program to find the greatest of two numbers. Firstly, the program declares two integer variables 'a' and 'b'. The user is prompted to enter the values of 'a' and 'b' using the 'cin' function. The values of 'a' and 'b' are then stored in their respective variables.

The program then checks the values of 'a' and 'b' using the 'if' condition. If 'a' is greater than 'b', then it displays a message stating that 'a' is the greatest number using the 'cout' statement. If 'b' is greater than 'a', then it displays a message stating that 'b' is the greatest number using the 'cout' statement. If 'a' and 'b' are equal, then it displays a message stating that 'a' and 'b' are equal using the 'cout' statement. Finally, the program returns 0, which indicates that the program has executed successfully.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cout<<"\nEnter The Value of A & b :";
    cin>>a>>b;//25,45
    if(a>b)//25>45
    {
        cout<<a<<" is Greatest Number";
    }
    if(b>a)//45>25
    {
        cout<<b<<" is Greatest Number";
    }
    if(a==b)//45==25
    {
        cout<<a<<" and "<<b<<" are Equal";
    }
    return 0;
}
 

Output

Enter The Value of A & b :23
56
56 is Greatest Number
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples