0% found this document useful (0 votes)
17 views16 pages

Hi Hello Good Bye

Uploaded by

misskonasiandre
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)
17 views16 pages

Hi Hello Good Bye

Uploaded by

misskonasiandre
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/ 16

UNIT 1

Introduction to Programming

CC 103Computer Programming 1
LESSON
3 Program Structures

CC 103Computer Programming 1
LESSON
3 Program Control Structures

Program Control Structures are just a way to specify flow of control in


programs. Any algorithm or program can be more clear and understood if
they use self-contained modules called as logic or control structures. It
basically analyzes and chooses in which direction a program flows based on
certain parameters or conditions.
Basic types of logic, or flow of control:

• Sequence logic, or sequential flow


• Selection logic, or conditional flow
• Iteration logic, or repetitive flow
• Modular

CC 103Computer Programming 1
LESSON
3 Program Control Structures

• Sequential

Steps are performed in strictly sequential manner, each step being


executed exactly once.

CC 103Computer Programming 1
LESSON
3 Program Control Structures
Examples: Sequential
Problem 1: sum computation Problem 2: average computation Problem 3: bonus computation

CC 103Computer Programming 1
LESSON
3 Program Control Structures

• Selection/Conditional
The execution of instructions by selection, wherein conditions for a series of
alternative statements are evaluated to specify which instruction is to be
executed. In this way, the flow of the program depends on the set of
conditions that are written.

CC 103Computer Programming 1
LESSON
3 Program Control Structures
• Selection/Conditional
In this way, the flow of the program depends on the set of conditions that are
written.
Algorithm : Flowchart :

if (Condition) then No/ False

Process A Condition
Process B
else
Yes /True
Process B

Process A

Condition or conditional expression is in the form of a mathematical statement using


equals, less-than, or greater-than.

CC 103Computer Programming 1
LESSON
3 • Selection/Conditional
start
Problem 4:
Determine if the grade of a student is
"passed" or failed," considered 75 as
the passing grade. grade = 0

Algorithm :

Input grade
Input grade
if (grade >=75) then
print”passed”
F
else print “Failed”
grade >= 75 1
print “failed”

print “Passed”

1
end CC 103Computer Programming 1
LESSON
3 • Selection/Conditional
start
Let h=hours worked ; r=rate per hour; b=bonus;
s=salary
Problem 5: h=0;r=0;
Compute the bonus of an b=0;s=0;
employee. Consider the following
criteria: If the employee’s salary is
greater than or equal to 10000 Input h,r
pesos, the bonus is 50% of the
salary; else, the bonus is 25% of
s=h * r 1
the salary. Salary is hours worked
times rate per hour.
F
s >= 10000 b= s*.25 Output b end

b= s*.50

1
CC 103Computer Programming 1
LESSON
3 • Selection/Conditional
start

h=0;r=0;
b=0;s=0;
Problem 6:
Compute the bonus of an
Input h,r
employee. Consider the following
criteria: If the employee’s salary is
less than 10,000 pesos the bonus
s=h * r 1
is 25% of the salary; for employees
with salaries greater than to
10,000 pesos, the bonus is 50% of T
the salary ;and for salaries equal s < 10000 b= s*.25 Output b end
to 10,000 pesos, bonus is 35% of
salary. Salary is hours worked F

times rate per hour.


T
s > 10000 b= s*.50 1

b= s*.35 1
CC 103Computer Programming 1
LESSON
3 Program Control Structures
• Repetition/ Iteration /Loop

Repetitive /Iterative statements (loops) allow a set of instructions to be


repeated or carried out several times until certain conditions have been
met.

Two types: (1)count-controlled (2) condition - controlled

CC 103Computer Programming 1
LESSON
3 Program Control Structures
• Repetition/ Iteration /Loop
The way the count-controlled loop works is simple: the loop
Count-controlled loop holds a count of the number of times it runs, and the loop
stops when the count reaches the specified count.
Initialization of
This loop normally performs the following three counter (control
actions: variable)

Initialization : The control variable (counter) is


initialized to the starting value before the loop False

starts. Condition
END

Test : the loop terminating condition that True

checks if looping should continue body of the loop

Increment (or decrement)value by which the


control variable is modified at each iteration of Increment/
Decrement
the loop.

CC 103Computer Programming 1
LESSON
3 Program Control Structures
• Repetition/ Iteration /Loop

Condition-controlled loop Initialization of


control variable

A condition-controlled loop is used when it is not False


known how many times iteration will need to Condition
END
occur.
True

body of the loop

CC 103Computer Programming 1
LESSON
3 • Repetition/ Iteration /Loop

Problem 7:
Create a flowchart that accepts number 5 times

Problem 8:
Create a flowchart that accepts numbers and stops when the user
enters 0.

CC 103Computer Programming 1
LESSON
3 Program Control Structures

• Modular/Procedural
The approach is sometimes called divide and conquer
because a large task is divided into several smaller tasks
that are easily performed.

Predefined Process

CC 103Computer Programming 1
LESSON
3 Program Control Structures

• Modular/Procedural

CC 103Computer Programming 1

You might also like