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

 

There are other Java language keywords that are similar to this post

if keyword in Java language

else keyword in Java language

 

Similar post

If statements in C language

If statements in C++ language

If statements in Python language

 

 Nested if statements in C language

 Nested if statements in Java language

 Nested if statements in C++ language

 Nested if statements in Python language

 

Suggested for you

for loop in Java Language

for loop in C Language

for loop in Python Language

for loop in C++ Language

 

While loop in Java Language

While loop in C Language

While loop in Python Language

 

Nested while loop in Java

Nested while loop in C++

Nested while loop in C

Nested while loop in Python

 

 

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago