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...
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 and comparisons in C++. Each type of operator will be discussed with practical examples and real-life analogies to ensure understanding.
Additionally, the lesson introduces user input handling using the cin object from the iostream library. Students will learn how to capture different data types from the user, understand input flow, and troubleshoot common issues related to whitespace and input buffer behavior.
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.