# Problem Converting While and Do While Loop to For loop in Visual Logic

Page 1 of 1

## 1 Replies - 5366 Views - Last Post: 17 April 2011 - 12:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=226693&amp;s=5d8cb7c31b1164135881bef6840d8924&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MeghanNanney

Reputation: 0
• Posts: 1
• Joined: 08-April 11

# Problem Converting While and Do While Loop to For loop in Visual Logic

Posted 08 April 2011 - 11:31 AM

I have an assignment to determine whether the user entered an odd or even number and to display how many odd and even numbers were entered, with a predetermined number of iterations, in this case 6. So user enters 6 numbers and the program outputs how many entered were odd and how many entered are even.
I am to write a While loop, and Do - While loop and a FOR loop that all do this exact thing. I have my While and DO-While algorithms written but am having some difficulty converting the program to a FOR loop, and suggestions to help get me started in the right direction would be greatly appreciated.

While Loop:

```Set count=0                   //initialize counter and variables
set MaxValue=6
Set TotalEven=0
Set TotalOdd=0

While count < Max Value
Input Num
SignDet=NumMod2            //operation to determine if number entered is odd or even
if SignDet=0
TotalEven=TotalEven + 1
Else
TotalOdd=TotalOdd + 1
End If
count=count+1       //increment counter
End While
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers."    //display number of even and odd numbers were entered

Do While Loop:

Set count=0              //initialize counter and variables
set MaxValue=6
Set TotalEven=0
Set TotalOdd=0

Do
Input Num
SignDet=NumMod2       //operation to determine if number is even or odd
if SignDet = 0
TotalEven=TotalEven + 1
Else
TotalOdd=TotalOdd + 1
End If
count = count + 1
While count < MaxValue
End While
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers."

```

I know that the FOR loop is made specifically to initialize, test and increment a counter variable and I have the format of the pseudocode but for some reason am struggling with putting the program into a For loop.

My attempt is:

```Set counter = 1      //initialize counter and variables
Set MaxValue = 6
Set TotalEven = 0
Set TotalOdd = 0

For counter = 1 to MaxValue
Input Num
SignDet = NumMod2
If SignDet = 0
TotalEven=TotalEven + 1
Else
TotalOdd = TotalOdd +1
End If
count=count +1
End For
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers."
```

I am not sure if this is correct or not. I am a math major and only taking intro to computer program as a requirement of my major. I find myself having a tough time wrapping my mind around the logic used to program so any help anyone could give me would be greatly appreciated. Thanks so much for taking the time to help me and sorry if I posted incorrectly.

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 18 April 2011 - 06:36 AM

Is This A Good Question/Topic? 0

## Replies To: Problem Converting While and Do While Loop to For loop in Visual Logic

### #2 calvinthedestroyer

• D.I.C Lover

Reputation: 172
• Posts: 1,932
• Joined: 13-October 07

## Re: Problem Converting While and Do While Loop to For loop in Visual Logic

Posted 17 April 2011 - 12:56 PM

```Set count=0 //initialize counter and variables
set MaxValue=6
Set TotalEven=0
Set TotalOdd=0

While count < Max Value
Input Num
SignDet=NumMod2 //operation to determine if number entered is odd or even
if SignDet=0
TotalEven=TotalEven + 1
Else
TotalOdd=TotalOdd + 1
End If
count=count+1 //increment counter
End While
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers." //display number of even and odd numbers were entered

```

That one looks ok.

Do While Loop:
```Set count=0 //initialize counter and variables
set MaxValue=6
Set TotalEven=0
Set TotalOdd=0

Do
Input Num
SignDet=NumMod2 //operation to determine if number is even or odd
if SignDet = 0
TotalEven=TotalEven + 1
Else
TotalOdd=TotalOdd + 1
End If
count = count + 1
While count < MaxValue
End While
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers."

```

Instead of just "DO", you should have "DO While count < MaxValue"

```Set counter = 1 //initialize counter and variables
Set MaxValue = 6
Set TotalEven = 0
Set TotalOdd = 0

For counter = 1 to MaxValue
Input Num
SignDet = NumMod2
If SignDet = 0
TotalEven=TotalEven + 1
Else
TotalOdd = TotalOdd +1
End If
count=count +1
End For
Display "You entered six numbers: " & TotalOdd & " odd numbers and" & TotalEven & " even numbers."

```

um, that's supposed to be FOR, NEXT not FOR, END FOR

This post has been edited by calvinthedestroyer: 17 April 2011 - 01:02 PM