Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JavaScript
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Conditional Examples(if - else- switch case) in JavaScript

                         

In this article you will learn the basics of JavaScript conditions and create your conditional examples in JavaScript .

About Conditional Statement
First of all we write the code after that we want to perform the different actions for different decisions. We can use conditional statements in our code. Conditional statements in JavaScript are used to perform different actions based on different conditions.

There are following types of Conditional Statements:-

  • if statement:- It means the condition is true then our writing code is executed other wise our code is not execute . 
     Syntax for the if statement:-
                                               if ( expression )
                                               {
                                                statement1
                                                statement2
                                                }
  • if - else statement:- This statement is used to when the condition is true then our code to execute and when the condition is false then our written code is not execute.
    Syntax for the if - else statement:-
                                                         if (expression)
                                                            statement1
                                                        else if (expression2)
                                                            statement2
                                                        else
                                                            statement3
  • if...else if....else statement -This statement is used to if you want to select one of many blocks of code to be executed .
  • switch statement:-This statement is used to if you want to select one of many blocks of code to be executed.
    Syntax for the switch statement:-
                                                     switch (expression)
                                                     {
                                                       case1: 
                                                       statement1
                                                       break
                                                       case2: 
                                                       statement2
                                                       break
                                                       default: 
                                                       statement3;
                                                      }

 This example is if - else statement. It display the first of all the sum of two numbers and check the condition after that it execute the code.

<html>
<head>
<script language="javascript">
function showAlert()
{
var a=20;
var b=10;
var sum=0;
sum=a+b;
document.write(" sum= "+sum);
if(sum==3)
{
document.write("Condition is true");
document.write("This is my first if statement program");
}
false
{
document.write("Condition is false");
document.write(" This is my first if -else statement program");
}
}
</script>
</head>
<body>

<script language="javascript">

showAlert();

</script>

</body>
</html>

 

This example is display to all months in a year for using the switch case:-

<body> 
<script type="text/javascript"> 
var n=0;
n=
prompt("Enter a number between 1 to 12:") 
switch(n) 
{
case(n="1"): 
document.write("January");
break
case(n="2"): 
document.write("Febuary");
break
case(n="3"): 
document.write("March");
break
case(n="4"):
document.write("April");
break
case(n="5"):
document.write("May");
break
case(n="6"):
document.write("June");
break
case(n="7"):
document.write("July");
break
case(n="8"):
document.write("August");
break
case(n="9"):
document.write("September");
break
case(n="10"):
document.write("October");
break
case(n="11"):
document.write("November");
break
default:
document.write("December"); 
break
}
</script> 
</body> 

 

 

 

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.