Re: While Loops (I think)...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Howard wrote:
> Hello everyone (total VB.NET beginner here),
> I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came
> across an exercise that I can't get to work. The exercise asks that
> you create a game that makes the user guess a number from 1-100, and
> you tell the user "lower" or "higher" as they input their guesses,
> until they guess the correct number, at which point you then tell the
> user "Correct". I tried using the While Loop, and it "sort of" worked,
> but after a while (no pun intended) of inputting numbers (guesses), it
> said "Correct" to incorrect answers.
<snip>

and then sent some code:

<snip>
> Sub Main()
> Console.WriteLine("Pick a number from 1 to 100")
> Dim StrInput As Integer = Console.ReadLine()
>
> While (StrInput < "27")
> Console.WriteLine("higher")
> StrInput = Console.ReadLine()
> End While
> While (StrInput > "27")
> Console.WriteLine("lower")
> StrInput = Console.ReadLine()
> End While
> Console.WriteLine("Correct!")
> StrInput = Console.ReadLine()
>
> End Sub
<snip>

The While loop will execute until a given condition becomes true. Its
structure is:

While Condition
'Do something
End While

where Condition represents a boolean expression.

In your case, the condition that "governs" the loop is the number
entered being diferent from a given value: Number <> Value.

Therefore, your While loop would look like this:

While Number <> Value

'Do something

End While

If the condition ceases to be true then the looping will stop and
execution will proceed at the next instruction.

In your case this will happen only when Number ceases to be different
from Value. In other words, when execution breaks out of the loop, we
know we have a Number which is equal to Value and therefore we may do
something about it, which is to congratulate the user and finish the
program:

While Number <> Value

'Do something

End While
Console.WriteLine("Correct!")

Now, back to the While loop. What to do while Number remains different
from Value? The problem states that we must print "lower" if Number is
above Value, or "higher" if Number is bellow Value:

While Number <> Value

If Number < Value Then
Console.WriteLine("Higher")
Else
'the number can only be above the value
Console.WriteLine("Lower")
End If

End While
Console.WriteLine("Correct!")

Of course, there are still a few parts missing. Notice that a common
use-pattern of the While loop goes like this:

Get some input
While the input is not valid
Show some message
Get the input again
(end While)
Deal with the valid input

In your case, this might translate to the following actions:

'Get some input
Console.WriteLine("Pick a number from 1 to 100")
Number = Integer.Parse(Console.ReadLine())

'While the Input is not valid
While Number <> Value

'Show some message
If Number < Value Then
Console.WriteLine("Higher")
Else
Console.WriteLine("Lower")
End If

'Get the input again
Number = Integer.Parse(Console.ReadLine())

End While

'Deal with the valid input
Console.WriteLine("Correct!")

Of course, in a correct VB.Net app we need to define variable types and
constants. Therefore, the full program would be:

Sub Main()
Const Value As Integer = 27
Dim Number As Integer

Console.WriteLine("Pick a number from 1 to 100")
Number = Integer.Parse(Console.ReadLine())

While Number <> Value
If Number < Value Then
Console.WriteLine("Higher")
Else
Console.WriteLine("Lower")
End If
Number = Integer.Parse(Console.ReadLine())
End While

Console.WriteLine("Correct!")
End Sub

HTH.

Regards,

Branco.

.



Relevant Pages

  • RE: Email and Fax a Report
    ... Opening a report in a loop seems odd, ... By placing the sub on its own, ... Dim objOutlook As Outlook.Application ... Set newMail = objOutlook.CreateItem ...
    (microsoft.public.access.modulesdaovba)
  • Re: arrays verodern
    ... interessant wenn SUB ... unnötige Bytes in der Schleife hat, ... welches LOOP, ... mov ecx, CCount ...
    (de.comp.lang.delphi.misc)
  • Re: Recognizing When An IE window changes URLs and closing the IE
    ... lpClassName As String, ByVal lpWindowName As String) As Long ... Private Sub x_NewWindow2 ... Dim ie1 As New IEClass ... On Error GoTo Here ' Error handling sequence that breaks the loop, ...
    (microsoft.public.excel.programming)
  • Re: Trying to "Drop-up" a dropped-down combobox
    ... Thanks for the warning about the continuous loop problem. ... reflected an attempt I had made to solve the dropdown issue. ... > Private Sub Combo2_NotInList(NewData As String, Response As Integer) ...
    (microsoft.public.access.formscoding)
  • Re: MKDir not working
    ... Public Sub MkDirs ... first argument of the Split function in the active loop statement; ... track and return a Boolean status from the function. ... MkDirs = MkDirs + Err.Number ...
    (microsoft.public.vb.general.discussion)