Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
138 views

Programming Languages (Css 2) : Number of Sessions (Time Allotment) : 60 Hours (2nd Quarter)

The document discusses programming language concepts including: - Conditional statements which use logical and relational operators to control program flow. - Looping constructs which allow code to be repeated. - Examples of single and dual alternative selection structures for conditional logic. - How multiple decision boxes can be combined or used separately to handle different conditional outcomes.

Uploaded by

Rodney Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Programming Languages (Css 2) : Number of Sessions (Time Allotment) : 60 Hours (2nd Quarter)

The document discusses programming language concepts including: - Conditional statements which use logical and relational operators to control program flow. - Looping constructs which allow code to be repeated. - Examples of single and dual alternative selection structures for conditional logic. - How multiple decision boxes can be combined or used separately to handle different conditional outcomes.

Uploaded by

Rodney Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

K to 12 Basic Education Curriculum

Technical Vocational Livelihood Education

PROGRAMMING LANGUAGES (CSS 2)


Number of Sessions (Time Allotment): 60 Hours (2nd Quarter)

LOGIC FORMULATION
SUMMARY OF LEARNING OUTCOMES (LO):
➢ LO 01: Conditional Statements
➢ LO 02: Looping Constructs
TANAUAN INSTITUTE, INC.
Senior High School Department
Modified Learning Scheme: Workbook
PROGRAMMING LANGUAGES (CSS 2)
1st Semester, S.Y. 2020-2021
Subject Teacher: LORIE A. AVILLANOZA

Name: ___________________________________ Score: ________________


Section: __________________________________ Date: _________________

Module 05: LOGIC FORMULATION

Topic : Lesson 01: Conditional Statements Session :


Lesson 02: Looping Constructs WEEK 06 & 07
8 hrs.

Objectives of the Lesson :


At the end of the lesson, the student should be able to:
• Identify the steps in creating a program,
• Describe the two categories of conditional statements, and
• Identify the different looping constructs.

Values Integration:

Nowadays, PROGRAMMING is already a large part of daily life-from games in gadgets to mainstream movies,
they are anywhere, However, a few decades ago, program is very limited. Programming came a long way before
getting to gadgets, televisions and big screens. Explain problem definition, problem analysis, algorithm, and
coding and debugging.
• Identify and explain conditions and Boolean value
• Enumerate and discuss the logical operators

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 1


• Define and explain Single Alternative Selection Structure and Dual Alternative Selection Structure.
• Illustrate Multiple Decision Boxes and explain the example provided.

Questions
1. What are the Importance of CONDITIONAL STATEMENTS?

2. What are the major contributions of LOGICAL CONTRUCTS Nowadays?

3. What are some rules that you think must be followed when creating a LOGICAL
OPERATORS?

Discussion :

Lesson 01: CONDITIONAL STATEMENTS

Conditions are statements that result in a Boolean value. A Boolean value may either be TRUE or
FALSE. Conditions are logical expressions. Logical expressions evaluate to a Boolean value. There are
two types of operators which provide Boolean values as results – logical and relational.

Logical Operators are mainly used to control program flow. They are usually part of an IF, WHILE, or
some other control statements. Logical operators allow a program to make a decision based on multiple
conditions.

Relational Operators are used to compare two operands and determine the validity of a relationship.

Conditions:
✓ are statements that result to a Boolean value.

Boolean value
✓ may either be a TRUE or FALSE, 0 or 1 value
✓ Operators which give Boolean value as results:
- Relational Operators
- Logical Operators

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 2


Basic Logical Operators: Logical
Operators

AND OR

NOT
The NOT truth table shows the nature of the NOT operator. A truth table is a type of mathematical table
used in logic to determine whether an expression is true or valid. Such a table typically contains several
rows and columns with the top row representing the logical variables and combinations, in increasing
complexity leading up to the final function.

Basically, the NOT operator operates on a single value and the result is the reverse of the operand
Boolean value. Therefore, a TRUE will become FALSE and a FALSE will become TRUE. Note that in
flowcharting, TRUE may be represented by T or 1 and FALSE may be represented by F or 0.

CONDITIONAL STATEMENTS
• NOT operator
• reverses the logic or result of a certain condition

Condition A !A

F T

T F

The AND and OR operators are commonly used operators in conditions. The AND operator is used if
you want a strict condition. For AND to result to TRUE, both conditions must be satisfied. The OR
operator is used when either one of the conditions is true.

The truth table shows how the AND and OR operators function. For the AND operator, the result will
become TRUE if and only if both conditions are TRUE, while the OR operator becomes TRUE if either
one or both conditions are TRUE.

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 3


