If Else Statements-1
If Else Statements-1
1
MESSAGE OF THE DAY
Psalm: 128:1
2
C++ If Else
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)
}
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)
}
7
Flow Diagram
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
9
If Else If Statement Flow Diagram
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
12
QUESTIONS TIME
13
NEXT TOPIC
• Switch Statement
14
Reading Assignment
• LOOPs
15
Reference
1. https://www.tutorialkart.com/cpp/cpp-relational-
operators/
16