/
CS0004:  Introduction to Programming CS0004:  Introduction to Programming

CS0004: Introduction to Programming - PowerPoint Presentation

natalia-silvester
natalia-silvester . @natalia-silvester
Follow
385 views
Uploaded On 2017-05-06

CS0004: Introduction to Programming - PPT Presentation

Repetition For Next Loops Review A loop is used to repeatedly execute a sequence of statements a number of times Each repetition of the loop is called a pass or iteration ID: 545272

for

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS0004: Introduction to Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CS0004: Introduction to Programming

Repetition – For Next

LoopsSlide2

Review

A

loop

is used

to…

repeatedly execute

a sequence of statements

a number of times.

Each repetition of the loop is called

a

pass

, or

iteration

.

Do

Loops come in two forms:

Pretest

and

Posttest

.

Do While loops repeat until the condition is…

False

Do

Until

loops repeat until the condition is…

TrueSlide3

Review

General Form of Pretest:

Do While

condition

statement(s)

Loop

General Form of Posttest:

Do

statement(s)

Loop While

conditionSlide4

For…Next Loops

When we know exactly how many times a loop should be executed, a special type of loop, a

For…Next

loop, can be used.

For instance…

For i As Integer = 1 To 5

MessageBox.show

(i)

Next

…will show the numbers 1 through 5 in message boxes.Slide5

For…Next Loops

We could write the same program with a Do loop

Dim i As Integer = 1

Do While I <= 5

MessageBox.show

(i)

i += 1

Loop

However, this is more verbose and less intuitiveSlide6

For…Next Loops

General Form:

For

i

As

numberDataType

=

m

To

n

statement(s)

Next

i

can be any valid variable name (

i

is fine here though)

i

is a

counter variable

that is incremented (one is added to it) after every iteration of the loop.

numberDataType

is any number data type (most often it is

Integer

)

m

is the

initial value

that

i

takes (

i

is initialized to

m

)

T

he loop runs until

i

>

n

.

n

is called the

terminating value

.

i

does not need to be declared beforehand, because the loop for all intents and purposes declares it. It has block-level scope.Slide7

For…Next Loops

General Form:

For

i

As

numberDataType

=

m

To

n

statement(s)

Next

When a program reaches a For…Next Loop:

The counter variable receives the initial value

It checks to see if the counter variable is greater than the terminating value

If it is, the programs jumps out of the loop

If it is not, the statement(s) inside of the loop execute

The counter variable is incremented

Goes back to step 2.Slide8

For…Next Loops

Set counter variable to initial value

Is counter variable > terminating value

Execute statements within loop

Increment counter variable

Execute statements within loop

Yes

NoSlide9

Counter Variables Declared Outside of Loop

You can declare the counter variable outside of the loop if you want to use it outside of the loop:

Dim i As Integer

For i = 1 To 5

MessageBox.show

(i)

Next

Does the same thing as…

For i As Integer = 1 To 5

MessageBox.show

(i)

NextSlide10

For…Next Example 1

New Topics:

For…Next LoopSlide11

Step Values

By default For…Next loops increment the counter variable by 1 every iteration of the loop.

You can also define how much is added to the counter variable every iteration. This amount is called a

step value

.

For i As Integer = 1 To

11 step 5

MessageBox.show

(i)

Next

This loop will display 1, 6, and then 11 in separate dialog boxes.

General Form:

For

i

As

numberDataType

=

m

To

n

Step s

statement(s)NextWhere

s is the step value.Slide12

For…Next Loop Examples 2, 3, 4

New Topic:

Step Value

Negative initial values, and step values

Nested For LoopsSlide13

For…Next Notes:

If you want to skip an iteration of a For…Next Loop you can use the

Continue For

statement (there is also a

Continue Do

statement)

When

Continue For

is encountered, the loop then skips to the next iteration without executing the code below it in the current iteration.

Likewise, much like a

Exit Do

, there is also an

Exit For

.

Again, counter variables have block-level scope in the for loop they are used in.

Any decision statements or repetition statements are followed by

statement(s)

. These are called

blocks

. Any blocks can be nested in any other blocks (If statements can be nested in for loops, etc.)Slide14

Type Inference

As we write our programs now, this statement is legal:

Dim

var

= 1

Question: How does the compiler know what type

var

is?

Answer: It infers the type from the value it is initially given.

The compiler gives

var

the type

Integer

because

1

is of type

Integer

.

This can be dangerous for beginning programmers:

Dim

var = 2var

= 2 * 4.5This will result in an error, because even though we may have wanted var

to be of type Double, but we did not control what type it was given initially, and it was inferred to by of type Integer

.To disallow this type inference (implicit typing

, or duck typing) add Option Infer Off to the top of the

program.