Basic Programming - Selection Structures

JasperGarcia9 3 views 29 slides Aug 30, 2025
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

In this lesson, students will explore two foundational concepts in C++ programming: Operators and Input Handling using cin (commonly referred to as the Scanner equivalent in C++). The session introduces how arithmetic, relational, logical, and assignment operators are used to perform calculations an...


Slide Content

COMPUTER PROGRAMMING 1
CC102
LESSON 3: Selection Structure

INTENDED
LEARNING
OUTCOME
I. Plan/ Create
Decisiong- making Logic
II. Use the If – Else
Conditional Statements
III. Use Switch
Statements

A PROGRAMMER IS
ASSIGNED TO CREATE A
PROGRAM THAT WILL
IDENTIFY IF THE STUDENT
IS ON LEGAL
AGE OR NOT.
WHAT WILL THE PROGRAMMER DO?

PLANNING
DECISION- MAKING
LOGIC
•Programmers must plan the
complex portions of programs
using paper and pencil.
Programmers often use
pseudocode, a tool that helps
them plan a program’s logic by
writing plain statements.

PLANNING
DECISION- MAKING
LOGIC
•A flowchart is will be made from
the pseudocode, you will write the
steps in diagram form, as a series
of shapes connected by arrows.
•The result of every decision is
either true or false, also known as
the Boolean values.
Start
Enter
Age
If
age
>=
18
Print
Legal
Print
Minor
True
False
End

PLANNING
DECISION- MAKING
LOGIC
A decision symbol always contains a
statement that is known as
condition.
1.Conditional Operators
2.Logical Operators
If age
>=
18

IF-ELSE STATEMENTS
Theif statement is used to test the condition. It checks
Boolean condition: true or false.
There are various types of if statement.
❖if statement
❖if-else statement
❖if-else-if ladder
❖nested if statement

IF STATEMENT
Use the if statement
to specify a block of
code to be executed
if a condition is true.

IF STATEMENT SYNTAX
Sample Program

IF-ELSE
STATEMENT
The if-else statement
also tests the
condition. It executes
the if block if condition
is true otherwise else
block is executed.

IF-ELSE STATEMENT SYNTAX
Sample Program

IF-ELSE LADDER
STATEMENT
The if-else-if ladder
statement executes one
condition from
multiple statements.
It is used when more
than one condition is
to be checked.

IF-ELSE STATEMENT SYNTAX
An if … else if … else statement must have only one if block but can have as many else if block as required.
Else part is optional and may be present or absent.

NESTED IF
The nested if statement
represents the if block
within another if block.
Here, the inner if block
condition executes
only when outer if
block condition is true.

NESTED IF SYNTAX

NESTED IF SYNTAX
Sample Program

SWITCH STATEMENT
•The switch statement executes one statement from
multiple conditions.
•It is like if-else-if ladder statement. The switch statement
works with byte, short, int, long, String.
•The switch statement tests the equality of a variable
against multiple values.

POINTS TO REMEMBER
1.There can be one or N number of case values for a switch expression.
2.The case value must be of switch expression type only. The case value must be literal or
constant. It doesn't allow variables.
3.The case values must be unique. In case of duplicate value, it renders compile-time
error.
4.Theswitch expression must be of byte, short, int, long, string.
5.Each case statement can have a break statement which is optional. When control
reaches to the break statement, it jumps the control after the switch expression. If a
break statement is not found, it executes the next case.
6.The case value can have a default label which is optional.

SWITCH
FLOWCHART

SWITCH
STATEMENT
The program will
print the day of a
given number.

USING
TERNARY
OPERATOR
•We can also use ternary
operator (? :) to
perform the task of if
else statement. It is a
shorthand way to
check the condition. If
the condition is true,
the result of ? is
returned. But, if the
condition is false, the
result of : is returned.

A. Determine which of the
two numbers is
equal to 0?

B. Determine whether the student will be awarded
with high honor, honor, or achiever.
i. If the grade is above 94, the award is with high honor.
ii. If the grade is equal and above 90 but less than 95, the award is
with honor.
iii. If the grade is below 90,the award is achiever but if the grade is
74 below the student is failed.

C. Determine the largest
of the given three numbers.

B. Calculate the bonus for the employees of an
organization based on the following:
i. If the salary is more than P3,000, the bonus amount is fixed,
and it is equal to P300.
ii. If the salary is more than P1,600, but less than or equal to
P3,000, the bonus will be 10% of the pay subject to a
maximum of P240.
iii. If the salary is less than or equal to P1,600, the bonus is 15% of
pay, subject to a minimum of P100.