DenizKutlu
DenizKutlu

Reputation: 21

Can't figure out do-while and do-until loops

I'm having trouble with the same task that I have to do in four different ways (the four do loops, basically), and it is to generate random numbers and sort them to even and odd. I have specific instructions that this has to be done using do while and do until loops, and so far I've managed to do only one of the four. The first problem is in the fact that the loop here won't run because nch = ch already and it checks the condition first. Is there a way to get around this? The exit conditions are no more than 290 iterations or nch = ch.

Cells.Clear
Randomize
ch = 0
nch = 0

Worksheets("sheet2").Cells(1, 1) = "Do..Until Loop"
Worksheets("sheet2").Cells(1, 2) = "Even"
Worksheets("sheet2").Cells(1, 3) = "Odd"

Do Until (nch + ch) = 290 Or nch = ch
    n = Fix((31 - 13 + 1) * Rnd) + 13
    If n Mod 2 = 0 Then
                ch = ch + 1
                Worksheets("sheet2").Cells(1 + ch, 2) = n
            Else
            nch = nch + 1
            Worksheets("sheet2").Cells(1 + nch, 3) = n
    End If
Loop

The second problem is with a do while loop, where I have to achieve the same goal, but the first condition keeps being ignored for some reason.

Do

   If n Mod 2 = 0 Then
                ch = ch + 1
                Worksheets("sheet2").Cells(1 + ch, 2) = n
            Else
            nch = nch + 1
            Worksheets("sheet2").Cells(1 + nch, 3) = n
    End If
Loop While (nch + ch) < 290 Or nch <> ch

Any help would be much appreciated! Thanks!

Upvotes: 2

Views: 477

Answers (3)

Pierre
Pierre

Reputation: 1046

"until" stops if true, "while" stops when false.

not ( (nch + ch) = 290 Or nch = ch ) 

is

(nch + ch) <> 290 AND nch <> ch

the "or" is likely to be your problem.

Please keep in mind the two codes are not totally equivalent, as in the second, the loop is executed at least once.

Upvotes: 0

TrtlBoy
TrtlBoy

Reputation: 669

If you add a boolean to evaluate it

Cells.Clear
Randomize
ch = 0
nch = 0
equal = false

Worksheets("sheet2").Cells(1, 1) = "Do..Until Loop"
Worksheets("sheet2").Cells(1, 2) = "Even"
Worksheets("sheet2").Cells(1, 3) = "Odd"

Do Until (nch + ch) = 290 Or equal
    n = Fix((31 - 13 + 1) * Rnd) + 13
    If n Mod 2 = 0 Then
         ch = ch + 1
         Worksheets("sheet2").Cells(1 + ch, 2) = n
    Else
         nch = nch + 1
         Worksheets("sheet2").Cells(1 + nch, 3) = n
    End If
    If nch = ch Then
         equal = true
    End If
Loop

With the second example where is n defined? if n=0 the n Mod 2 will equal 0

Upvotes: 0

Hocus
Hocus

Reputation: 121

Not sure if you have to write it as 'Loop While' for your home work.

but you could change your syntax to.

Do While (nch + ch) < 290
    n = Fix((31 - 13 + 1) * Rnd) + 13
    If n Mod 2 = 0 Then
        ch = ch + 1
        Worksheets("sheet2").Cells(1 + ch, 2) = n
    Else
        nch = nch + 1
        Worksheets("sheet2").Cells(1 + nch, 3) = n
    End If
Loop

And your I'm not sure your 'Do Until' needs the second check, try this:

Cells.Clear
Randomize
ch = 0
nch = 0

Worksheets("sheet2").Cells(1, 1) = "Do..Until Loop"
Worksheets("sheet2").Cells(1, 2) = "Even"
Worksheets("sheet2").Cells(1, 3) = "Odd"

Cells.Clear
Randomize
ch = 0
nch = 0

Worksheets("sheet2").Cells(1, 1) = "Do..Until Loop"
Worksheets("sheet2").Cells(1, 2) = "Even"
Worksheets("sheet2").Cells(1, 3) = "Odd"

Do Until (nch + ch) = 290
    n = Fix((31 - 13 + 1) * Rnd) + 13
    If n Mod 2 = 0 Then
                ch = ch + 1
                Worksheets("sheet2").Cells(1 + ch, 2) = n
            Else
            nch = nch + 1
            Worksheets("sheet2").Cells(1 + nch, 3) = n
    End If
Loop

Also its not the cleanest way but to check a criteria later on you can use this to exit the loop.

If ch = nch Then
    Exit Do
End If

Upvotes: 2

Related Questions