You are on page 1of 27

What do you

see in the
pictures?

ROLLER
COASTER

This Photo by Unknown Author is licensed under CC BY-SA


LOOPING
SECOND GRADING
COMPUTER 10
OBJECTIVES
◦ At the end of the lesson, the students are expected to
◦ Describe Iteration and Looping.
◦ Translate the pseudocode into a flowchart and program.
◦ Explain the For Next Loop statement.
◦ Create an application using For Next statement.
◦ Differentiate counter from accumulator.
◦ Use If then structure, looping with Counter and Accumulator
◦ Appreciate the efficiency of using for next in making repetitive tasks
in a program.
ITERATIVE, ITERATION, AND LOOP

◦Using iterations in programming is a good way


to perform specific tasks, solve problems, and
present solutions.
ITERATIVE, ITERATION, AND LOOP
◦ITERATIVE means repetitious
◦In computer programming,
iterative control structure
involves statements in which a
sequence of codes can be
executed repeatedly a number
of times or until a condition is
This Photo by Unknown Author is licensed under CC BY-SA

met.
ITERATIVE, ITERATION, AND LOOP
◦Iteration – refers to the
process of repeating statement/s
in a program. One (1) form of
iteration is a loop. If the
sequence of statements is
executed repeatedly, it is called a
loop. This Photo by Unknown Author is
licensed under CC BY-SA-NC
ITERATIVE FLOWCHART
◦Iterations can be best
illustrated using
flowcharts.

In Iterative flowcharting, a
statement or sequence of
statements is executed
repeatedly as long as a
condition holds True.
EXAMPLE 1: Name
◦ Create a flowchart that asks the user to input a name. The program will
display the name five (5) times.
◦ Pseudocode
1.Initialize counter = 0
2. Input name
3. Display name
4.Increment the value of counter by 1
5. If counter = 5 then
Program ends
Else
Repeat steps 3 to 5
EXAMPLE 1: Name
◦ Create a flowchart that
asks the user to input a
name. The program will
display the name five (5)
times.
◦ FLOWCHART
EXAMPLE 3: Sum of Three Numbers
◦ Create a flowchart that displays the sum of the three(3) numbers
entered by the user.
◦ Pseudocode
1. Initialize counter=0
2. Initialize sum=0
3. Input a number
4. Add the entered number to the value of sum.
5. Increment the value of counter by 1.
6. If counter=3 then
Display sum
Program ends
Else
Repeat steps 3 to 6
FOR NEXT LOOP
◦Hands-on Practice (Counter) (Syntax 1)
1. Create one button on a form, with text caption Counter
2. Double- click the button and type the following:
Dim intNum as Integer
Dim intNum2 as Integer
For intNum =1 to 10 step 1
◦ intNum2=intNum2 +1
◦Msgbox (“value of j is:” & intNum2)
Next intNum
End Sub
FOR NEXT LOOP
❑Syntax 1:
◦ For variable_counter= startNumber to endNumber
◦VB statement/s
◦ Next
❑Syntax 2:
• For variable_counter= startNumber to endNumber step
increment/decrement
◦VB statement/s
◦ Next
FOR NEXT LOOP
1.Hands-on Practice (Counter) (Syntax 2)
1. Create one button on a form, with text caption Counter
2. Using For Next (syntax no. 2), create a program that will
display only the odd numbers from 1-20.
2.Using For Next (syntax no. 2), create a program that
◦will display only the even numbers from 1-20
USING LOOP WITH
CONDITIONAL
STATEMENT(COUNTER
AND ACCUMULATOR)
DAY 2
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
❑Counter is a numeric variable used for counting something such
as the number of employees paid in a week.

❑Accumulator is a numeric variable used for accumulating(adding


together) something – such as the total peso amount of a your
saviings.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
❖Two tasks are associated with counters and accumulators:
initializing and updating
✓ Initializing means to assign a beginning value to the counter, or
accumulator.

✓ Updating also called incrementing, means adding a number to


the value stored in the counter or accumulator.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
◦ A counter is always incremented by a constant value- typically
the number 1
example c = c +1
whereas an accumulator is incremented by a value that varies
example c = c +x
◦ The assignment statement that updates a counter or an
accumulator is placed within the loop in a procedure because the
update task must be performed each time the loop instructions are
processed.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
Seatwork:Activity notebook
1.Write a VB statement that updates the intTotal counter
variable by 2.
2.Write a VB assignment statement that updates the intTotal
counter variable by -3.
3.Write a VB assignment statement that updates the intTotalPurchases
accumulator variable by the value stored in the intPurchases variable.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
4.Write a VB assignment statement that subtracts 100 from
the sngSales accumulator variable.

5.Write a For. . . Next statement that first calculates and then


displays the square of the even numbers from 2 through 12in
the output window.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
◦HANDS-ON PRACTICE
• Do the hands-on activity using For Next with accumulator
and counter.

• Problem 1. Compute for the total allowances of five students


given the following values 50, 100, 45, 75,30.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
◦ HANDS-ON PRACTICUM (Assignment)
• Problem 1. There are two presidential candidates for the
Student Council Melendrez and Azarcon. Make a simple
program whereby the voter can input his choice (M) or (A)
and then the computer will determine who the winner is at
the end. Suppose that there are 6 voters. Use For next with
counter, accumulator and if then statements.
FOR NEXT LOOP WITH COUNTER
AND ACCUMULATOR
◦ HANDS-ON PRACTICUM (Assignment)
• Problem 2. Student 2 Using Inputbox enter the name and
shooting score of 6 applicants for the basketball team. Count
and display the number of
• “Qualified” if it is at least 15points and compute for the total
cost of Uniforms if each uniform costs P120.00. Otherwise, count
and display the number of “Disqualified” if the point earned is
less than 15.
“Why do we need to reflect on the
things that we repeatedly do?”

This Photo by Unknown Author is licensed under CC BY-SA-NC


◦“In iterative programming control structure,
statements are executed repeatedly. In life, we also
do iterations. We are what we repeatedly do.
◦As Frank Outlaw said, “Watch your thoughts, for they
become words.Watch your words, for they become actions.
Watch your actions, for they become habits.Watch your
habits, for they become character.Watch your character, for
it becomes your destiny.”

This Photo by Unknown Author is


licensed under CC BY-SA
Textbook:
• VB. Net Programming Fundamentals
by A. Nicolas, PE Ortiz, and JDL, Caro, Ph.D.

• References:
Simple Program Logic Formulation by Jerald Herrera De La Rosa
• Visual Basic.NET by Diane Zak

• pictures taken from www.google.com

You might also like