In this tutorial, we will discuss about if statement in CPP programming language
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++:
The syntax of if statements
if(test_expression){ statement(s); //code to be executed only when condition is true }
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.
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
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 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 }
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.
//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 }
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
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 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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…