I need some help here I guess I am suppossed to put this code inside a while loop. Don't have a clue this is my first scripting class.

if (x <=20 and >=25 ) then
 print x

Else

 If (x <=40 and >=45 )

  Print x

endif

Dani AI

Generated

The original snippet shown by contains a logical mistake: a single comparison like x <= 20 and >= 25 can never be true because the variable must appear in each comparison (for example x >= 20 And x <= 25). was correct that a loop is needed to iterate values; since the instructor asked for a While loop, a minimal VB.NET While-based example and a few clarifications are below.

' VB.NET example (code-behind in ASP.NET can use Response.Write instead of Console.WriteLine)
Dim x As Integer = 1

While x <= 45
    If (x >= 20 AndAlso x <= 25) OrElse (x >= 40 AndAlso x <= 45) Then
        Console.WriteLine(x)   ' or Response.Write(x & "<br />") in ASP.NET pages
    End If
    x += 1
End While

Notes and troubleshooting tips:

  • Each bound must repeat the variable: use x >= 20 And x <= 25 (inclusive range). The original form tested incompatible bounds.
  • Use AndAlso / OrElse to short-circuit boolean expressions when appropriate; that avoids unnecessary evaluation (AndAlso operator).
  • Initialize the loop variable and increment it inside the loop to prevent infinite loops; VB requires End If and End While.
  • For reference on VB control flow see the language docs for If...Then...Else and While...End While (If...Then...Else statement, While...End While statement).

Recommended Answers

All 5 Replies

try

'ADD LINE
FOR X = 1 to 45

if (x <=20 and >=25 ) then
print x

Else

If (x <=40 and >=45 )

Print x

endif 

'ADD LINE
NEXT X

Hope this helps. Next time you post, please enclose code inside

...

tags for readability.

Bill P.

My instructor said something about putting it all inside a while loop.

That'll work too.

The question is how to do it. I don't know . I am really getting frustrated with this class.

Hi...
You post much thread with same question.
Your question already answered in other thread by other member with looping example.
Check This :
http://www.daniweb.com/forums/thread136096.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.