0% found this document useful (0 votes)
21 views16 pages

If Else Statements-1

The document discusses different forms of if-else statements in C++, including if statements, if-else statements, and if-else-if statements. It provides the syntax for each and flow diagrams to illustrate the logic. Examples are given to demonstrate how each can be used to check conditions and execute code blocks conditionally. Key points covered include using if-else-if to sequentially evaluate multiple conditions and the else block being optional for all statement forms.

Uploaded by

Kaptain Suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

If Else Statements-1

The document discusses different forms of if-else statements in C++, including if statements, if-else statements, and if-else-if statements. It provides the syntax for each and flow diagrams to illustrate the logic. Examples are given to demonstrate how each can be used to check conditions and execute code blocks conditionally. Key points covered include using if-else-if to sequentially evaluate multiple conditions and the else block being optional for all statement forms.

Uploaded by

Kaptain Suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C++ If Else

• In C++, if else statements are used to perform conditional


execution of statement(s). Based on the result of a condition,
the decision to execute a block is taken.
• In this lesson, we shall learn the different forms of if else
statement, their syntax with detailed explanation and
examples for each of them.

1
MESSAGE OF THE DAY

"Blessed is every one that feareth the


LORD; that walketh in his ways".

Psalm: 128:1

2
C++ If Else

In C++, If Else statement can occur in three forms. They are:

1. If statement
2. If-Else statement
3. If-Else-If statement

3
C++ If Statement
• Following is the syntax of simple C++ If statement.
if (condition) {
// statement(s)
}

• If the condition is true, the statement(s) inside if block are


executed.
• If the condition is false, the statement(s) inside if block are not
executed.
• In either of the cases, the execution continues with the subsequent
statements after the completion of if statement.
4
If Statement Flow Diagram
• Following is the flow diagram of if statement in C++.
example
Write C++ program, to determine whether a number entered by user is
positive.

C++ Program
#include <iostream>
using namespace std;

int main() {
int a = 10;
if (a>0) {
cout << a << " is positive.";
}
}
5
C++ If Else Statement
• Following is the syntax of C++ If Else statement.
if (condition) {
// statement(s)
} else {
// statement(s)
}

• If the condition is true, the statement(s) inside if block are executed.


If the condition is false, the statement(s) inside else block are
executed.
• After the execution of either if block or else block, program
execution continues with next statements after if-else statement.
6
C++ If Else Statement

• Else block is optional. So, if you do not provide else block to an


if block, it becomes a simple if statement. So, if-else could be
considered as an extension to simple if statement.

7
Flow Diagram

• Following is the flow diagram of if-else statement in C++.


Example: In the following example C++ program, we use if-else statement to
check if a number is positive or not.
C++ Program
#include <iostream>
using namespace std;

int main() {
int a = -10;
if (a>0) {
cout << a << " is positive.";
} else {
cout << a << " is not positive.";
}
Return 0;
} 8
If Else If Statement

• Following is the syntax of C++ if-else-if statement.


if (condition1) { If condition1 is true, executes the statement(s) inside if
block are executed. Else, the next condition in the else-if
// statement(s) ladder is evaluated.
} else if (condition2) {
The next condition is condition2. If the condition2 is true,
// statement(s) the statement(s) inside corresponding else if block are
} else if (condition3) { executed. If condition2 is also false, condition3 is
// statement(s) executed, and so on, until a condition is true.
} else { If no condition evaluates to true, then else block is
// statement(s) executed.
} Even in if-else-if statement, the last else block is optional.

9
If Else If Statement Flow Diagram

• Following is the flow diagram of if-else-if statement in C++.

10
Example: In the following example C++ program, we use if-else-if
statement to check if a number is positive, negative or zero.

. C++ Program
#include <iostream>
using namespace std;

int main() {
int a = -7;
if (a>0) {
cout << a << " is positive.";
} else if(a<0) {
cout << a << " is negaitive.";
} else {
cout << a << " is zero.";
}
Return 0;
}
11
Conclusion

• In this C++ lesson, we learned about different type of C++ If


Else statement: simple If statement, if-else statement, if-else-
if statement, with syntax, flow diagram and example for each
one of them.

12
QUESTIONS TIME

13
NEXT TOPIC

• Switch Statement

14
Reading Assignment
• LOOPs

15
Reference
1. https://www.tutorialkart.com/cpp/cpp-relational-
operators/

16

You might also like