Little question

Does this coding take all int values?
if(random >= 6 || random <= 9)
I mean, can number 3 be part of this if statement?
Last edited on
Does this coding take all int values?


You don't give us the types for the random variable, we therefore cannot say if it takes int values.

I mean, can number 3 be part of this if statement?


It can indeed take three, however it will always return true, no matter what number you enter. The reason this is is because you use the || operator. What your code is saying is that if random is greater than/ equal to six or less than/ equal to nine, the statement is true. So anything greater than six or less than nine fits, but everything is greater than six or less than nine (three is less than nine, 100 is greater than six for example)

To combat this, you need to use the && operator. It will check to see if random is greater than/ equal to six and less than/ equal to nine, giving a range of six to nine. In this case, three would not be a true statement as it is less than six.
Last edited on
You are correct, the statement "(random >= 6 || random <= 9)" will always return true no matter the value of 'random'. It is the equivalent of the statement "(true)".

If you want a value between 6 and 9 inclusive you would need to use the and operator.

I.E.
1
2
3
if( random >= 6 && random <= 9){

}
A leap year is any year divisible by 4 unless it is divisible by 100,but not 400.Write a program to tell whether a year is a leap year.

(SYNTAX: if-else statement using && //)

Last edited on
Topic archived. No new replies allowed.