Statements

By: aathishankaran Viewed: 153185 times  Printer Friendly Format    


Conditional

You can also use this statement if and else to evaluate an expression. Instead of returning a value based on the result as a conditional operator.

 if

Syntax 

if (condition)

{

statements

}

EXAMPLE 

<Html>

<Head><Title>Example for if</Title></Head>

<Body>

<Script>

a=parseInt(prompt("Enter a number",0));

b=parseInt(prompt("Enter a second Number",0));

 

if(a>b)

{

alert("A is greater");

}

</Script>

</Body>

</Html>

Output:

 

 Enter first number

.

Enter second Number

 Result

         The condition can be any logical expression. If the result of condition is true, the statements enclosed in curly braces are executed and program execution continues.

If the condition returns false, JavaScript ignores the block of code between the curly braces and continues there after.

 If... Else

Sometimes using the if statement alone is not enough. You can also reserve a set of statements to execute if the conditional expression returns false. This is done by adding an else block of statements immediately following the if block. 

Syntax 

if (condition)

{

stq.tements

}

else

{

statements

}

 EXAMPLE

 <Html>

<Head><Title>Example for If statement</Title></Head>

<Body>

<Script>

 

if (confirm("Do you wish to continue))

{

alert("Reply is true");

}

else

{

alert("Reply is false");

}

</Script>

</Body>

</Html>

Output

If you click Ok

If you click Cancel

 



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial