C IF Statement
C IF Statement
The if Statement
Use the if statement to specify a block of C++ code to be executed if a
condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:
Example
if (20 > 18) {
cout << "20 is greater than 18";
}
if (x > y) {
cout << "x is greater than y";
}
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
int main()
{
float percent;
cout<<"Enter your percentage: ";
cin>>percent;
cout<<"You scored "<<percent<<"%"<<endl;
if (percent>=40)
{
cout<<"Congratulation: You have passed";
}
getch();
return 0;
}
if … else statement
if … else statement is a two way branching statement. It is similar to if
statement but the only difference is if the condition is false then a different
block of statements are executed which is inside else statement.
int main()
{
float percent;
cout<<"Enter your percentage: ";
cin>>percent;
cout<<"You scored "<<percent<<"%"<<endl;
if (percent>=40)
{
cout<<"Congratulation: You have passed";
}
else
{
cout<<"Sorry: You have failed";
}
getch();
return 0;
}
This program asks the percentage of a student and display which division
he/she has got. The criteria for division is displayed below:
percentag
Division
e
Distinctio
>=80
n
<40 Fail
According to the condition, the result is displayed.
Output
Enter your percentage: 87.67
You scored 87.67%
You have passed with distinction
Enter your percentage: 34.50
You scored 34.5%
Sorry: You have failed
Nested if statements
An if statement inside another if statement is known as nested if statements.
Nested if statements are used if there is a sub condition to be tested after one
condition has been checked. The depth/number of nested if statements
depends upon the number of conditions to be checked.
Syntax of nested if statement
if (condition 1)
{
statements;
if (sub condition 1)
{
statements;
}
statements;
}
else if (condition 2)
{
statements;
if (sub condition 2)
{
statements;
}
statements;
}
... ... ...
... ... ...
else
{
statements;
if (sub condition n)
{
statements;
}
statements;
}
Flowchart for nested if statement
Example of nested if statement
C++ program to check if a number entered by user is even and divisible
by 5 or not.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n;
cout<<"Enter a number: ";
cin>>n;
if (n%2 == 0)
{
if (n%5 == 0)
{
cout<<"Number is even and divisible by 5";
}
else
{
cout<<"Number is even but not divisible by 5";
}
}
else
{
if (n%5 == 0)
{
cout<<"Number is not even but divisible by 5";
}
else
{
cout<<"Number is not even and not divisible by 5";
}
}
}
This program checks whether a number is even and divisible by 5 or not. A
number is entered by user which is then checked by using nested if
statements.
Output
Enter a number: 6
Number is even but not divisible by 5
Enter a number: 20
Number is even and divisible by 5