0% found this document useful (0 votes)
22 views15 pages

Week 9 - Decision Making

The document discusses different decision making statements in C++ including if statements, if-else statements, and nested if statements. It provides examples of using if statements to check conditions and execute code blocks accordingly. If a condition is true, the if code block runs, and if false, the else block runs or nothing runs. Decision making allows programmers to control program flow based on evaluating conditions.
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)
22 views15 pages

Week 9 - Decision Making

The document discusses different decision making statements in C++ including if statements, if-else statements, and nested if statements. It provides examples of using if statements to check conditions and execute code blocks accordingly. If a condition is true, the if code block runs, and if false, the else block runs or nothing runs. Decision making allows programmers to control program flow based on evaluating conditions.
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/ 15

Decision Making in C++

(if , if..else, Nested if, if-else-if )


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.

Decision-making statements in programming languages decide the direction of the flow of


program execution. Decision-making statements available in C or C++ are:

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++

In computer programming, we use the if statement to


run a block code only when a certain condition is met.
For example, assigning grades (A, B, C) based on marks
obtained by a student.
• if the percentage is above 90, assign grade A
• if the percentage is above 75, assign grade B
• if the percentage is above 65, assign grade C

4
if statement in C++

The syntax of the if statement is:


if (condition) {
// body of if statement
}
The if statement evaluates the condition inside the parentheses ( ).
• If the condition evaluates to true, the code inside the body
of if is executed.
• If the condition evaluates to false, the code inside the body
of if is skipped.
Note: The code inside { } is the body of the if statement.
5
if statement in C++

▪ Example:

if(condition)
statement1;
statement2;

// Here if the condition is true, if block


// will consider only statement1 to be inside
// its block.

6
if statement in C++

7
if statement in C++

Example 1: C++ if Statement


// Program to print positive number entered by the user
// If the user enters a negative number, it is skipped

#include <iostream>
using namespace std;
int main() {
int number;

cout << "Enter an integer: ";


cin >> number;
// checks if the number is positive
if (number > 0) {
cout << "You entered a positive integer: " << number << endl; Output:
} Enter an integer: 5
cout << "This statement is always executed."; You entered a positive number: 5
return 0; This statement is always executed.
8 }
if statement in C++
Output 1:
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.

When the user enters 5, the condition number > 0 is evaluated


to true and the statement inside the body of if is executed.

Output 2:
Enter a number: -5
This statement is always executed.

When the user enters -5, the condition number > 0 is


evaluated to false and the statement inside the body of if is
not 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

If the condition evaluates true,


▪ the code inside the body of if is executed
▪ the code inside the body of else is skipped from
execution
▪ If the condition evaluates false,
▪ the code inside the body of else is executed
▪ the code inside the body of if is skipped from
execution

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;

In the above program, we have the


cout << "Enter an integer: "; condition number >= 0. If we enter the number
cin >> number;
greater or equal to 0, then the condition
if (number >= 0) {
evaluates true.
cout << "You entered a positive integer: " << number << endl;
Here, we enter 4. So, the condition is true. Hence,
}
the statement inside the body of if is executed.
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}

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;

Here, we enter -4. So, the condition is false. Hence,


cout << "Enter an integer: "; the statement inside the body of else is executed.
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}

14
Quiz!

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy