IF Statement Examples in C++


Description

The syntax of an if statement in C++ is − if(boolean_expression) { // statement(s) will execute if the boolean expression is true } If the boolean expression evaluates to true, then the block of code inside the if statement will be executed.

Example Program

Here is the simple program for if statement in C++.

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

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

Basic Programs


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


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes