for…next : nested loop x= 0 for i = 0 to 4 for j = 0 to i-1 x=x+(i+j-1) next j next i how many...

11
For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of this loop?

Upload: pauline-anthony

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

For…Next : Nested Loop

X= 0For i = 0 To 4

For j = 0 To i-1X=X+(i+j-1)

Next jNext i

How many times the inner loop is executed?

What is x at the end of this loop?

Page 2: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Nested For…Next : Executioni=0j=0-1

i=1j=00

i=2j=01

i=3j=02

i=4j=03

No. of times inner loop is executed

0 1 2 3 4

x 0 0 13

5812

15192430

Page 3: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

For…Next : Exit

X= 0For i = 0 To 4

For j = 0 To i-1

If x > 5 ThenExit For

End Ifx=x+(i+j-1)

Next jNext I

Exit from the inner loop

Page 4: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Do…Loop

Do While logical expression

Executable statement(s)Loop

Do

Executable statement(s)Loop Until logical expression

Page 5: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Do While…Loop

Expression

Statement(s)True

False

Page 6: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Do…Loop Until…

Statement(s)

Expression

True

False

Page 7: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Do Loops

Sum = 0Kount = 0

Do While Kount <= 10Sum = Sum + KountKount = Kount + 1

Loop

Sum = 55

Kount = 11

Sum = 0Kount = 0

DoSum = Sum + KountKount = Kount + 1

Loop Until Kount > 10

Sum = 55

Kount = 11

Page 8: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Decimal to Binary : Do While

Private Sub Command1_Click()Dim Deci As IntegerDim R As Byte, Bin As String

Deci = Text1.TextBin = ""Picture1.Cls

If Deci = 0 Then Bin = Str(Deci)End If

Do While Deci > 0 R = Deci Mod 2 Bin = Str(R) + Bin Deci = Deci \ 2Loop

Picture1.Print Bin

End Sub

Page 9: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

Decimal To Binary: Do…Until

Private Sub Command3_Click()Dim Deci As IntegerDim R As Byte, Bin As String

Deci = Text1.TextBin = ""Picture1.Cls

Do R = Deci Mod 2 Bin = Str(R) + Bin Deci = Deci \ 2Loop Until Deci = 0

Picture1.Print BinEnd Sub

Page 10: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

File Handling

Files are used for large data input/output

Create a .txt file called CarData by using Notepad (or ASCII file using other software) with following contents

“Suzuki”, 3, 4

“Toyota”, 5, 7

“Honda”, 4, 15

Page 11: For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of

File Handling

Files must be Opened before any read/write operation.

Files must be Closed after any read/write operation

SyntaxOpen “CarData” For Input As #138

Opens a file called CarData.txt for read-only and this file will be called #138

The file reference number can be from 1 to 255