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

JavaScript If Statement

Uploaded by

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

JavaScript If Statement

Uploaded by

budi utomo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Day 04: Decision Making Structure in JS

• Decision making in JavaScript

• If statement in JavaScript

• Problem with solution in JavaScript

• If else statement in JavaScript

• Problem with solution in JavaScript

• If else if statement in JavaScript

• Problem with solution

• Switch statement in JavaScript

• Problem related to switch statement and solution


Decision Making Structure in JS
We can use decision making structure, in which our code decide to execute statement OR set of statements according to
given condition.
Example:
Display message ”Welcome” if day is Friday
Display message, “you are eligible for the admission of MCS”, if your marks greater than 1000
Show a message “It is even number” if user input a number that divisible by 2.

There are different decision making structure used in JavaScript:


Following types of decision structure work with given condition!
• if statement
• if...else statement
• if...else if... statement.
If statement in JavaScript
if statement Example
It take a condition, if condition is true, then it execute a var marks =60;
statement or set of statement.
if( marks > 70 )
if condition is false, no thing to display.
{
Syntax
document.write("Qualified for admission");
if (expression/condition) {
}
Statement or set of statement
}
Problem: If statement in JavaScript
->Write A Program In JavaScript To Get A Number From User And Display Message If
Number Is Even
if else statement in JavaScript
if else statement Example
It take a condition, if condition is true, then it execute a var marks =60;
statement or set of statement. If condition is false, it will
if( marks > 70 )
display a false like statement or set of statements.
{
Syntax
document.write("Qualified for admission");
if (expression/condition) {
}
Statement or set of statement
else {
}
document.write(“Sorry, not Qualified");
else {
}
Statement or set of statement
}
Problem: If else statement in JavaScript
->Write A Program In JavaScript To Get A Number From User And Display Message If
Number Is Even, If Not Then Sorry Message Should Display
if else if statement in JavaScript
if else if statement Statement or set of statement

• It take a condition, if condition is true, then it execute a }


statement or set of statement. else if (expression/condition) {
• If condition is false then again, it check another condition Statement or set of statement
• if that is also false, again it check another condition }

• So it will check conditions until it match else if (expression/condition) {

• if did not match with any condition, then it will execute Statement or set of statement
statement that are written inside the else block at the }
end else {
Syntax
Statement or set of statement
if (expression/condition) {
}
Statement or set of statement
}
else if (expression/condition) {
If-else if statement Example
Example }
var marks =60; else if (marks > 60) {
if( marks > 90 ) document.write(“You are qualified for 60%
scholarship ");
{
}
document.write(“You are qualified for full
scholarship"); else {
} document.write(“Sorry! We cannot provide any
scholarship on this base");
else if (marks > 80) {
}
document.write(“You are qualified for 80%
scholarship ");
}
else if (marks > 70) {
document.write(“You are qualified for 70%
scholarship ");
Problem: If-else if statement in JS
->Write A JavaScript Program To Display Grade Of Student On The Basis Of Student Marks
According To Following Criteria:

If Marks == 100, Grade Is A+


If Marks >= 90, Grade Is A
If Marks >= 80, Grade Is B
If Marks >= 70, Grade Is C
If Marks >= 60, Grade Is D
Other Wise Fail Consider
Switch Statement in JavaScript
It is alternative of if else if statement
Syntax case 4:
switch(variable/expression) { // block of code
case 1: break;
// block of code default:
break; // block of code
case 2: }
// block of code
break;
case 3:
// block of code
break;
Switch Statement in JavaScript
Switch Statement

• It is alternative of if else if statement

• It take condition which match with different cases

• We can provide more cases as we want

• If condition is matched with any case, then statement or set of statement of that case will execute.

• Break statement: It will break the execution of the statements. It will break the control to execute more

• Default statement: If condition is not match with any case, then statement in side the default statement
will execute
Switch Problem in JS
 Write A JS Program To Get A Character From User To Check It Is Vowel Or
Consonant
Switch Problem Solution in JS
Var char = ‘A’ break;
switch(‘A’) { case ‘O’:
case ‘A’: case ‘o’:
case ‘a’: document.write(“It is vowel”);
document.write(“It is vowel”); break;
break; case ‘U’:
case ‘E’: case ‘u’:
case ‘e’: document.write(“It is vowel”);
document.write(“It is vowel”); break;
break; default:
case ‘I’: document.write(“It is consonant”);
case ‘i’: }
document.write(“It is vowel”);

You might also like