English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Example, I want the loop to execute while x < 0 and x > 50..

2006-07-08 14:07:37 · 5 answers · asked by sublime 3 in Computers & Internet Programming & Design

Program details: I want the program to print "Invalid input." when x < 0 or x > 50, then ask the user to input continuously until the number he inputs is not < 0 or not > 50.

2006-07-08 16:18:28 · update #1

5 answers

Except this will never iterate

while (x<0 && x>50)
;

since if the first expression evaluates to true, the second will be false and vice-versa

while ( x < 0 || x > 50 )
;

will iterate so long as x is not 0 to 50 inclusive
but that's not what you asked for

You have to be really careful with the second one as well since you could easily end up in an infinite loop. In the second, the breaking condition is x = [0,50].

There are several synonomous structures you can use.

2006-07-08 15:58:10 · answer #1 · answered by Anonymous · 4 1

while (x<0 || x > 50){
//do stuff
}

as long as whatever in the while loop evaluates as true it will repeat.

2006-07-08 21:10:18 · answer #2 · answered by John J 6 · 0 0

It depends on the language in PERL or PHP it would something like

while (x<0 && x>50){


}

AND is &&
OR is ||

2006-07-08 21:11:27 · answer #3 · answered by MikeD 3 · 0 0

do
{
printf("Enter value of X: ");
scanf("%d",&x);

//perform other operation

} while(x<0 || x>50);

The thing that you are telling works best with do-while loop
see this, it might helps you http://analysingc.50webs.com

2006-07-09 07:30:39 · answer #4 · answered by Anonymous · 0 0

while(x<=50)
will work just fine in any lang.

2006-07-08 21:15:25 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers