473,408 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

When to use if vs else if

Death Slaught
1,137 1GB
wich would be better to use the if or the else if statement?

Expand|Select|Wrap|Line Numbers
  1. if (whatever)
  2. {
  3. alert("something")
  4. }
  5. if (otherwhatever)
  6. {
  7. alert("othersomething")
  8. }
  9. else
  10. {
  11. alert("analert")
  12. }

or would it be more appropriate to write it this way.

Expand|Select|Wrap|Line Numbers
  1. if (whatever)
  2. {
  3. alert("something")
  4. }
  5. else if ("otherwhatever")
  6. {
  7. alert("othersomething")
  8. }
  9. else
  10. {
  11. alert("analert")
  12. }
Thanks, Death Slaught
Aug 14 '07 #1
7 5627
epots9
1,351 Expert 1GB
wich would be better to use the if or the else if statement?

Expand|Select|Wrap|Line Numbers
  1. if (whatever)
  2. {
  3. alert("something")
  4. }
  5. if (otherwhatever)
  6. {
  7. alert("othersomething")
  8. }
  9. else
  10. {
  11. alert("analert")
  12. }

or would it be more appropriate to write it this way.

Expand|Select|Wrap|Line Numbers
  1. if (whatever)
  2. {
  3. alert("something")
  4. }
  5. else if ("otherwhatever")
  6. {
  7. alert("othersomething")
  8. }
  9. else
  10. {
  11. alert("analert")
  12. }
Thanks, Death Slaught
it depends on your code.

the if statement is only accessed when a condition is met, otherwise it is ignored.
the if elseif else block is the same but it is testing for many conditions and depending on which one is met a different action might have to be performed in order to get the results u want.
Aug 14 '07 #2
Death Slaught
1,137 1GB
it depends on your code.

the if statement is only accessed when a condition is met, otherwise it is ignored.
the if elseif else block is the same but it is testing for many conditions and depending on which one is met a different action might have to be performed in order to get the results u want.
could you give an example or explain this in more detail i get it sort of but i kinda dont i get that you should use the else if statement for say having a different greeting for certain users and a sort of default greeting for every one else kind of like if condition 1 returns false it goes to condition 2 and if condition 2 returns false it uses condition 3 but i dont get the mutiple if statement did you meen if (name1 == name) alert("analert") if (name1 == name2) alert("anotheralert") but you could use the else if statement here instead of another if statement lol im confused.....
Thanks, Death Slaught
Aug 14 '07 #3
epots9
1,351 Expert 1GB
lets try this example:

Expand|Select|Wrap|Line Numbers
  1. var num = 99;
  2. if(num == 99) //it does, so we enter the if block
  3. {
  4. num /= 2;
  5. }
  6. if(num % 2 == 0)  //fails, does second test
  7. {
  8. alert("2");
  9. }
  10. else if(num % 3 == 0) //passes, enters
  11. {
  12. alert("3");
  13. }
  14. else if(num % 5 == 0) //never tested
  15. {
  16. alert("5");
  17. }
  18. else //never reached
  19. {
  20. alert("nothing :(");
  21. }
  22.  
if u just did if instead of else if/else, it would test them all, but with an else if, if one passes it doesn't waste time testing the others.

i know its a bad example, i'll try and post something alittle better later on.
Aug 14 '07 #4
Death Slaught
1,137 1GB
lets try this example:

Expand|Select|Wrap|Line Numbers
  1. var num = 99;
  2. if(num == 99) //it does, so we enter the if block
  3. {
  4. num /= 2;
  5. }
  6. if(num % 2 == 0)  //fails, does second test
  7. {
  8. alert("2");
  9. }
  10. else if(num % 3 == 0) //passes, enters
  11. {
  12. alert("3");
  13. }
  14. else if(num % 5 == 0) //never tested
  15. {
  16. alert("5");
  17. }
  18. else //never reached
  19. {
  20. alert("nothing :(");
  21. }
  22.  
if u just did if instead of else if/else, it would test them all, but with an else if, if one passes it doesn't waste time testing the others.

i know its a bad example, i'll try and post something alittle better later on.

No need to post a better example i get it now the last sentence makes perfect sense so thanks alot.
Aug 14 '07 #5
pbmods
5,821 Expert 4TB
Changed thread title to better describe a problem (threads tend to get more responses when the title contains nouns *and* verbs, with emphasis on plural).
Aug 14 '07 #6
epots9
1,351 Expert 1GB
No need to post a better example i get it now the last sentence makes perfect sense so thanks alot.
I'm glad i could help, come back anytime u need any help.
Aug 15 '07 #7
The bottom line is this:

Use if statements if you want every condition tested. Theoretically, every if-wrapped code block could be executed if its conditional expression evaluated to "true."

Use if/else statements when you want only one of a number of possibilities to be executed under a particular set of circumstances.
Aug 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joey | last post by:
Hi There, I am trying to get the selected value of a listbox when I click a button, everything works ok and I can bind the list and when I have a basic page and click a button to invoke a sub it...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
3
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to...
6
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I want to implement the following: If the user clicks on the border of a form, then I want to show a box around the form that represents the form's bounds. As the user moves the mouse only the...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: shaqa | last post by:
I try to do this but i cannot. i creat two layers with actionscript seperated as slideshow need to be,,and i try to put in one rectangle all of my images but cannot put in work,it doesnt load when i...
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.