Week 9 - Decision Making
Week 9 - Decision Making
There come situations in real life when we need to make some decisions and based on
these decisions, we decide what should we do next. Similar situations arise in programming
also where we need to make some decisions and based on these decisions we will execute
the next block of code.
if statement
if..else statements
nested if statements
if-else-if ladder
switch statements
Jump Statements:
• break
• continue
• goto
2 • return
Decision Making Structure
1
Des
3
if statement in C++
4
if statement in C++
▪ Example:
if(condition)
statement1;
statement2;
6
if statement in C++
7
if statement in C++
#include <iostream>
using namespace std;
int main() {
int number;
Output 2:
Enter a number: -5
This statement is always executed.
9
C++ if...else Statement
The if statement can have an optional else clause. Its syntax is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
The if..else statement evaluates the condition inside the
parenthesis.
10
C++ if...else Statement
11
C++ if...else Statement
12
C++ if...else Statement
Example 2: C++ if...else Statement
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
Output 1
#include <iostream>
Enter an integer: 4
using namespace std; You entered a positive integer: 4.
This line is always printed.
int main() {
int number;
13
C++ if...else Statement
Example 2: C++ if...else Statement
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
Output 2
#include <iostream>
Enter an integer: -4
using namespace std; You entered a negative integer: -4.
This line is always printed.
int main() {
int number;
14
Quiz!
15