-2

Im very new to c and am trying to make a while loop that checks if the parameter is less than or equal to a certain number but also if it is greater than or equal to a different number as well. I usually code in python and this is example of what I'm looking to do in c:

while(8 <= x <= 600)

Allen
  • 162
  • 2
  • 3
  • 15
  • For a while loop to process the information the condition needs to be true. So if you start at 0 you will not execute the while loop. The syntax you have is valid, all you need is an expression inside the while, If the expression evaluates to 0 it will not evaluate else it will. See http://stackoverflow.com/questions/2254075/using-true-and-false-in-c – Romain Hippeau Jun 08 '16 at 00:08

3 Answers3

3
while (x >= 8 && x <= 600){

}
Kaizhe Huang
  • 990
  • 5
  • 11
0

The relational and equality operators (<, <=, >, >=, ==, and !=) don't work like that in C. The expression a <= b will evaluate to 1 if the condition is true, 0 otherwise. The operator is left-associative, so 8 <= x <= 600 will be evaluated as (8 <= x) <= 600. 8 <= x will evaluate to 0 or 1, both of which are less than 600, so the result of the expression is always 1 (true).

To check if x falls within a range of values, you have to do two separate comparisons: 8 <= x && x <= 600 (or 8 > x || x > 600)

John Bode
  • 119,563
  • 19
  • 122
  • 198
-1

this one means if x>=8,if x is bigger than 8 ,it turns 1 <= 600 ;(always true) if not , Then it turns 0<=600 ; (always fause)

Dennis
  • 1
  • 2
  • @user3078414 I don't know what the x value is ? Sorry about my bad English. – Dennis Jun 08 '16 at 09:09
  • In that case, please don't give answers before you _know_ what you are answering to. You have comments to ask additional information... – user3078414 Jun 08 '16 at 09:24