Sunday, January 18, 2009

Javascript IF ELSE IF conditional statement with AND OR NOT Operator




Please visit my new Web Site WWW.Codedisplay.com



For a javascript beginner, understanding the if-else conditional statement is the first task. Since no one can start to write any logic witout IF ELSE statement so why do not start from very basic. In this post i will try to give you a complete understaning on IF - ELSE conditional statement with AND OR NOT operator to build up the logic. For an example we have 3 requirements like:

1. I will pass a number to a function. The function give an alert if the supplied number is MAX.
2. I will pass a number to a function. The function give an alert if MAX otherwise Not MAX.
3. I will pass a number to a function. The function give an alert for MAX, MIN or EQUAL.


IF Statement:
Look at our first requirement. Here if we think normally then the answer will be we need to alert only if the supplied number is greater than function local variable. So to implement the above requirement we need to write an IF Statement like:

<script type="text/javascript">
function AlertMAX(nNumber)
{
var nFixNumber=100;
if(nNumber>nFixNumber)
alert ('Your supplied number '+nNumber+' is MAX.');
}

AlertMAX(200);
</script>

If...else Statement:
Look at the second requirement. Here if the supplied number is greater than the function defined fix loocal variable then we need to alert by 'MAX' otherwise 'Not MAX'. So in this scenario we can't build the logic without else satement. The code looks like:

<script type="text/javascript">
function AlertMAX(nNumber)
{
var nFixNumber=100;
if(nNumber>nFixNumber)
alert ('Your supplied number '+nNumber+' is MAX.');
else
alert ('Your supplied number '+nNumber+' is Not MAX.');
}

AlertMAX(50);
</script>

If...else if...else Statement:
Now look at the requirement number three. Here if the supplied number is MAX then we need to alert 'MAX' else if the supplied number is equal than we need to alert 'EQUAL' else definitely 'MIN'. So the code will be:

<script type="text/javascript">
function AlertMAX(nNumber)
{
var nFixNumber=100;
if(nNumber>nFixNumber)
alert ('Your supplied number '+nNumber+' is MAX.');
else if(nNumber==nFixNumber)
alert ('Your supplied number '+nNumber+' is EQUAL.');
else
alert ('Your supplied number '+nNumber+' is MIN.');
}

AlertMAX(100);
</script>

Now we have learned the basic structure of IF..ELSE conditional statement with proper example. But in our real lives logic building is not an easy job. To build up logic we need more complex conditional statement. Now i like to discuss how we can apply AND OR NOT operator to build up a logic.

IF statement with AND operator:
Let we have a requirement like: We will pass an employee service length to a javascript function & the function will give us a message 'One Basic' if the service length is between 0 to 5. 'Three Basic' if the service length between 6 to 10. Otherwise 'Five Basic'. So if you simply think you can understand here we need AND operator to build the logic. Javascript syntax for AND operator is &&. The code looks like:

<script type="text/javascript">
function AlertGratuity(nYear)
{
if(nYear<=5) alert ('One Basic'); else if(nYear>5 && nYear<=10) alert ('Three Basic'); else alert ('Five Basic'); } AlertGratuity(7); </script>

IF statement with OR operator:
Let we have a requirement like: We will pass an employee service length and designation to a javascript function & the function will give us a message 'Three Basic' if the service length is greater than 5 years OR he is a Manager. Otherwise 'One Basic'. So if you simply think you can understand here we need OR operator to build the logic. Javascript syntax for OR operator is . The code looks like:

<script type="text/javascript">
function AlertGratuity(nYear,sDesig)
{
if(nYear>5 sDesig=='Manager')
alert ('Three Basic');
else
alert ('One Basic');
}

AlertGratuity(3,'Manager');
</script>

The output will be "Three Basic" since his designation is Manager.

IF statement with NOT operator:
Let we have a requirement that the company never give any gratuity if his service length less than 10 years. The syntax for NOT operator is !. So the code looks like:

<script type="text/javascript">
function AlertGratuity(nYear)
{
if(!(nYear>=10))
alert ('Not Eligible');
else
alert ('Eligible');
}

AlertGratuity(3);
</script>

Hope one can implement logic with Javascript IF..ELSE..IF...ELSE conditional statement as well as can use AND, OR & NOT opeartor.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash