Tuesday, December 14, 2010

Learning JavaScript tutorial – JavaScript if, if else and else if Condition Statement

Decision making based on different computations or perform actions depending on a programmer-specified condition is one of the features of the JavaScript language.

JavaScript if Condition Statement

JavaScript if Condition Statements are used to perform different actions (make decisions) based on different conditions evaluated to a Boolean true or false. JavaScript if Condition Statement help us to execute statement block conditionally.

JavaScript if Condition Statement Syntax

if (conditional expression)
  {
  statement block executed if condition is true(satisfied).
  }
Here, If the resulting value of conditional expression is true or can be evaluated to true, statement block enclosed within the Curly braces – {} is executed.
if Condition Statement flowchart
If Condition Statement flowchart
<script type="text/javascript">
var iCount=5;
if (iCount<10)
  {
  alert(iCount + " is less the 10");
  }
</script>
Basically, JavaScript if Condition Statement evaluate conditions like Is True, Is Flase, less than (<), less than or equal to (<=), equal to (==), greater than (>), greater than or equal to (>=), not equal to (!=). JavaScript if Condition Statement also allows the use of Logical expression like AND (&&), OR (||) and NOT (!) to evaluate more than one conditional expression.
<script type="text/javascript">
var iCount=5;
if (iCount>3 && iCount<10)
  {
  alert(iCount + " is greater than 3 but less the 10");
  }
</script>

JavaScript if else Condition Statement

JavaScript if Condition Statement execute statement block if a desired condition is satisfied. To improve the usability of "if Condition" JavaScript if Condition Statement offer an else clause that is executed when expression is false (not satisfied). When the JavaScript if else Condition Statement is evaluated; only, any one of statement block is executed, either "if statement block" or "else statement block".

JavaScript if else Condition Statement Syntax

if (conditional expression)
  {
  statement block executed if condition is true(satisfied).
  }
else
  {
  statement block executed if condition is false( not satisfied).
  }
Here, If the resulting value of conditional expression is true or can be evaluated to true, statement block enclosed within the Curly braces – {} followed by if statement is executed. If the resulting value of conditional expression is false, statement block enclosed within the Curly braces – {} followed by else clause is executed.
if else Condition Statement flowchart
If Else Condition Statement flowchart
<script type="text/javascript">
var iCount=5;
if (iCount<3)
  {
  alert(iCount + " is less than 3");
  }
else
  {
  alert(iCount + " is greater than 3");
  }
</script>

JavaScript else if Condition Statement

JavaScript if else Condition statement can test a condition and execute one of two pieces of statement block, depending on the evaluated condition. Sometime we need to execute any one of the many pieces of statement block. In this situation JavaScript else if Condition Statement help us alot. JavaScript else if Condition Statement is not really a JavaScript statement, however it is the result of repeated use of JavaScript if else Condition Statement.

JavaScript else if Condition Statement Syntax

if (conditional expression 1)
  {
  statement block executed if condition 1 is true(satisfied).
  }
else if (conditional expression 2)
  {
  statement block executed if condition 2 is true(satisfied).
  }
else
  {
  statement block executed if condition 1 and 2 are false( not satisfied).
  }
Syntactically, JavaScript else if Condition Statement not limited to above illustrated syntax. You can use any combination of Syntactically valid JavaScript if and if else Condition Statement.
else if Condition Statement flowchart
Else If Condition Statement flowchart
<script type="text/javascript">
var iCount=5;
if (iCount<3)
  {
  alert(iCount + " is less than 3");
  }
else if (iCount==5)
  {
  alert(iCount + " is equal to 5");
  }
else
  {
  alert(iCount + " is greater than 3");
  }
</script>

No comments:

Post a Comment