Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+22 votes

Can we create the if, else if statement like:

if ( x = 1, 3, 5, 7,8) {

asked in CSC211_Winter2018 by (8 points)

3 Answers

+13 votes

Well you can't really assign multiple values such as. You can make it a double equal sign which is making a condition of testing whether it is equal.

answered by (8 points)
+2

Well, then can we use anything like if ( x = 1 or 3 or 5 or 7...) ?

+10 votes

Actually, yes! This requires use of the OR operator, which will return true if at least one argument in a set is true. Here's what it would look like in your example:

for (int i = 1; i <= 10; i++) {
	if (i == 1 | i == 3 | i == 5 | i == 7 | i == 8) {
		System.out.print(i + "");
	}
}
// --> 1 3 5 7 8
answered by (8 points)
+3 votes

you could also just keep doing if statements and make it do the same thing

if(x=1){
}
if (x=3){
}
and so on

answered by (8 points)
...