If block

if statement in CPP programming language

In this tutorial, we will discuss about if statement in CPP programming language

if statement in CPP programming

Typically, if statement evaluates the particular condition inside the parenthesis. When the expression in the if statement is evaluated to be true, body part statements of if is executed. When the expression is evaluated to be false, the flow of control exits from the body of if (skipped if part) and moves to the nearest iteration.

If statements in C++

Three types of “if” statements are mainly available in C++:

  • if statements
  • if else statements
  • if else if else statements

if statement in CPP programming

The syntax of if statements

if(test_expression){
statement(s);
//code to be executed only when condition is true
}

Flow chart of if statement

flow diagram of if in Cpp

When the test expression(return Boolean value) is evaluated and that is true, Statements inside the body of if is executed then displays output

When the test expression is evaluated and that is false, Statements inside the body of if are skipped from the execution and flow of control move to outside of if statements.

How if statements works

flow of control of if in C++

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int age=17;
    if (age>=15)
    {
         cout << "You are teenagers" << endl;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

you are a teenager

In the above program, age is declared  as 17. the test expression(age>=15) is evaluated. that is true So Statements inside the body of if are executed the output displays

//when the test expression is true executes the program and display output

Program 2

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int age=17;
    if (age<=15)//evaluate test expression
    {           //test expression is false
         cout << "You are teenagers" << endl;
    }flow of control skip the execution and come out of if condition
    getch();
    return 0;
}

 

//if  the test expression is false, no output from this program

When the above code is executed, it produces the following results:

You are teenagers

 

if else statements

Some times the if statements can have a optional part else. Typically, the if statements executes a section of codes when the test expression evaluated to true. when the test expression is false code inside the else statements are executed

Syntax of if else statements

//Syntax of the if ..else statement
if(test_expression) //if part
{//if test expression is true, this part is executed
 statements 1;
 statements 2;
}
else //else part
{
 //if test expression is false, this part is executed
 statement 3
}

flow chart of if – else statement

Flowchart of if else in CPP

How if else statements works

 

flow of control of if else

Program 1

 

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int age=20;
    if(age>=18){
    cout << "You are qualified for voting" << endl;
    }
    else{
        cout << "You are not qualified for voting" << endl;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

you are qualified for voting

 

According the program if age is above 18, he/she is eligible for voting in the election, if not he/she is not eligible for voting.

if else if -else statements

Syntax of if- else if- else statements

//Syntax of the if ..else if else statement
if(test_expression) //if part
{//if test expression is true, this part is executed
 statements 1;
 statements 2;
}
else if//else if part 
{ //if test expression is false, this part is executed 

}
else //else part
{
 //finally when the above test expressions are false, this part is executed
 
}

Flow diagram of if – else if- else statements

flow diagram if else-if else i n CPP

Initially, test expression is evaluated by if part. when it returns true codes inside the body of if is executed. Otherwise returns false flow of control move to body of else if , then test expression is evaluated by else if part. when it returns true codes inside the body of else if is executed.  , otherwise returns false flow of control moves to body of else and executed codes inside the body of else

How if else-if else statements works

Flow of control if elseif

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int age=17;
    if (age>18)
    {
         cout << "You are qualified for voting" << endl;
    }
    else if(age==18){
         cout << "You can register in voting list" << endl;
    }
    else{
         cout << "You are not qualified for voting" << endl;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

You are not qualified for voting

 

Suggested for you

 Nested if statements in C language

 Nested if statements in Java language

 Nested if statements in C++ language

 Nested if statements in Python language

 

If statement in C programming language
if statement in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago