0% found this document useful (0 votes)
10 views15 pages

Statments

The document discusses different types of conditional statements in JavaScript including if, else if, else statements and switch statements. It provides the syntax and examples of each statement to control program flow based on different conditions.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Statments

The document discusses different types of conditional statements in JavaScript including if, else if, else statements and switch statements. It provides the syntax and examples of each statement to control program flow based on different conditions.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

// console.

log("Condtional Statement");
// conditional statements: it is used to
control the flow of program.

/*

1. if staement: Use if to specify a block


of code to be executed, if a specified
condition is true.

*Syntax:->

if (condition) {
// block of code to be executed if the
condition is true
}

*Note:->

that if is in lowercase letters. Uppercase


letters (If or IF) will generate a
JavaScript error.

*Example 1:
Q:Make a "Good day" greeting if the hour is
less than 18:00:
if (hour < 18) {
greeting = "Good day";
}
ans-> Good day
*/
console.log("Example of if statements");
//example 2 of if codn:
let n = 15;
if (n >= 10) {
console.log("true");
}

//example 1 of if codn:
let age = 21;
if (age >= 18) {
console.log("person is eligible to
vote");
}

/*
2. The else Statement :

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
}

*Example 1:

Q : If the hour is less than 18, create a


"Good day" greeting, otherwise "Good
evening":

if (hour < 18) {


greeting = "Good day";
} else {
greeting = "Good evening";
}

ans:-> Good day


*/

console.log("Example of else statements");


//example 1 of else codn:
let num = 4;
if (num % 2 == 0) {
console.log("even");
} else {
console.log("odd");
}

//example 2 of else codn:


let num1 = 5;
if (num1 % 2 == 0) {
console.log("even");
} else {
console.log("odd");
}

/*
3. The else if Statement :->

Use the else if statement to specify a new


condition if the first condition is false.

*Syntax :->

if (condition1) {
// block of code to be executed if
condition1 is true
} else if (condition2) {
// block of code to be executed if the
condition1 is false and condition2 is true
} else {
// block of code to be executed if the
condition1 is false and condition2 is false
}

Example:->

Q: If time is less than 10:00, create a


"Good morning" greeting, if not, but time
is less than 20:00, create a "Good day"
greeting, otherwise a "Good evening":

if (time < 10) {


greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}

The result of greeting will be:


Good day
*/

console.log("Example of else if
statements");

//compare 2 numbers
//example 1
let a = 20;
let b = 10;

if (a > b) {
console.log(" a is greater than b. ");
} else if (a == b) {
console.log(" a is equal to b. ");
} else {
console.log(" a is less than b. ");
}

//example 2
let a1 = 10;
let b1 = 20;

if (a1 > b1) {


console.log(" a is greater than b. ");
} else if (a1 == b1) {
console.log(" a is equal to b. ");
} else {
console.log(" a is less than b. ");
}

//example 3
let a2 = 10;
let b2 = 10;

if (a2 > b2) {


console.log(" a is greater than b. ");
} else if (a2 == b2) {
console.log(" a is equal to b. ");
} else {
console.log(" a is less than b. ");
}

//to find gender of a person

let gender = "";


// let gender = "F";
// let gender = "F";
if (gender == "M") {
console.log("male");
} else if (gender == "F") {
console.log("female");
} else {
console.log("invalid gender");
}

/*
3: Switch Statement:->
it is used to perform different actions
based on different conditions switch
statement to select one of many code blocks
to be executes.

break : this keyword is used to break out


if the switch block, this stops the
execution inside code block.

Default : this keyword is used to specify a


pieace of code if no case matches the given
condition.

Syntax:
witch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

This is how it works:

1. The switch expression is evaluated once.


2. The value of the expression is compared
with the values of each case.
3. If there is a match, the associated
block of code is executed.
4. If there is no match, the default code
block is executed.
*/

//Switch stament example:


console.log("Switch staement");

// example 1
let i = 5;
switch (i) {
case 1:
console.log("i is one");
break;
case 2:
console.log("i is two");
break;
case 3:
console.log("i is three");
break;

default:
console.log("i is greater than 3");
}

// example 2
let i1 = 3;
switch (i1) {
case 1:
console.log("i is one");
break;
case 2:
console.log("i is two");
break;
case 3:
console.log("i is three");
break;

default:
console.log("i is greater than 3");
}

// example 3
let i2 = 2;
switch (i2) {
case 1:
console.log("i is one");
break;
case 2:
console.log("i is two");
break;
case 3:
console.log("i is three");
break;

default:
console.log("i is greater than 3");
}

// example 4
let i3 = 1;
switch (i3) {
case 1:
console.log("i is one");
break;
case 2:
console.log("i is two");
break;
case 3:
console.log("i is three");
break;

default:
console.log("i is greater than 3");
}

// example 5
let grade = "B";

let result;

switch (grade) {
case 'A':
result = "Excellent";
break;
case 'B':
result = "Average";
break;
case 'C':
result = "Poor";
break;
default:
result = "No grade";
}
console.log(result);

// example 6
let grade1 = "A";

let result1;

switch (grade1) {
case 'A':
result1 = "Excellent";
break;
case 'B':
result1 = "Average";
break;
case 'C':
result1 = "Poor";
break;
default:
result1 = "No grade";
}
console.log(result1);

// example 7
let grade3 = "C";
let result3;

switch (grade3) {
case 'A':
result3 = "Excellent";
break;
case 'B':
result3 = "Average";
break;
case 'C':
result3 = "Poor";
break;
default:
result3 = "No grade";
}
console.log(result3);

// example 8
let grade4 = " ";

let result4;

switch (grade4) {
case 'A':
result4 = "Excellent";
break;
case 'B':
result4 = "Average";
break;
case 'C':
result4 = "Poor";
break;
default:
result4 = "No grade";
}
console.log(result4)

You might also like