Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+23 votes

Can we create the if, else if statement like:

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

asked in CSC211_Winter2018 by (1 point)

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 (1 point)
+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 (1 point)
+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 (1 point)
...