0% found this document useful (0 votes)
10 views

Javascript Part2

Operator precedence determines the order in which operators are evaluated, with higher precedence operators evaluated first. Conditional statements like if, else if, and switch allow executing different code blocks based on conditions. If/else if can have multiple conditions checked in order, while switch compares a value to multiple possible cases.

Uploaded by

itikhed971
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Javascript Part2

Operator precedence determines the order in which operators are evaluated, with higher precedence operators evaluated first. Conditional statements like if, else if, and switch allow executing different code blocks based on conditions. If/else if can have multiple conditions checked in order, while switch compares a value to multiple possible cases.

Uploaded by

itikhed971
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

 Operator precedence determines the order in which

operators are evaluated.


 Operators with higher precedence are evaluated first.
 Example 1
var a= 3+4*5 //returns 23
In above example (*) has higher precedence
 Example 2
var x = (10 + 3) * 5; // First add 10 and 3, then
multiply by5
When we write code for a particular program, we sometimes takes
various decisions for executing different action. These can be done
through conditional/control statements.
Conditional
Statement

If Switch

If If..else If..else..if
• It is used to execute the code whether condition is true or
false. There are three forms of if statement in JavaScript.
1. If Statement
2. If else statement
3. if else if statement
• It evaluates the content only if expression/Condition is true.
• Syntax
If (condition)
{
block of code to be executed if the condition
is true
}
Start

End
• If the expression is false, then no statement would be not executed.
• Most of the times, you will use comparison operators
while making decisions.}
• Example 1
Make a "Good day" greeting if the time is less than 18:00:
Var time=10;
if (time < 18)
{
document.write( "Good day“);
}
• Example 2
var age = 20;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}
<script>
var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>
• Use the else statement to specify a block of code to be executed if the
condition is false.
• Syntax
if (condition)
{
block of code to be executed if the condition is true
}
else
{
block of code to be executed if the condition is false
}
• If the resulting value is true, the given statement(s) in the ‘if’ block,
are executed.
• If the expression is false, then the given statement(s) in the else block
are executed.
Flowchart of JavaScript If...else statement

Start

End
Example 1
If the time is less than 18:00, create a "Good day" greeting,
otherwise "Good evening":
if (time < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Example 2
var age = 15;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}else
{
document.write("<b>Does not qualify for driving</b>");
}
Example 3
Program to find out if the number is Even /Odd
var a=20;
if(a%2==0)
{
document.write("a is even number");
}
Else
{
document.write("a is odd number");
}
Example 4
Program to find Leap Year.
var year=2001;
if(year%4==0)
{
document.write(“”Leap Year”);
}
Else
{
document.write(“Not a Leap Year");
}
Example 5
Program to Find out Positive and Negative
Numbers.
var num=2001;
if(num>0)
{
document.write(“It is Positive Number”);
}
Else
{
document.write(“It is Negative Number");
}
Example 6
Program to display result Pass/Fail.

var marks=60;
if(marks>40)
{
document.write(“Pass”);
}
Else
{
document.write(“Fail");
}
• Nested if statements means an if statement inside another
if statement.
• Syntax-
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
/*Q) Find if a number is positive and even*/
var x=8;
if(x>0)
{
document.write("<h3>Positive Number</h3>");
if(x%2==0)
{
document.write("<h3>Positive & Even
Number</h3>");
}
}
Example- Find if a number is positive, negative or 0*/
var x=8;
if(x>0)
{
document.write("<h3>Positive Number</h3>");
}
else if(x<0)
{
document.write("<h3>Negative Number</h3>");
}
else
{
document.write("<h3>Number is 0</h3>");
}
var m=40
if(m>90)
{
document.write("A");
}
else if(m>80)
{
document.write("B");
}
else if(m>70)
{
document.write("c");
}
else if(m>60)
{
document.write("D");
}
else if(m>50)
{
document.write("Pass");
}
else {
document.write("fail");
}
• You can use multiple if…else…if statements to perform a multiway
branch. However, this is not always the best solution, especially when
all of the branches depend on the value of a single variable.
• you can use a switch statement which handles this situation, and it
does it more efficiently than repeated if…else if statements.
• The switch case statement in JavaScript is also used for decision
making purposes. Consider a situation when we want to test a
variable for hundred different values and based on the test we
want to execute some task. Using if-else statement for this purpose
will be less efficient over switch case statements and also it will
make the code look messy.
• It is convenient than if..else..if because it can be used with numbers,
characters etc.
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
case valueN:
statementN;
break;
default:
statementDefault;
}
• expression can be of type numbers, strings or boolean.
• The switch expression is evaluated once.
• The default Keyword
The default statement is optional. If the expression passed to switch
does not matches with value in any case then the statement under default
will be executed.
• The break Keyword
1. When the JavaScript code interpreter reaches a break
keyword, it breaks out of the switch block.
2. This will stop the execution of more execution of code
and/or case testing inside the block.
3. The break statement is used inside the switch to terminate a
statement sequence.
4.The break statement is optional. If omitted, execution will continue on
into the next case.
• Duplicate case values are not allowed.
Flow Chart Diagram :
/*Q1) Find day of week by accepting its number.
eg. 1-> sunday, 2-> monday etc*/
var x = 3;
switch(x)
{
case 0:
document.writeln("<h1>Sunday</h1>");
break;
case 1:
document.writeln("<h1>Monday</h1>");
break;
case 2:
document.writeln("<h1>Tuesday</h1>");
break;
case 3:
document.writeln("<h1>Wednesday</h1>");
break;
case 4:
document.writeln("<h1>Thursday</h1>");
break;
case 5:
document.writeln("<h1>Friday</h1>");
break;
case 6:
document.writeln("<h1>Saturday</h1>");
break;
default:
document.writeln("<h1>Enter correct value</h1>");
}
Program to Work like a calculator
<script>
var a=45;
var b=20;
var ch=4;
var r;
switch(ch)
{
case 1:
r=a+b;
document.write(“Addition=“+r);
break;
case 2:
r=a-b;
document.write(“Subtraction=“+r);
break;
case 3:
r=a*b;
document.write(“Multiplication=“+r);
break;
case 4:
r=a/b;
document.write(“Multiplication=“+r);
break;
default:
document.write ("Not correct option/Wrong
Choice");
}

</script>

You might also like