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.
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
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
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
How if else statements works
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
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
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
Similar post
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