AND/OR truth table

Condition a Condition b a AND b a OR b

F F F F

F T F T

T F F T

T T T T

Relational Operators
Compare two values and return a Boolean value depending on whether the test condition is true or false.

• Compare two values and returns a Boolean value depending on whether the test condition is true or
false.

< Less than


> Greater than
<= Less than or equal to
>= Greater than or equal to
== Is equal to
!= Not equal to
<> Not equal to

Assume you have variables x and y. What would be the


results of the conditions if we were to compare them with
different values and different relational operator?
x<y tests if x is less than y
x>y tests if x is greater than y
x <= y tests if x is less than OR equal to y
x >= y tests if x is greater than OR equal to y
x == y tests if x is equal to y
x != y tests if x is not equal to y
x <> y tests if x is not equal to y

Conditions inside a decision box must:


• Either result to TRUE or FALSE
• Be represented in a mathematical form

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 4


Is variable X

X < 20? less than


20?

It has been discussed previously that that a diamond box is used for decision making within a flowchart.
It should be noted that what you place inside the diamond box are conditions – conditions that either
result to TRUE or FALSE.

Also, you don’t place conditions that are in essay form. You must be able to represent it in a
mathematical form.

• Single Alternative Selection Structure


A decision box may not necessarily have to do something for both the TRUE path and the FALSE path.

T F

• Dual Alternative Selection Structure


This performs two different things when the condition is TRUE or FALSE.

T F

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 5


A decision box may not necessarily have to do something for both the TRUE path and the FALSE
path. This kind of selection structure is called the Single Alternative Selection Structure.

The Dual Alternative Selection Structure allows you to perform two different tasks depending on the
result of the condition’s evaluation.
One path is performed if the evaluation resulted to TRUE and the other if it is FALSE.

Example: if x < y, print x, otherwise print y

Multiple Decision Boxes

• Multiple decision boxes can oftentimes be combined into a


Start single box. There are times, however, when you have to be
very specific with the conditions that you have to put in.
This is especially true in cases when different actions need
to be performed depending on various conditions.
x=0
• This flowchart will ask the user to enter a number from 1 to 3.
If 1 is entered, the string "Hello" will be displayed. If 2 is
entered, the string "Hi" will be displayed. If 3 is entered, the
Get x string "Bye" will be displayed. Otherwise, the string
"Invalid" will be displayed.
No Yes
x == 1? Disp “Hello”

No Yes

x == 2? Disp “Hi”
Stop
No Yes

Disp “Bye”
x == 3?

Disp “Invalid”

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 6


Using Decision Box with Ranges
Example: Create a flowchart that would print the name of the supervisor under a certain
department number.

Department Number Supervisor


1-3 Mr. X
4-7 Mr. Y
8-9 Mr. Z

Solution #1:

• Business programs often need to make selections based on


Start a variable falling within a range of values. To perform a
range check, a sequence of decision blocks starting with
either the lowest or highest value in each range of values are
used to make your selections.
Dept = 0

Get Dept

Yes
Dept< = 3? Disp “Mr. X”

No Yes

Dept <= 7? Disp “Mr. Y”


Stop

No
Disp “Mr. Z”

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 7


Activity :

ACTIVITY 1:
Directions: Draw a flowchart that will ask the user to enter a character (either ‘M’ or ‘F’) to indicate the user’s
gender (Male or Female).
• Display “Male” if the user enters the value of ‘M’ and “Female” for the value of ‘F’
• Assume all inputs are correct

ACTIVITY 2:
Directions: Draw a flowchart that will ask the user to input the values of four variables, A, B, C, and D
• Print “TRUE” if A is greater than B, and C is less than D or if A is greater than B and C is less than D
• Print “FALSE” if otherwise.

Lesson 02: LOOPING CONTRUCTS


A loop is a set of program instructions that executes repeatedly until a condition is satisfied. The chart
below shows the basics of looping, the nature of a loop and how a loop works.

A loop can be executed and/or stopped. Running and stopping the loop is dependent on the condition. If
you want the loop to run, the condition must be TRUE. If you want the loop to stop, the condition must
be FALSE. The condition for it to become TRUE or FALSE must be dependent on a variable.

For instance, 5 < 8 will always be TRUE; however, using x < 8 the result will be dependent on the value
of x. In programming, the value of the variable can be changed in three basic ways:
1. Through mathematical computations (ex: Set x to x +
3 or Increment x by 3)
2. user input (enter x)
3. by initialization or passing of values (ex: x = 4, y = x)

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 8


