In programming, an if statement is a conditional statement used to execute a block of code if a certain condition is true.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var number = 100;
if (number < 100) {
alert("Number is less then 100");
}
if (number == 100) {
document.write("They are same");
}
</script>
</body>
</html>
This code defines a variable called number
with a value of 100, and then uses two if
statements to check its value.
The first if
statement checks whether number
is less than 100. Since number
is equal to 100, this condition is not true, and the code inside the block (alert("Number is less than 100");
) is not executed.
The second if
statement checks whether number
is equal to 100. Since number
is indeed equal to 100, the code inside the block (document.write("They are same");
) is executed, and the text "They are same" is written to the document.
Parentheses ()
are used to group expressions and determine their evaluation order, while curly braces {}
are used to group statements together to form a block of code.
In the case of an if
statement, the parentheses are used to enclose the condition or expression that is being evaluated. The condition can be a simple comparison, or a more complex expression involving logical operators such as &&
(AND) or ||
(OR). The braces are used to enclose the statements that should be executed if the condition is true. By using braces, we can group multiple statements together, ensuring that they are all executed if the condition is true.
It is good practice to always use braces with an if
statement, even if there is only a single statement to be executed. This helps avoid errors and ensures that the code is more readable and maintainable.
A block of code is a section of program statements that are grouped together and treated as a single unit. A block is usually delimited by curly braces {} and can contain any number of statements, including other blocks of code. Blocks are used to group statements together so that they can be executed together as a unit, or to create a new scope for variables and functions declared inside the block. Blocks can also be used to create conditional code that only executes if a certain condition is met, such as with an if statement or a loop.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var number = 123;
if (number < 100) {
alert("Number is less then 100");
}
if (number == 100) {
document.write("They are same");
}
if (number > 100) {
alert("Our real number is bigger than starting number");
}
</script>
</body>
</html>
The code defines a variable number
with a value of 123, and then uses a series of if
statements to check its value.
The first if
statement checks if number
is less than 100, which is not true, so the code inside the statement will not be executed.
The second if
statement checks if number
is equal to 100, which is also not true, so the code inside the statement will not be executed.
The third if
statement checks if number
is greater than 100, which is true, so the code inside the statement will be executed and an alert message will appear saying "Our real number is bigger than starting number".
No comments:
Post a Comment