Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Conditional Statements Of Javascript, Tutorial for beginers
BDIT
post Jun 11 2008, 04:54 PM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 11
Joined: 30-April 08
Member No.: 30,066



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>

Go to the top of the page
 
+Quote Post
Jeigh
post Jun 11 2008, 05:33 PM
Post #2


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,354
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Remove Bad Sectors Or Bad Clusters From HDD(17)
  2. 3ds Max Tutorial.(7)
  3. C#.NET: Web Timer Control Tutorial(3)
  4. Video Editing Tutorial(3)
  5. VB.NET: MS-Access Interaction Tutorial (Part I)(18)
  6. Using The Php Mail() Function For Images Or Attachments(3)
  7. Pre Loader Tutorial For Flash(6)
  8. Installing Glut To Dev C++(3)
  9. Wireless: Bypassing Mac Filtering(10)
  10. Phpbb - Installation Tutorial ( For Newbies Based On Astahost Cpane)l(5)
  11. Photoshop Tutorial: Signature(4)
  12. Php Tutorial: Making A Shoutbox(12)
  13. Html Basic Tutorial(9)
  14. Safety(9)
  15. How To Download Videos From Youtube(11)
  1. Linux Beginners - Tutorial On Editors In Linux.(3)
  2. C/c++ -gdb Linux Debug Tool(1)
  3. Gimp Userbar Tutorial(3)
  4. Web Editor(0)
  5. Php: Lesson #2; If...else Statements.(1)
  6. Bulletproof Ftp Server Tutorial(0)
  7. Moving To Fedora 9(1)
  8. Basic Html Tutorial(1)
  9. Psychostats(1)
  10. How To Understand A Database Schema(4)
  11. Here Are Some Great Php Tutorial Sites(4)
  12. Gimp Animation Tutorial(7)
  13. 12 Javascript Image Galleries(0)


 



- Lo-Fi Version Time is now: 16th October 2008 - 04:11 AM