If Statement
If statement is used to run a specific block of code based on certain condition. There are two parts of if statement, condition check and body which is executed if condition is true.
Syntax
if( condition ){
Body code block
}
Javascript If Else Statement
In if statement, if condition is false than nothing is executed. In if else statement, one of two code block is executed based on condition. Two parts of if else statement are if part and else part. If condition is true than if part is run and if condition is false than else part is executed.
Syntaxif( condition )
if body
else{
else body
}
Extended if else statements
You can have multiple if else statements checks.
Syntax
if( condition ){code..
}else if(condition){
code..
}else if(condition){
code..
}else{
code..
}
Nested if statement
You can write if statement in another if statement.
Syntax
if( condition ){if(condition){
Condition is checked using followings conditional operators.
Conditional Operators / Ternary Operator Javascript
These are binary operators because two operands are required to check a condition.
Less than (<)
It is used to check, if first operand is less than the second. Below are few possible conditions checks.
if(3 < 5) true as 3 is less
then 5
if(5 < 3) false as 5 is not
less then 3
if(4 < 4) false as 4 is not
less than 4
Less than or equal to (<=)
This operator is used to check, whether first operand is less than or equal to second operand as
if(2 <= 5) true as 2 is less than 5Greater than (>)
Condition is true if first value is greater than second value as
if(4 > 2) true as 4 is greater than 2
if(5 > 9) false as 5 is not
greater than 9
if(3 > 3) false as 3 is not greater than 3
Greater than or equal to (>=)
Whether first value is greater or equal to second value as
if(9 >= 2) true as 9 is greater than 2
if(7 >= 8) false as 7 is not greater than or equal to 8
if(12 >= 12) true as 12 is
equal to 12
If first value is equal to second value, than condition if true otherwise it is false as
if(6 == 6) true as 6 is equal to 6if(5 == 4) false as 5 is not equal to 5
Not equal to (!=)
If both operands are unequal then condition is true as
if(8 != 9) true as 8 is not equal to 9Problem
Now we create Grading System in which we calculate the grade based on subject marks.
- We write a function gradeCalculator taking two parameters obtained marks and total marks.
- First we calculate percent marks by dividing obtained marks by total marks and multiplying with 100
- Than we declare a variable grade
- Now in if else statement, we check condition and assign grade A-D and Fail to variable grade. Finally we return grade.
- At the end we call function gradCalculator, pass obtained marks and total marks. A variable sgrade store the grade returned by function.
- We show the output in console
const gradeCalculator = function(obtMarks, totalMarks)
{
var percentMarks = (obtMarks/totalMarks)*100;
var grade = null;
if(percentMarks >= 90){
grade = 'A';
}
else if(percentMarks >= 80){
grade = 'B'
}
else if(percentMarks >= 70){
grade = 'C'
}
else if(percentMarks >= 60){
grade = 'D'
}
else if(percentMarks < 60){
grade = 'Fail'
}
return grade;
}
let marks = 87;
let total = 100;
let sgrade = gradeCalculator(marks,total);
console.log(sgrade);
Logical Operators
Ternary operators are used to check single condition. Sometime we need to check compound conditions. Here logical operators are used between each two conditions. There are three types of logical operators.
AND Operator (&&)
It returns true if both conditions are true. In case any one condition becomes false, the overall compound condition is false.
Syntax
if(first condition && second condition)OR Operator (||)
It returns true if any one condition is true and false if all conditions are false.
Syntax
if(first condition || second condition)- We create two variable name and age
- Next we write if else statement. We write compound condition with logical operator AND in if condition and OR in else if condition.
- if name is John and age is less then 20 (both conditions), than first condition will be true and its body will be executed
- if name is Wick or age is greater than or equal to 25 (any condition), then second condition will be true and its body will be executed.
var name = 'John';
var age = 25;
if(name == 'John' && age < 20)
{
console.log('if condition true');
}
else if(name == 'Wick' || age >= 25)
{
console.log('if else condition true')
}
- In first compound condition, name is John (true) but age is not less than 20 (false) so compound condition is false.
- In second compound condition, name is Wick (false) but age is equal to 25 (true) so compound condition is true.
Post a Comment