JS-IV Conditional Statements Notes - 835428
JS-IV Conditional Statements Notes - 835428
Conditional Statements
Conditional statements are used to decide the flow of execution based on different
conditions. If a condition is true, you can perform one action and if the condition is
false, you can perform another action.
Through Conditional Statements, we can control which code needs to run or which
code will not run.
For Ex: let's understand with the analogy, the traffic light controls the flow of
vehicles on the road. Depending upon the colour of light, the actions happened.
If light is green , then it is asignal to move whereas if the light is red then it is a
signal to move.
Based on the comparision , if the comparison is true then it will execute the one
block of code otherwise another block of code.
1. If statement
2. If…Else statement
if Statement
Conditional Statements 1
It is to specify a block of JavaScript code to be executed if a condition is true.
Syntax
condition
block of code to be executed if the condition is true
if()
It takes a Boolean Value or the expression that will give boolean value.
if() { }
console.log("Code Start")
if(true) {
console.log("Inside Code")
}
console.log("Code End")
b) If with Expression
The decision is based on the value of Expression
For Example :
if(5>3){
console.log("Inside Code");
}
c) If with Variables
The decision is based on the value of Expression
For Example :
Conditional Statements 2
var name2 = "rahul";
var check = (name1==name2);
if(check){
console.log("Both Names are same");
}
var a = 2;
var b = 3;
var c = (a==b);
if(c)
{
console.log("a and b are equal");
}
if/else Statement
The if...else is a type of conditional statement that will execute a block of code
when the condition in the if statement is truthy . If the condition is falsy , then
the else the block will be executed.
false
0 (zero)
0 (negative zero)
0n (BigInt zero)
null
undefined
Conditional Statements 3
Else another block of code executes.
Syntax
condition
var a = 3;
var b = 20;
if(a>b)
{
console.log("a is greater");
}
else
{
console.log("a is not greater");
}
if(name1==name2)
{
console.log("Names are Equal");
}
else
{
console.log("Names are not equal");
}
Conditional Statements 4
Given total_bill, discount_start_price if you satisfy the condition Print Discount
Availaible Otherwise print No Discount
if(total_bill>=discount_start_price){
console.log("Discount Availaible");
}
else{
console.log("No discount");
}
Else-if Statement
There will be times where you want to test multiple conditions. That is where
the else if the block comes in.
When the if statement is false , the computer will move onto the else
if statement. If that is also false , then it will move onto the else block.
Syntax
condition1
Conditional Statements 5
Total Bill Discount Applied
Code 5 : For a Restaurant, write the program for the following total_bill > 500
Then print 10% discount total_bill > 1000 Then print 20% discount Otherise No
discount
If-Else-If vs if-if-if :
Code 6 : If-Else-If
*My mother told me to get any one of the thing from the market
if(rice_availaible)
{
Conditional Statements 6
console.log("Buy rice");
}
else if(wheat_availaible)
{
console.log("Buy Wheat");
}
else if(apple_availaible)
{
console.log("Buy apple");
}
else
{
console.log("Nothing is availaible");
}
Code 7 : If - If - If
*My mother told me to get all of the thing if available from the market
if(rice_availaible)
{
console.log("Buy rice");
}
if(wheat_availaible)
{
console.log("Buy Wheat");
}
if(apple_availaible)
{
console.log("Buy apple");
}
Conditional Statements 7
Code 8 : Solve the Marriage Problem
Legal Age in India Males ----> 21
Females ----> 18
if(gender == "male")
{
if(age>=21)
{
console.log("Males : get marry");
}
else
{
console.log("Males : Can't get marry");
}
else
{
if(age>=18){
console.log("Females : get marry");
}
else{
console.log("Females : Can't get marry");
}
}
Code 9 : Given a char , you need to print whether the char is a vowel or not
vowels : a, e, i, o, u
if(char == "a")
{
console.log("vowel");
}
else if(char == "e")
{
console.log("vowel");
}
else if(char == "i")
Conditional Statements 8
{
console.log("vowel");
}
else if(char == "o")
{
console.log("vowel");
}
else if(char == "u")
{
console.log("vowel");
}
else{
console.log("Not a vowel");
}
Not Operator
On applying to a boolean value, the not operator turns true to false and false to true.
Conditional Statements 9