If-Else in JavaScript
If-else statements are fundamental building blocks for decision-making in JavaScript programs. They allow you to control the flow of your code based on whether a certain condition is True or False.
if (condition) {
// code to execute if the condition is True
}
else (condition) {
// code to execute if the condition is False
}
The if keyword is followed by a condition that is evaluated as True or False.
If the condition is True, the indented code block after the if statement is executed.
If the condition is False, the indented code block after the else statement (if present) is executed.
This program asks the user for a number. It then uses an if statement to check if the number is divisible by 2. If the remainder is 0, then the number is Even, otherwise it's Odd.
The condition can be any expression that evaluates to True or False. Common comparisons include == (equal to), != (not equal to), < (less than),> (greater than), <= (less than or equal to), and>= (greater than or equal to).
You can have multiple lines of code indented within the if and else blocks
If the condition is False, and there's no else block, no code is executed after the if statement.
Output :
Enter the no : 9
Perfect square
Enter the no : 6
Not Perfect square
Output :
Enter value of a : 3
Enter value of b : 4
Enter value of c : 5
Pythagorean Triplet
Enter value of a : 5
Enter value of b : 6
Enter value of c : 7
Not a Pythagorean Triplet
Output :
Enter a number : 5
Positive
Enter a number : -5
Negative
The elif statements are checked only if the previous if conditions are False.
1. Write a program to find largest of three numbers.
2. Write a program to accept a number and check whether the number is Buzz number or not. A number is said to be Buzz Number if it ends with 7 or is divisible by 7. Example - 1007