Javascript conditional statements 1

901 views
744 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
901
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Javascript conditional statements 1

  1. 1. JavaScript if...else Statements
  2. 2. REVIEW<html><body><script>var x= prompt("Who created the JavaScript? "," ");var y= "Brendan Eich", n= "brendan eich", m= "Brendan"var z= (x==y||x==n||x==m)?"Correct": "Wrong";alert(""+z+"");document.write("Correct Answer: "+y+"");</script>
  3. 3. Identify if correct or error source codeIf error ,find code/s or missing codes that will cause error.7. var x= prompt(What is kbps stand for? “ ,””);8. Var y= “kilobytes per seconds”9. var z= (x==y) Correct: Wrong ;10. Confirm(Are you sure);11. msdocument.wrirte(The correct answer is “+z+”);12. var x= parsefloatprompt(What number is less than 10? “,”)
  4. 4. JAVASCRIPT: CONDITIONAL STATEMENTS• While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform right actions. Reference: Tutorial Points.com• Sometimes when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. Conditional statements are the set of commands used to perform different actions based on different conditions. JavaScript supports two conditional statements: if...else and switch. Both perform in saw way the same task. Reference:WebCheatSheet.com
  5. 5. JavaScript supports conditional statements which are used to perform different actions based on different conditions. Here we will explain if..else statement.JavaScript supports following forms of if..else statement:if statementif...else statementif...else if... statement.
  6. 6. if statement:The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.Syntax:if (expression)Statement(s) to be executed if expression is true }Here JavaScript expression is evaluated. If the resulting valueis true, given statement(s) are executed.If expression is false then no statement would be not executed.Most of the times you will use comparison operators whilemaking decisions.
  7. 7. Example:<html><body><script >var x=prompt(“HTML stands for?”,””);var y= “Hypertext Mark Up Language”if(x==y)alert(“Correct");If(x!=y)alert(“Wrong”);</script></body></html>
  8. 8. if...else statement:The if...else statement is the next form of control statement that allows JavaScript to execute statements in more controlled way.Syntax:if (expression){ Statement(s) to be executed if expression is true }else{ Statement(s) to be executed if expression is false}Here JavaScript expression is evaluated. If the resulting valueis true, given statement(s) in the if block, are executed.If expression is false then given statement(s) in the else block, areexecuted.
  9. 9. EXAMPLE<html><body><script>var x= parseFloat(prompt("Kilobytes is consist of how many bytes? "," "));var y= "1000"if(x==y){ alert("Yes, Kilobytes is equal to "+x+" bytes");}else{ alert("No, Kilobytes is not equal to "+x+" bytes");}</script></body></html>
  10. 10. if...else if... statement:The if...else if... statement is the one level advance form of control statement that allows JavaScript to make correct decision out of several conditions.There is nothing special about this code. It is just a series of if statements, where each if is part of the else clause of the previous statement. Statement(s) are executed based on the true condition, if non of the condition is true then else block is executed
  11. 11. Syntax:if (expression 1){Statement(s) to be executed if expression 1 is true}else if (expression 2){ Statement(s) to be executed if expression 2 is true}else if (expression 3){Statement(s) to be executed if expression 3 is true}else{Statement(s) to be executed if no expression is true}
  12. 12. <script>var x= parseFloat(prompt("Enter age? "," "));var y= 18if(x>y){ alert("Yes, You are qualified to create your account");}else if(x<y){ alert("No, You are not qualified to create your account");}else if(x==y){ alert("Sorry, consent with your parents is needed")}</script>
  13. 13. EXERCISES/COMPUQUIZWrite a JavaScript source code using if…else conditional statements.var year= Enter your birth yearExpression and statements1970-1975 Your age is between 38-43 years old1976-1980 Your age is between 33-37 years old1981-1985 Your age is between 28-32 years old1986-2013 Your age is between 0-27 year/s old

×