You are on page 1of 46

SPECIALIZED TRAINING WORKSHOP IN ICT SEASON 4/ MAY 25-27, 2020

Apply Looping Structure in a Program

GLENDA D. BALMELERO
SHS Teacher III, Exequiel R. Lina High School
Facilitator
Objectives
Terminal Objectives:
• At the end of the session, participants should be able to apply looping
structure in a program

Enabling Objectives:
• Explore the various types of looping structure in a program
• Create a program that will continue to run based on the given
condition/s
• Appreciate the value of using loop in automating repetitive tasks

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


What is Loop?
• A loop statement allows us to execute a statement or group of statements
multiple times.

If the Condition is FALSE


Check
Condition Exit Loop

If the Condition is TRUE

Execute
Statement Block

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Components of Loop
A loop usually has the following components:
• initialization of a variable or of several variables
• condition (that would evaluate to either true or false)
• body (which maybe a single statement or a group of statements)
• a change of state which is usually a statement inside the body of the
loop that changes the content of the variable(s).

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


VB.Net provides following types of loops:
• While... End While
• For...Next
• For Each...Next
• Do Loop
• With... End With
• Nested loops

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop While condition
statements…
End While
• It executes a series of statements as If the Condition is FALSE
long as a given condition is True. Check
Condition Exit Loop
The syntax for this loop construct is −
If the Condition is TRUE
While condition
[ statements ] Execute
[ Continue While ] Conditional code

[ statements ]
[ Exit While ]
[ statements ]
End While

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop
Example 1: Create a program that will output numbers from 1 to 5.

ctr = 1 2 3 4 5 6

Initialization
Condition
Body of loop Counter is 1
Counter is 2
Change of state
Counter is 3
Counter is 4
Counter is 5

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop
Example 2: Create a program that will output numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop
Example 3: Create a program that will output odd numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop
Example 4: Create a program that will output even numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While-End While Loop
Example 5: Create a program that will output numbers from 100 down to 1.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
• It repeats a group of statements a specified number of times and a
loop index counts the number of loop iterations as the loop executes.
• The syntax for this loop construct is −

For counter [ As datatype ] = start To end [ Step step ]


[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For counter=start to end
statements…
For-Next Loop Next counter initialization

If the Condition is FALSE


Check
Condition Exit Loop

If the Condition is TRUE

Execute
Conditional code

increment

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
Example 1: Create a program that will output numbers from 1 to 5 using For-Next
loop.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
Example 2: Create a program that will output numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
Example 3:
Create a program that will output odd numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
Example 4: Create a program that will output even numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


For-Next Loop
Example 5: Create a program that will output numbers from 100 down to 1.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
• It repeats the enclosed block of statements while a Boolean
condition is True or until the condition becomes True.
• It could be terminated at any time with the Exit Do
statement.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop Do
statements…
While (condition)
• The syntax for this loop construct is
Execute
Conditional code
Do
[ statements ] If the Condition
is TRUE
[ Continue Do ]
[ statements ] Check
[ Exit Do ] Condition
[ statements ]
Loop{While | Until}condition If the Condition is FALSE

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
Example 1: Create a program that will output numbers from 1 to 5 using Do Loop.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
Example 2: Create a program that will output numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
Example 3:
Create a program that will output odd numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
Example 4: Create a program that will output even numbers from 1 to100.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Do Loop
Example 5: Create a program that will output numbers from 100 down to 1.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


While, For, Do loop Comparison

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Each-Next Loop

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


With-End With Statement
• It is not exactly a looping construct.
• It executes a series of statements that repeatedly refers to a single
object or structure.
• The syntax for this loop construct is −

With object
[ statements ]
End With

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
• VB.Net allows using one loop inside another loop
• The syntax for a nested While loop statement in VB.Net is as follows −

While condition1
While condition2
...
End While
End While

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
Example: Use a nested while loop to construct the 10 x 10 multiplication table

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
The syntax for a nested For loop statement in VB.Net is as follows −

For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ]


For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ]
...
Next [ counter2 ]
Next [ counter 1]

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
Example: Use a nested for loop to construct the 10 x 10 multiplication table

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
• The syntax for a nested Do...While loop statement in VB.Net is as follows −

Do { While | Until } condition1


Do { While | Until } condition2
...
Loop
Loop

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Nested Loop
Example: Use a nested Do loop to construct the 10 x 10 multiplication table

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Loop Control Statement
• Loop control statements change execution from its normal sequence.
• When execution leaves a scope, all automatic objects that were
created in that scope are destroyed.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Loop Control Statement
VB.Net provides the following control statements:
• Exit statement
• Continue statement
• GoTo statement

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Exit Statement
• Terminates the loop or select case
statement and transfers execution
to the statement immediately
following the loop or select case.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Exit Statement

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Continue Statement
• Causes the loop to skip the remainder of
its body and immediately retest its
condition prior to reiterating.

Continue { Do | For | While }

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Continue Statement

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


GoTo Statement
• Transfers control to the labeled
statement. Though it is not advised
to use GoTo statement in your
program.

GoTo label

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


GoTo Statement

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Evaluation
1. Write a program that will display the even numbers from 1 to 100.

2. Write a program that will ask the user to enter a number n and display
the numbers from 1 to n.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Evaluation
1. Write a program that will display the even numbers from 1 to 100.

2. Write a program that will ask the user to enter a number n and display
the numbers from 1 to n.

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS


Evaluation
3. Write a program to list the numbers from 0 to 25, their squares, and the
fourth power. The output should be in a neat 3-column format.
 
Number Square 4th power
0 0 0
1 1 1
. . .
. . .
25 625 390625
ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS
Thank you

GLENDA D. BALMELERO
SHS Teacher III
Exequiel R. Lina High School
0930.554.1193
glenda.balmelero@deped.gov.ph

ACTIVITY-BASED TRAINING WITH ACTIVITY CARDS

You might also like