0% found this document useful (0 votes)
34 views

C IF Statement

C++ supports logical conditions like less than, greater than, equal to etc. to be used in if, else if and else statements. The if statement executes code if a condition is true, else if checks another condition if the first is false, and else executes code if all conditions are false. Nested if statements allow checking sub-conditions, with an if inside another if. Multiple conditions can be checked in a single if statement using && and || logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

C IF Statement

C++ supports logical conditions like less than, greater than, equal to etc. to be used in if, else if and else statements. The if statement executes code if a condition is true, else if checks another condition if the first is false, and else executes code if all conditions are false. Nested if statements allow checking sub-conditions, with an if inside another if. Multiple conditions can be checked in a single if statement using && and || logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ Conditions and If Statements

What is the if statement in C++?


C++ has the following conditional statements: Use if to specify a block of code to be
executed, if a specified condition is true. Use else to specify a block of code to be
executed, if the same condition is false. Use else if to specify a new condition to test, if
the first condition is false.

What are the two parts of an if statement C++?


All if statements are made up of two parts, a boolean expression and an action.
Remember that boolean values are just true or false. Boolean expressions therefore are
expressions which can be evaluated to either a true or a false value.

C++ supports the usual logical conditions from mathematics:


int a = 1, b = 2;
 Less than: a < b = True
 Less than or equal to: a <= b = True
 Greater than: a > b = False
 Greater than or equal to: a >= b = False
 Equal to a == b = False
 Not Equal to: a != b = True
You can use these conditions to perform different actions for different decisions.
C++ has the following conditional statements:
 Use if to specify a block of code to be executed, if a specified condition is
true
 Use else to specify a block of code to be executed, if the same condition
is false
 Use else if to specify a new condition to test, if the first condition is
false
 Use switch to specify many alternative blocks of code to be executed

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";
}

We can also test variables:


Example
int x = 20;
int y = 18;

if (x > y) {
  cout << "x is greater than y";
}

Implement the if Statement With Multiple Conditions in C++


1. Implement the if Statement With Multiple Conditions in C++
2. Implement the if Statement With Multiple Conditions Using
the && Logical Operator in C++
3. Implement the if Statement With Multiple Conditions Using the || Logical
Operator in C++
4. Implement the if Statement With Multiple Conditions Using
the && and || Logical Operators in C++
5. In C++, we can have multiple if statements in two ways. There can be
nested if statements and multiple if statements in one program to check
for different conditions.
6. Let’s discuss the two cases in detail.
7. There may be a situation where we need to check multiple conditions
using a nested if statement. For example, suppose we want to check
whether a number is between 45 and 60.
8. The code to check the given condition using a nested if statement is
shown below:
9. //C++ program demonstrating nested if-statement

10. #include <iostream>


11. using namespace std;
12.
13. int main()
14. {
15. cout << "Enter a number: "; 45
16. int num;
17. cin >> num;
18.
19. if (num >= 45 )
20. {
21. if(num < 60)
22. {
23. cout << "Entered number is between 45 and 60";
24. }
25.
26. else
27. {
28. "Entered number is not in between 45 and 60";
29. }
30. }
31.
32. else
33. cout << "Entered number is not in between 45 and 60";
34.
35. return 0;
36. }

37. Here, we used one if statement for every condition in the form of a


nested if statement. In nested if statements, we need to write a longer
piece of code.
38. Instead, we can use multiple logical && operators in a single if statement
to make our code simpler and avoid writing unnecessary lines of code.

What does && mean in C++?


logical AND operator
The logical AND operator ( && ) returns true if both operands are true and returns false otherwise.
The operands are implicitly converted to type bool before evaluation, and the result is of type bool .
Logical AND has left-to-right associativity
Syntax of if statement
if (condition)
{
statements;
... ... ...
}

int x = 20;
int y = 18;

if (x > y) {
  cout << "x is greater than y";
}

Flowchart for if statement


Example of if statement
C++ program to check percentage of a student and display pass if it is
greater than 40.
#include <iostream>
#include <conio.h>
using namespace std;

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.

Syntax of if…else statement


if (condition)
{
statements;
... ... ...
}
else
{
statements;
... ... ...
}
Flowchart for if … else statement

Example of if … else statement

C++ program to check percentage of a student and display pass or fail.


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

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 student. If the percentage is equal to or


greater than 40 then a congratulation message saying you have passed is
displayed. Otherwise, i.e if percentage is below 40 then a sorry message is
printed on the output screen.
Output
Enter your percentage: 86
You scored 86%
Congratulation: You have passed
Enter your percentage: 37
You scored 37%
Sorry: You have failed

if … else if … else statement


if … else if … else statement is used for multiple branching. When there are
two or more conditions that needs to be checked to decide which block of
statement is to be executed, it is used. The number of else if statements
depends upon the number of conditions to be checked.
Syntax of if…else if…else statement
if (condition 1)
{
statements;
... ... ...
}
else if (condition 2)
{
statements;
... ... ...
}
... ... ...
... ... ...
else if (condition n)
{
statements;
... ... ...
}
else
{
statements;
... ... ...
}

Flowchart for if … else if … else statement


.T. True/ .F. False
Example of if … else if … else statement
C++ program to check percentage of a student and display
division(distinction, first, second, third or fail).
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float percent;
cout<<"Enter your percentage: ";
cin>>percent;
cout<<"You scored "<<percent<<"%"<<endl;
if (percent>=80)
{
cout<<"You have passed with distinction";
}
else if (percent>=60)
{
cout<<"You have passed with first division";
}
else if (percent>=50)
{
cout<<"You have passed with second division";
}
else if (percent>=40)
{
cout<<"You have passed with third division";
}
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

>=60 and First


<80 division
>=50 and Second
<60 Division

>=40 and Third


<50 Division

<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

You might also like