IF Statement Examples in C++


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.

The program checks if a given integer num is less than 100 and greater than 50. The program begins by initializing the integer variable num to 90.

Then, the program checks if num is less than 100 using an if statement. If the condition is true, then the program executes the block of code inside the if statement, which prints the message "Number is less than 100".

Next, there is another if statement nested inside the first one, which checks if num is greater than 50. If this condition is true, then the program prints the message "Number is greater than 50".

Since the value of num is 90, both conditions are true, and both messages are printed to the console.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
   int num=90;
   if(num<100)
   {
      cout<<"\nNumber is less than 100"<<endl;
      if(num>50)
      {
         cout<<"\n Number is greater than 50";
      }
   }
   return 0;
}
To download raw file Click Here

Output

Number is less than 100
Number is greater than 50

The program reads two integers a and b from the user and then checks if a is greater than b. The program begins by declaring two integer variables a and b. Then, the program prompts the user to enter the first number by printing the message "Enter the number :" using cout. The user's input is then read and stored in a using the cin function.

Similarly, the program prompts the user to enter the second number by printing the message "Enter the another number :". The user's input is then read and stored in b using cin.

After that, the program checks if a is greater than b using an if statement. If the condition is true, then the program executes the block of code inside the if statement, which prints the message "A is greater then B" using cout.

If the value of a is not greater than b, then the program skips the if block and proceeds to the next statement after the if block, which is return 0; . This terminates the program.

Source Code Example : 2

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cout<<"\nEnter the number :";
    cin>>a;
    cout<<"\nEnter the another number :";
    cin>>b;
    if(a<b)
    {
        cout<<"\nA is greater then B";
    }
    return 0;
}
To download raw file Click Here

Output

Enter the number :54
Enter the another number :78
A is greater then B

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