Looping Structure

MELJUN CORTES
Rommel Dorin

Testing Data

Keypress

Explained
KeyChar property to determine the pressed key. (KeyChar stands for "key character.") You then use the e parameter's Handled property to cancel the pressed key if it is an inappropriate one. Example shows using the KeyChar and Handled properties in the KeyPress event procedure. Notice that you refer to the Backspace key on a computer keyboard using the ControlChars.Back constant. The Backspace key is necessary for editing the text box entry.

Trim

Looping
• The condition is evaluated with each repetition (or iteration) of the loop and can be phrased in one of two ways: It can specify either the requirement for repeating the instructions or the requirement for not repeating them .

Looping
• The requirement for repeating the instructions is referred to as the looping condition, because it indicates when the computer should continue “looping” through the instructions. • The requirement for not repeating the instructions is referred to as the loop exit condition, because it tells the computer when to exit (or stop) the loop.

Looping
• A repetition structure can be either a pretest loop or a posttest loop. • In a pretest loop, the loop condition is evaluated before the instructions within the loop are processed. • In a posttest loop, the evaluation occurs after the instructions within the loop are processed. Depending on the result of the evaluation, the instructions in a pretest loop may never be processed.

Do…Loop

Do…Loop

Do…Loop

Do…Loop

Loop Structure
• A loop is a series of commands that gets repeated. • Types: a.For . . . Next Loops b.Do While Loops c.Do Loop While d.Do Until Loops

For Next Loop
• For . . . Next loops are usually used when you know the number of times you want to repeat a loop. The top of the loop is marked with a For statement and Next marks the end of the loop. For loops use a counter to keep track of times it runs.

For Next Loop

For Next Loop

Counting Loops

For…Next Loop

For…Next Loop Flowchart

Step Parameter
• The Step parameter determines how fast you move through a loop. • For loops can use positive and negative numbers, and they can also change by decimal values.

Loop Control Variables •The numeric variable that controls a For . . . Next loop increments until it’s larger than its limit. (If it steps backward, it decrements until it’s smaller than its limit.)

Do While Loops
• For . . . Next loop is controlled by a numeric variable • While loop can be controlled with a string, a Boolean, or a numeric variable.

Do While Loops
• While loops are set up with a condition and run while that condition is true. There must a line of code inside the loop that can change that condition.

Do While Loops
Syntax:

Counting Loops
• A While loop can mimic a For . . . Next loop. It can be initialized to any value, increment or decrement as needed, and end when a specified value is reached.

Counting Loops
i=1 Do While i<= 5 rtbOut.AppendText (i.ToString & vbNewLine) i = i+1 Loop

Do Loop While

Sentinel Values
• Sentinel values are set with each trip through the loop. • As soon as that value makes the condition False, the loop ends.

Do While – Example 1

Do Until Loops
Do While loops run while their condition is True. Do Until Loops, as the name implies, run until the condition becomes True. In other words, Until loops run as long as their condition is False.

Do Until Loops
Syntax:

Do Loop While
There’s a variation on the While loop. Instead of putting the condition in the Do line, the While condition is placed in the Loop line.

Exit Statement

Skipping to the Next Loop
• The Continue statement transfers control to the last statement in the loop and retests the loop exit condition. This effectively skips to the next iteration of the loop. Generally, the Continue statement is part of an If statement. General Form:

Example

Activity
• Depreciation Program

• Formula:

Activity

MACHINE PROBLEM

Rnd
• Rnd is short for random. It’s used to generate a random number. Random numbers are great. • Rnd generates a random number greater or equal to 0 and less than 1. The range is 0 to .999999. • Syntax:

ID Random Generator

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times