wich would be better to use the if or the else if statement? - if (whatever)
-
{
-
alert("something")
-
}
-
if (otherwhatever)
-
{
-
alert("othersomething")
-
}
-
else
-
{
-
alert("analert")
-
}
or would it be more appropriate to write it this way. - if (whatever)
-
{
-
alert("something")
-
}
-
else if ("otherwhatever")
-
{
-
alert("othersomething")
-
}
-
else
-
{
-
alert("analert")
-
}
Thanks, Death Slaught
7 5627
wich would be better to use the if or the else if statement? - if (whatever)
-
{
-
alert("something")
-
}
-
if (otherwhatever)
-
{
-
alert("othersomething")
-
}
-
else
-
{
-
alert("analert")
-
}
or would it be more appropriate to write it this way. - if (whatever)
-
{
-
alert("something")
-
}
-
else if ("otherwhatever")
-
{
-
alert("othersomething")
-
}
-
else
-
{
-
alert("analert")
-
}
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.
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
lets try this example: -
var num = 99;
-
if(num == 99) //it does, so we enter the if block
-
{
-
num /= 2;
-
}
-
if(num % 2 == 0) //fails, does second test
-
{
-
alert("2");
-
}
-
else if(num % 3 == 0) //passes, enters
-
{
-
alert("3");
-
}
-
else if(num % 5 == 0) //never tested
-
{
-
alert("5");
-
}
-
else //never reached
-
{
-
alert("nothing :(");
-
}
-
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.
lets try this example: -
var num = 99;
-
if(num == 99) //it does, so we enter the if block
-
{
-
num /= 2;
-
}
-
if(num % 2 == 0) //fails, does second test
-
{
-
alert("2");
-
}
-
else if(num % 3 == 0) //passes, enters
-
{
-
alert("3");
-
}
-
else if(num % 5 == 0) //never tested
-
{
-
alert("5");
-
}
-
else //never reached
-
{
-
alert("nothing :(");
-
}
-
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.
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).
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |