06. JavaScript If...Else Statements

Javascript If...Else Statements

Conditional Statements
If Statement
If..Else Statement
If...Else if ....else statement

Conditional Statements

to perform different actions for different decisions.

if statement : use this statement to execute some code only if a specified condition is true

if..else statement: Use this statement  to execute some code if the condition is true and another code if the condition is false

if...else if...else statement: Use this statement to select one of many blocks of code to be executed.

switch statement use this statement to select to select one of many bloks of code to be executed.


a {} block must contain the statements to be executed. if curly braces are not present, only the subsequent statement is executed, which is very common programming error

if(condition)
{
statement 1;
statement 2;
statement 3;
}//all three statements are executed

if(condition)
statement 1;
statement 2;
statement 3;
//only statement 1 is executed

if statement

use the if statement to execute some code only if a specified condition is true.

if(condition)
{
code to be executed if condition is true
}

if is written in lowercase letters. using Uppercase letters (IF) will generate a javascript error!

<html>
<body>
<script type="text/javascript">
var d=new Date();
var time=d.getHours();
if(time<10)
{
document.write("<b>Good morning</b>");
}
</script>

<p>This example demonstrates the if statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
</body>
</html>

If...else statement

use the if ...else statement to execute some code if a condition is true and another code is false

if(condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}


<html>
<body>

<script type="text/javascript">
var d=new Date();
var time=d.getHours();

if(time<10)
{
document.write("<b>Good Morning<</b>");
}
else
{
document.write("<b>Good day</b>");
}
</script>

<P>This example demonstrates the If...Else statement.</p>

<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>

</body>
</html>

<html>
<body>
<script type="text/javascript">
var r=Math.random();
if(r>0.5)
{
document.write("<a href='hppt://www.w3schools.com'>Learn web development!</a>");
}
else
{
document.write("<a href='http://www.refsnesdata.com'>Refsnes Data!</a>");
}
</script>
</body>
</html>

if..else if...else statement

Use the if....else if...else statement to select one of several blocks of code to be executed

if(condition)
{
code to be executed if condition1 is true
}
else if(condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}

<html>
<body>
<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>Hello World!</b>");
}
</script>
<p>
This example demonsrates the if ..else if .... else statement.
</p>
</body>
</html>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams