33
Iteration Control Structure Fourth Quarter

Iteration Control Structure

  • Upload
    sunila

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Iteration Control Structure. Fourth Quarter. Fundamental Concept. Involves loops or cycles Loops : means that a process may be repeated as long as certain condition remains true or remains false. Means “ multiple processing ” - PowerPoint PPT Presentation

Citation preview

Page 1: Iteration Control Structure

Iteration Control Structure

Fourth Quarter

Page 2: Iteration Control Structure

Involves loops or cycles◦Loops: means that a process may be repeated as long as certain condition remains true or remains false.

◦Means “multiple processing”Provides means of repeating a part of instruction without rewriting the part again and again.

Fundamental Concept

Page 3: Iteration Control Structure

Body of the Loop : Set of statements which is repeated.

Loop Exit Condition: condition to be tested before each repetition.

Parts of the Loop

Page 4: Iteration Control Structure

Start : the starting or the beginning point of the loop.

One may start with first or the last record in the file.

Parts of the Loop: 3 S

Page 5: Iteration Control Structure

Step : the manner on how the records are to be processed in the given file. Proper sequencing is needed (ascending or descending).

Page 6: Iteration Control Structure

Stop : the ending point . It is normally represented in a form of a conditional expression, thus, a decision symbol is used in the flowchart

Page 7: Iteration Control Structure

Is a variable that defines the starting, ending point and step of a looping statement.

It should be a variable that will uniquely identify all the records in the file.

It is a representation of a field in the record. The shorter the value, the better is the field

Control Variable

Page 8: Iteration Control Structure

Draw a flowchart that will read the grades in PT, CS and QS of student. Compute the average of the student. Print the name and the computed average.

Input : Process: Output:

Will there be multiple processing? How many times the inputting, processing and outputting will be performed? WHY?

Problem No. 1

Page 9: Iteration Control Structure

Draw a flowchart that will read the grades in PT, CS and QS of all the students in the class of II - ____. Compute the average of all the students. Print the names and the computed averages.

Input : Process: Output:

Will there be multiple processing? How many times the inputting, processing and outputting will be performed? WHY?

Problem No. 2

Page 10: Iteration Control Structure

Are used to literally count the number of times a portion of the flowchart is traced.

Need to be initialized / prepared prior to its use or application.

The operation involved is “addition” The increment value is a constant Example: C = C + 1

Increment Value Current Value New Value

Counters

Page 11: Iteration Control Structure

A numerical value that collects the result of a repeated mathematical operation.

Used to keep a running total of an item / the operation involved is “addition”.

Need to be initialized / prepared prior to its use or application.

The incremental value is a “variable” (subjected to change)

Example: S = S + N

Increment Value Current ValueNew Value

Accumulators

Page 12: Iteration Control Structure

Executes a group of instructions repeatedly

Has 3 Structures:◦For ……. Next◦Do …….. Loop : has three types:

Do While …. Loop Do Until …. Loop Do …… Loop while

◦While ….. Wend

Looping Statements

Page 13: Iteration Control Structure

Executes a section of the code a specified number of times.

Begins with the “for” statement and ends with the “next” statement.

Can only be used if the programmer knows the number of times the loop must be performed prior to its execution.

For …. Next Statement

Page 14: Iteration Control Structure

For <counter > = <Start> to <Stop> Step <Step>

<Statements>

Next <counter>

For … Next Syntax 1(no skipping)

Page 15: Iteration Control Structure

Dim UserName as stringDim times as integerUsername = “Paul”Lbloutput.text = “ ”For times = 1 to 10 step 1Lbloutput.text = lbloutput.text & chr(13) & Username

Next times

Example:

Page 16: Iteration Control Structure

Dim UserName as stringDim times as integerUsername = “Paul”Lbloutput.text = “ ”For times = 1 to 10 Lbloutput.text = lbloutput.text & chr(13) & Username

Next

Example:

Page 17: Iteration Control Structure

For <counter > = <Start> to <Stop> Step <Step>

<Statements>

Next <counter>

For … Next Syntax 2(with skipping/decrement)

Page 18: Iteration Control Structure

Dim counter as integerLbloutput.text = “”For counter = 10 to 1 step -1 lbloutput.text = lbloutput.text & chr(13) & counterNext

Example 1

Page 19: Iteration Control Structure

Dim counter as integerLbloutput.text = “”For counter = 9 to 1 step -2 lbloutput.text = lbloutput.text & chr(13) & counterNext

Example 2

Page 20: Iteration Control Structure

Most common statement among the do ..loop statements

Test condition appears at the TOP of the loop◦As long as the test condition is TRUE,

the block of code in the body of the loop will be continuously executed

◦When the test condition becomes FALSE, the loop terminates.

Condition before iteration

Do while ….. Loop

Page 21: Iteration Control Structure

Do while <condition><statement><statement>

Loop

Syntax: Do while …. Loop

Page 22: Iteration Control Structure

Private Sub Command1_Click( )Dim N As Integer

N = 0Do While N < 10

N = N + 1Print N

LoopEnd Sub

Example:

Page 23: Iteration Control Structure

Example:Private Sub Command1_Click( )Dim N As Integer

N = 0Do While N < 10

N = N + 1Print N

LoopEnd Sub

Loop Statement

Page 24: Iteration Control Structure

The test condition appears at the bottom of the loop.

When the test condition stays TRUE, the loop still executes until it becomes false

Condition after iteration

Do ….. Loop While

Page 25: Iteration Control Structure

Do< statement >< statement >< statement >

Loop while < condition >

Do … Loop While Syntax

Page 26: Iteration Control Structure

Private Sub Command1_Click( )Dim N As Integer

N = 0Do

N = N + 1Print N

Loop While N < 10End Sub

Example:

Page 27: Iteration Control Structure

Test condition also appears at the TOP of the LOOP.

Executes the block of statements as long as the test condition is FALSE

Do Until …. Loop

Page 28: Iteration Control Structure

Do until < condition >< statement >< statement >

Loop

Do Until … Loop Syntax

Page 29: Iteration Control Structure

Private Sub Command1_Click( )Dim N As Integer

N = 0Do Until N >= 10

N = N + 1Print N

LoopEnd Sub

Example:

Page 30: Iteration Control Structure

DO WHILE / DO UNTIL

Display numbers from

1 to 10

Do While N < 10N = N + 1Print N

Body of Loop will be executed as long as the condition is TRUE.

Display numbers from 1 to 10

Do Until N >= 10N = N + 1Print N

Body of Loop will be executed as long as the condition is FALSE.

Page 31: Iteration Control Structure

Hands – On Session

Page 32: Iteration Control Structure

a. Make a program that will display this output on the form:

b. Make a program that will display “Patience is a Virtue” 5 times.

Activity 1 & 2

Page 33: Iteration Control Structure

Filenames:◦Activity No.1

4Act12CN◦Activity No.2

4Act22CN