Nov 21, 2009

Conditional Statements Of Javascript - Tutorial for beginers

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > Client Side Scripting > Javascript

Conditional Statements Of Javascript - Tutorial for beginers

BDIT
In this tutorial we will discuss about conditional statements of JavaScript. JavaScript has 4 types of conditional statements such as if statement, if...else statement, if...else if....else statement and switch statement. All this statements are used in different condition. Now we will try to discuss this 4 types of statements with some examples.

When we need to execute some code only in a specified condition, then we will use if statement.
The syntax of if statement is
if (condition)
{
code to be executed if condition is true
}
One important thing is that this if will be in small letter, if we use in capital letter it will show a JavaScript error.

Let we want that if password is “11111” it will show “Password Accepted”. So, the condition will be password==11111, and the complete code will be
CODE
<script type="text/javascript">
var password=11111;
if (password==11111)
{
document.write("<b> Password Accepted </b>");
}
</script>


When we need to execute some code if the condition is true and another code if the condition is false, then we will use if....else statement.
The syntax of if....else statement is
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}

Let we want that if password is “xyzabc” it will show “Password Accepted” otherwise it will show “You do not have permission to enter”. So, the condition will be password==11111, and the complete code will be
CODE
<script type="text/javascript">
var password=11111;
if (password==11111)
{
document.write("<b> Password Accepted </b>");
}
else
{
document.write("<b> You do not have permission to enter </b>");
}

</script>



When we need to execute select one of many blocks of code to be executed then we will use if...else if....else statement.
Syntax of if...else if....else statement is
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else if (condition3)
{
code to be executed if condition3 is true
}
....
....
....
else if (condition n)
{
code to be executed if condition n is true
}

else
{
code to be executed if all conditions are not true
}

For an example, let we want that it will show Good Morning before 10 AM, Good Day in between 10 AM and 4 PM and all other time it will show only Welcome!. So the first condition is time<10, second condition is time>10 && time<16. So, the complete code will be
CODE
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Welcome!</b>");
}
</script>


We use switch statement when we want to execute select one of many blocks of code to be executed.
Syntax of switch statement is
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
case 3:
execute code block 3
break;
......
......
......
default:
code to be executed if n is different from all cases
}

For an example, let we ant to know what day today is. We know that 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday and 6 = Saturday. So, the complete code will be

CODE
<script type="text/javascript">
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 0:
  document.write("Sunday");
  break;
case 1:
  document.write("Monday");
  break;
case 2:
  document.write("Tuesday");
  break;
case 3:
  document.write("Wednesday");
  break;
case 4:
  document.write("Thursday");
  break;
case 6:
  document.write("Saturday");
  break;
default:
  document.write("Is it my Holyday!");
}
</script>

 

 

 


Comment/Reply (w/o sign-up)

Jeigh
This could be a very useful tutorial for someone just starting out into programming. These conditional statements are pretty standard fair and are pivotal to a programmers arsenal be it in javascript or other languages. While the syntax often changes in slight ways the basic concepts stay the same so hopefully some people just starting to look at coding stumble onto this tut.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Conditional Statements Javascript Beginers

  1. Javascript Operators - (0)
  2. Javascript Tutorial For Beginner - Using Javascript in HTML page (0)
    In this tutorial I will show you how you can put JavaScript in a HTML page. It is very easy to add
    JavaScript in a HTML page. We will use tag for this purpose. Inside the tag, we will use "type="
    attribute and will define the scripting language. We will define the script language as
    “text/javascript”. After define the script language we will add our JavaScript codes and
    at last we will close the tag using . So the complete code will be- CODE (Place for our
    JavaScript codes) Now we will use “document.write” command (it i...
  3. Is Their A Free Or Express Program To Help Make Javascript, Or How Do I Do It. - i want to make javascript but dont know how. (0)
    can someone direct me to a free program that will help me make java script, or maybe just tell me
    how? ...
  4. Javascript SSI- Blocking Internet Protocols - Blocking Internet Protocals from site (2)
    Hello, i thought this would be interesting since i haven't discussed Javascript in years.
    SSI(Server Side Includes) can be used for many interesting things...many of which deal with
    connection information and such. A few years ago i was working on something for the Counter-Strike
    Blacklist when it existed. It was a project i was making for a site called CS anti-hack Community.
    This was going to be an all web-based security project to basically not allow any IP currently on
    the blacklist to preceed to any site that wanted to be a part the anti-hack community. Well...
  5. Garmon's Javascript #1 - Simple Object Database - Learn basic Object-Oriented Programming (1)
    Okay, so you're like me - you've heard about OOP or Object Oriented Programming and wonder
    what all the hOOPlah is about! So here's the straight pOOP. Okay, sorry for the stOOPid puns.
    I'll stop now. Honestly, I will. OOPs I did it again. Right. Here's the scenario: we have
    an online store, we want to maintain a database of our products, and we want to be able to access
    them. This tutorial shows how this can be done, with very simple, commented code. Please note, that
    I'm not recommending that you use JavaScript to store your actual product dat...
  6. Forum post templates! - Using javascript (3)
    I guess I'll better start by explaining by what I mean by these post templates. The idea came
    to me as one online-friend of mine has a habit of posting all his posts with same colour on a
    certain forum. We had previously talked how it would be cool if vBulletin (in this case) would have
    option to save a "post template" which it would automatically load to textarea everytime a new reply
    or thread is posted. Well as it is not implemented on the server side, why not do it clientside!
    What you may have not realized is that "bookmark toolbar" or "personal bar" bookmark...



Looking for conditional, statements, javascript, tutorial, beginers

See Also,

*SIMILAR VIDEOS*
Searching Video's for conditional, statements, javascript, tutorial, beginers
advertisement



Conditional Statements Of Javascript - Tutorial for beginers

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com