You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// In computer programming, there may arise situations where you have to run a block of code among more than one alternatives. For example, assigning grades A, B or C based on marks obtained by a student.
3
+
4
+
// In such situations, you can use the JavaScript if...else statement to create a program that can make decisions.
5
+
6
+
// In JavaScript, there are three forms of the if...else statement.
7
+
8
+
// if statement
9
+
// if...else statement
10
+
// if...else if...else statement
11
+
// JavaScript if Statement
12
+
// The syntax of the if statement is:
13
+
14
+
// if (condition) {
15
+
// the body of if
16
+
// }
17
+
// The if statement evaluates the condition inside the parenthesis ().
18
+
19
+
// If the condition is evaluated to true, the code inside the body of if is executed.
20
+
// If the condition is evaluated to false, the code inside the body of if is skipped.
21
+
// Note: The code inside { } is the body of the if statement.
22
+
23
+
// Working of if statement in JavaScript
24
+
// Working of the if Statement
25
+
// Example 1: if Statement
26
+
// check if the number is positive
27
+
28
+
// const number = prompt("Enter a number: ");
29
+
30
+
// check if number is greater than 0
31
+
// if (number > 0) {
32
+
// the body of the if statement
33
+
// console.log("The number is positive");
34
+
// }
35
+
36
+
// console.log("The if statement is easy");
37
+
// Run Code
38
+
// Output 1
39
+
40
+
// Enter a number: 2
41
+
// The number is positive
42
+
// The if statement is easy
43
+
// Suppose the user entered 2. In this case, the condition number > 0 evaluates to true. And, the body of the if statement is executed.
44
+
45
+
// Output 2
46
+
47
+
// Enter a number: -1
48
+
// The if statement is easy
49
+
// Suppose the user entered -1. In this case, the condition number > 0 evaluates to false. Hence, the body of the if statement is skipped.
50
+
51
+
// Since console.log("The if statement is easy"); is outside the body of the if statement, it is always executed.
52
+
53
+
// Comparison and logical operators are used in conditions. So to learn more about comparison and logical operators, you can visit JavaScript Comparison and Logical Operators.
54
+
55
+
// JavaScript if...else statement
56
+
// An if statement can have an optional else clause. The syntax of the if...else statement is:
57
+
58
+
// if (condition) {
59
+
// block of code if condition is true
60
+
// } else {
61
+
// block of code if condition is false
62
+
// }
63
+
// The if..else statement evaluates the condition inside the parenthesis.
64
+
65
+
// If the condition is evaluated to true,
66
+
67
+
// the code inside the body of if is executed
68
+
// the code inside the body of else is skipped from execution
69
+
70
+
// If the condition is evaluated to false,
71
+
72
+
// the code inside the body of else is executed
73
+
// the code inside the body of if is skipped from execution
74
+
// Working of if-else statement in JavaScript
75
+
// Working of the if...else statement
76
+
// Example 2: if…else Statement
77
+
// check if the number is positive or negative/zero
78
+
79
+
// const number = prompt("Enter a number: ");
80
+
81
+
// check if number is greater than 0
82
+
// if (number > 0) {
83
+
// console.log("The number is positive");
84
+
// }
85
+
// if number is not greater than 0
86
+
// else {
87
+
// console.log("The number is either a negative number or 0");
88
+
// }
89
+
90
+
// console.log("The if...else statement is easy");
91
+
// Run Code
92
+
// Output 1
93
+
94
+
// Enter a number: 2
95
+
// The number is positive
96
+
// The if...else statement is easy
97
+
// Suppose the user entered 2. In this case, the condition number > 0 evaluates to true. Hence, the body of the if statement is executed and the body of the else statement is skipped.
98
+
99
+
// Output 2
100
+
101
+
// Enter a number: -1
102
+
// The number is either a negative number or 0
103
+
// The if...else statement is easy
104
+
// Suppose the user entered -1. In this case, the condition number > 0 evaluates to false. Hence, the body of the else statement is executed and the body of the if statement is skipped.
105
+
106
+
// JavaScript if...else if statement
107
+
// The if...else statement is used to execute a block of code among two alternatives. However, if you need to make a choice between more than two alternatives, if...else if...else can be used.
108
+
109
+
// The syntax of the if...else if...else statement is:
110
+
111
+
// if (condition1) {
112
+
// code block 1
113
+
// } else if (condition2){
114
+
// code block 2
115
+
// } else {
116
+
// code block 3
117
+
// }
118
+
// If condition1 evaluates to true, the code block 1 is executed.
119
+
// If condition1 evaluates to false, then condition2 is evaluated.
120
+
// If the condition2 is true, the code block 2 is executed.
121
+
// If the condition2 is false, the code block 3 is executed.
122
+
// Working of if-else ladder statement in JavaScript
123
+
// Working of the if...else if...else statement
124
+
// Example 3: if...else if Statement
125
+
// check if the number if positive, negative or zero
126
+
// const number = prompt("Enter a number: ");
127
+
128
+
129
+
// check if number is greater than 0
130
+
// if (number > 0) {
131
+
// console.log("The number is positive");
132
+
// }
133
+
// check if number is 0
134
+
// else if (number == 0) {
135
+
// console.log("The number is 0");
136
+
// }
137
+
// if number is neither greater than 0, nor zero
138
+
// else {
139
+
// console.log("The number is negative");
140
+
// }
141
+
142
+
// console.log("The if...else if...else statement is easy");
143
+
// Run Code
144
+
// Output
145
+
146
+
// Enter a number: 0
147
+
// The number is 0
148
+
// The if...else if...else statement is easy
149
+
// Suppose the user entered 0, then the first test condition number > 0 evaluates to false. Then, the second test condition number == 0 evaluates to true and its corresponding block is executed.
150
+
151
+
// Nested if...else Statement
152
+
// You can also use an if...else statement inside of an if...else statement. This is known as nested if...else statement.
153
+
154
+
// Example 4: Nested if...else Statement
155
+
// check if the number is positive, negative or zero
156
+
// const number = prompt("Enter a number: ");
157
+
158
+
// if (number >= 0) {
159
+
// if (number == 0) {
160
+
// console.log("You entered number 0");
161
+
// } else {
162
+
// console.log("You entered a positive number");
163
+
// }
164
+
// } else {
165
+
// console.log("You entered a negative number");
166
+
// }
167
+
// Run Code
168
+
// Output
169
+
170
+
// Enter a number: 5
171
+
// You entered a positive number
172
+
// Suppose the user entered 5. In this case, the condition number >= 0 evaluates to true, and the control of the program goes inside the outer if statement.
173
+
174
+
// Then, the test condition, number == 0, of the inner if statement is evaluated. Since it's false, the else clause of the inner if statement is executed.
175
+
176
+
// Note: As you can see, nested if...else makes our logic complicated and we should try to avoid using nested if...else whenever possible.
177
+
178
+
// Body of if...else With Only One Statement
179
+
// If the body of if...else has only one statement, we can omit { } in our programs. For example, you can replace
180
+
181
+
// const number = 2;
182
+
// if (number > 0) {
183
+
// console.log("The number is positive.");
184
+
// } else {
185
+
// console.log("The number is negative or zero.");
186
+
// }
187
+
// Run Code
188
+
// with
189
+
190
+
// const number = 2;
191
+
// if (number > 0)
192
+
// console.log("The number is positive.");
193
+
// else
194
+
// console.log("The number is negative or zero.");
for (initialExpression; condition; updateExpression) {
7
+
for loop body
8
+
}
9
+
Here,
10
+
11
+
The initialExpression initializes and/or declares variables and executes only once.
12
+
The condition is evaluated.
13
+
If the condition is false, the for loop is terminated.
14
+
If the condition is true, the block of code inside of the for loop is executed.
15
+
The updateExpression updates the value of initialExpression when the condition is true.
16
+
The condition is evaluated again. This process continues until the condition is false.
17
+
To learn more about the conditions, visit JavaScript Comparison and Logical Operators.
18
+
19
+
Working of for loop in JavaScript with flowchart
20
+
Flowchart of JavaScript for loop
21
+
Example 1: Display a Text Five Times
22
+
program to display text 5 times
23
+
const n = 5;
24
+
25
+
looping from i = 1 to 5
26
+
for (let i = 1; i <= n; i++) {
27
+
console.log(`I love JavaScript.`);
28
+
}
29
+
Run Code
30
+
Output
31
+
32
+
I love JavaScript.
33
+
I love JavaScript.
34
+
I love JavaScript.
35
+
I love JavaScript.
36
+
I love JavaScript.
37
+
Here is how this program works.
38
+
39
+
Iteration Variable Condition: i <= n Action
40
+
1st i = 1
41
+
n = 5 true I love JavaScript. is printed.
42
+
i is increased to 2.
43
+
2nd i = 2
44
+
n = 5 true I love JavaScript. is printed.
45
+
i is increased to 3.
46
+
3rd i = 3
47
+
n = 5 true I love JavaScript. is printed.
48
+
i is increased to 4.
49
+
4th i = 4
50
+
n = 5 true I love JavaScript. is printed.
51
+
i is increased to 5.
52
+
5th i = 5
53
+
n = 5 true I love JavaScript. is printed.
54
+
i is increased to 6.
55
+
6th i = 6
56
+
n = 5 false The loop is terminated.
57
+
Example 2: Display Numbers from 1 to 5
58
+
program to display numbers from 1 to 5
59
+
const n = 5;
60
+
61
+
looping from i = 1 to 5
62
+
in each iteration, i is increased by 1
63
+
for (let i = 1; i <= n; i++) {
64
+
console.log(i); // printing the value of i
65
+
}
66
+
Run Code
67
+
Output
68
+
69
+
1
70
+
2
71
+
3
72
+
4
73
+
5
74
+
Here is how this program works.
75
+
76
+
Iteration Variable Condition: i <= n Action
77
+
1st i = 1
78
+
n = 5 true 1 is printed.
79
+
i is increased to 2.
80
+
2nd i = 2
81
+
n = 5 true 2 is printed.
82
+
i is increased to 3.
83
+
3rd i = 3
84
+
n = 5 true 3 is printed.
85
+
i is increased to 4.
86
+
4th i = 4
87
+
n = 5 true 4 is printed.
88
+
i is increased to 5.
89
+
5th i = 5
90
+
n = 5 true 5 is printed.
91
+
i is increased to 6.
92
+
6th i = 6
93
+
n = 5 false The loop is terminated.
94
+
Example 3: Display Sum of n Natural Numbers
95
+
program to display the sum of natural numbers
96
+
let sum = 0;
97
+
const n = 100
98
+
99
+
looping from i = 1 to n
100
+
in each iteration, i is increased by 1
101
+
for (let i = 1; i <= n; i++) {
102
+
sum += i; // sum = sum + i
103
+
}
104
+
105
+
console.log('sum:', sum);
106
+
Run Code
107
+
Output
108
+
109
+
sum: 5050
110
+
Here, the value of sum is 0 initially. Then, a for loop is iterated from i = 1 to 100. In each iteration, i is added to sum and its value is increased by 1.
111
+
112
+
When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + ... + 100.
113
+
114
+
The above program to add sum of natural numbers can also be written as
115
+
116
+
program to display the sum of n natural numbers
117
+
let sum = 0;
118
+
const n = 100;
119
+
120
+
looping from i = n to 1
121
+
in each iteration, i is decreased by 1
122
+
for(let i = n; i >= 1; i-- ) {
123
+
adding i to sum in each iteration
124
+
sum += i; // sum = sum + i
125
+
}
126
+
127
+
console.log('sum:',sum);
128
+
Run Code
129
+
This program also gives the same output as the Example 3. You can accomplish the same task in many different ways in programming; programming is all about logic.
130
+
131
+
Although both ways are correct, you should try to make your code more readable.
132
+
133
+
JavaScript Infinite for loop
134
+
If the test condition in a for loop is always true, it runs forever (until memory is full). For example,
135
+
136
+
infinite for loop
137
+
for(let i = 1; i > 0; i++) {
138
+
block of code
139
+
}
140
+
In the above program, the condition is always true which will then run the code for infinite times.
141
+
142
+
In the next tutorial, you will learn about while and do...while loop.
0 commit comments