What's new

Multiple Ifs and Else if

  • Thread starter DatBoiJay53
  • Start date
  • Views 260
D

DatBoiJay53

Enthusiast
Messages
634
Reaction score
120
OK, I still don't really get the difference here. Say I was trying to determine 5 things. Whats the difference between using 5 if statements, or using 1 if statement and 4 else if statements. Is there a reason why i should choose 1 over the other. If so can you show an example where you would.
Thx ! :tongue:
 
D

denz

The Kingslayer
Retired
Messages
10,076
Reaction score
6,308
Im presuming youre talking about java? Im no java expect (been only doing it for few weeks) but i think you can only have 1 else statement per if statement. (may be wrong). Eg if x then a else b, Thus you could have 5 different if statement with an outcome, And it could still work.

eg if x then a
If z then b
If y then c etc

if this is wrong just ignore it and wait for someone that actually is sure.

OK, I still don't really get the difference here. Say I was trying to determine 5 things. Whats the difference between using 5 if statements, or using 1 if statement and 4 else if statements. Is there a reason why i should choose 1 over the other. If so can you show an example where you would.
Thx ! :tongue:
 
B

BKSlayer

Enthusiast
Messages
605
Reaction score
138
I think you mean what is the difference between an if/else if statement, and which one should you use?

When you use multiple 'if' statements, it will check every single one, regardless if it the other if statements are true or not. Whereas with an 'else if' statement, if one of the 'if/else if statements' is correct, it will stop checking. Example:

Code:
int x = 100;
if(x == 100)
  Console.WriteLine("The value of x is 100!");
if(x < 1000)
  Console.WriteLine("X has a value less than 1000!");
if(x > 200)
  Console.WriteLine("X has a value greater than 200!");
if(x > 50)
  Console.WriteLine("X has a value greater than 50");
(That is C#, but it applies to all languages.)

In this case, the program will print:
The value of x is 100!
X has a value less than 1000!
X has a value greater than 50

Whereas if you used an 'if/else' statement, like this:
Code:
int x = 100;
if(x == 100)
  Console.WriteLine("The value of x is 100!");
else if(x < 1000)
  Console.WriteLine("X has a value less than 1000!");
else if(x > 200)
  Console.WriteLine("X has a value greater than 200!");
else if(x > 50)
  Console.WriteLine("X has a value greater than 50");

It will just print "The value of x is 100!", as it will stop checking the other statements after it evaluates the first if statement (if x is equal to 100) to be true.

That was a pretty long and probably even unnecessary explanation, but yeah. :smile:
 
Z

Z61

Some times our saints are sinners
Retired
Messages
5,476
Reaction score
3,426
Switch statements are where the cool cats are.
 
B

Bar Starred

Contributor
Messages
2,264
Reaction score
748
Switch statements are where the cool cats are.

In regards to javascript, Switch statements are only more efficient when there are around 10 or more specific cases to be checked, of which, only one will be true.

@OP:

You use 1 if, and 4 else, if there is a case to be checked, where only one possible outcome can be true.
Code:
var number = 1;
if(number < 0)
else if(number === 1)
else if(number === 2)
......

But, you use multiple if's, if you are checking for multiple properties.
Code:
var number = 1;
if(number === 1)
if(number.length===1)
......
 
D

DatBoiJay53

Enthusiast
Messages
634
Reaction score
120
I think you mean what is the difference between an if/else if statement, and which one should you use?

When you use multiple 'if' statements, it will check every single one, regardless if it the other if statements are true or not. Whereas with an 'else if' statement, if one of the 'if/else if statements' is correct, it will stop checking. Example:

Code:
int x = 100;
if(x == 100)
  Console.WriteLine("The value of x is 100!");
if(x < 1000)
  Console.WriteLine("X has a value less than 1000!");
if(x > 200)
  Console.WriteLine("X has a value greater than 200!");
if(x > 50)
  Console.WriteLine("X has a value greater than 50");
(That is C#, but it applies to all languages.)

In this case, the program will print:
The value of x is 100!
X has a value less than 1000!
X has a value greater than 50

Whereas if you used an 'if/else' statement, like this:
Code:
int x = 100;
if(x == 100)
  Console.WriteLine("The value of x is 100!");
else if(x < 1000)
  Console.WriteLine("X has a value less than 1000!");
else if(x > 200)
  Console.WriteLine("X has a value greater than 200!");
else if(x > 50)
  Console.WriteLine("X has a value greater than 50");

It will just print "The value of x is 100!", as it will stop checking the other statements after it evaluates the first if statement (if x is equal to 100) to be true.

That was a pretty long and probably even unnecessary explanation, but yeah. :smile:

Might be unnecessary but it sure helped, thx! :wink:
 
Top Bottom