Repetition Structure
• Also referred to as a loop or iteration
• Repeats one or more instructions until a certain condition is met.

Yes
X>Y? Process A
No

The repetition structure, also referred to as a loop or iteration, is a type of structure that repeats one or
more instructions until a certain condition is met.

There are various ways of setting up a loop depending on the needs of the program. The action
performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite
loop is created.

Take note of the use of the diamond symbol. One possible loop can test a condition, perform an action if
the condition is satisfied, and then test the condition again. The action is repeated as long as the
condition is satisfied.

Looping Constructs
In the flowchart segment in the slide, the question “is X < Y?” is asked. If the answer is yes, then
Process A is performed and the question is asked again. Process A is repeated as long as X is less than
Y. When X is no longer less than Y, the repetition stops and the structure is exited.

In this flowchart segment, X is never changed. Once the loop

Yes
X < Y? Display X
No

Yes
X < Y? Display X X=X+1
No

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 9


Pre-test repetition structure
• The condition is tested BEFORE any actions are performed
• if the condition does not exist, the loop will never begin and the actions inside the loop will not be
executed.

a pre-test repetition structure where the condition is tested BEFORE any actions are performed.
In this structure, if the condition does not exist, the loop will never begin and the actions inside the
loop will not be executed.

Post-test repetition structure


• The condition is tested AFTER the actions are performed
• always performs its actions at least once

Display X

X=X+1

Yes
X < Y?

No
This flowchart segment shows a post-test repetition structure wherein the condition is tested AFTER the
actions are performed. This type of structure always performs its actions at least once.

• Create a flowchart that will display Start


the following result that the variable
is initialized to 5.

Output: X=5
5
4
3 Yes
2 X> Disp X
1
No 0?
DONE

Disp “Done” X=X-1

Stop

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 10


• Create a flowchart that will print a series of
numbers based on what was entered by the Start
user.

X, Y

Get X, Y

Yes
X<Y X=X+1

No
Disp X
Disp “Done”

Stop

LOOPING CONSTRUCTS – MULTIPLE LOOPS


It is possible to have a loop within a loop. In case of multiple loops, it is critical to determine what
condition will terminate the whole flowchart.
Multiple loops are useful if there is a need to perform repetitive tasks.
One use of multiple loops is when there is a need to perform user input validations.

• are useful if there is a need to perform


repetitive tasks.
• when there is a need to perform user input
validations.

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 11


LOOPING CONSTRUCTS – MULTIPLE LOOPS

Start

Gen=“”
Ctr=1

No
Disp Ctr<=5

‘Invalid’
Yes
Stop
Get Gen

Yes
Gen!=‘M’
AND

Gen!=‘F’
No

Yes No
Gen==‘M’

Disp ‘Male’ Disp ‘Female’

Ctr=Ctr+1

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 12


Activity :

ACTIVITY 1: Flowcharting
At the end of this exercise the students are expected to:
• Create a flowchart based on the given problem.
Materials:
Flash Drive
Note: Save all your flowcharts in your flash disk. Use appropriate file names for each
flowchart drawing.
Directions: Create the flowchart of the following problems:
1. Display the user’s name, birthday, age, gender, and phone number.
The flowchart should display the variables with the following format:
Name: ??????
Age: ??
Birthday: mm/dd/yyyy
Gender: ?
Phone#: ???????

2. Compute the Average score of a student based on three quizzes.


The quiz scores are entered by the user. The scores are integers
and may range from 0 to 100, inclusive. However, the Average may
have a value having decimal places.
For example:
Q1: 98 Q2: 79
Q3: 88 Ave is 83.33

ACTIVITY 2: Flowcharting
Directions: Create a flowchart that displays the student’s average score for three quizzes.
• Assume that there are 3 sections each having 5 students.
• The only valid number to be entered is 1 – 100 for the quizzes.
• Should the user enter an invalid number, display an appropriate error message and ask the user reenter
a value for the 3 quizzes.

References :

• Department of Technology Education at The University of Southern


Mississippi. (2007, February 26). Retrieved from
Dragon.ep.usm.edu/~it365/module/Basics/system.htm

• Hoffer J., George G., & Valacich J. (2008). In 5 Ed., Modern systems
analysis and design. Pearson Prentice Hall.

• Parsons J. J., & Oja D. (2010). In 8 Ed., Computer concepts Illustrated


Introductory. Cengage Learning.

PROGRAMMING LANGUAGES (CSS 2) / LORIE AVILLANOZA 13

You might also like