Skip to content

Commit 78456e9

Browse files
committed
Added control flow
1 parent 7223eb1 commit 78456e9

File tree

2 files changed

+251
-3
lines changed

2 files changed

+251
-3
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,19 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
3737
- [Logical ](./docs/operators.md#-logical)
3838
- [Assignment](./docs/operators.md#-assignment)
3939
- [Ternary](./docs/operators.md#-ternary)
40-
- Control flow
41-
- Conditional statements
42-
- Loops
40+
- [Control flow](./docs/control-flow.md)
41+
- [Conditional statements](./docs/control-flow.md#-conditional-statements)
42+
- [if statement](./docs/control-flow.md#-if-statement)
43+
- [if else statement](./docs/control-flow.md#-if-else-statement)
44+
- [else if statement](./docs/control-flow.md#-else-if-statement)
45+
- [switch statement](./docs/control-flow.md#-switch-statement)
46+
- [Loops](./docs/control-flow.md#-loops)
47+
- [for loop](./docs/control-flow.md#-for-loop)
48+
- [for in loop](./docs/control-flow.md#-for-in-loop)
49+
- [for of loop](./docs/control-flow.md#-for-of-loop)
50+
- [while loop](./docs/control-flow.md#-while-loop)
51+
- [do while loop](./docs/control-flow.md#-do-while-loop)
52+
- [comparison](./docs/control-flow.md#-comparison)
4353
2. Functions:
4454
- Defining functions
4555
- Calling functions

docs/control-flow.md

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
## ⚑ Control Flow
2+
Control flow refers to the order of the statements execution in a JavaScript program. It allows to make decisions, repeat actions, and control the flow of your code based on certain conditions.
3+
4+
*There are 2 Type Control Flow in JavaScript.*
5+
6+
### ☴ Overview:
7+
1. [Condition Statements](#-conditional-statements)
8+
- [if statement](#-if-statement)
9+
- [if else statement](#-if-else-statement)
10+
- [else if statement](#-else-if-statement)
11+
- [switch statement](#-switch-statement)
12+
2. [Loops](#-loops)
13+
- [for loop](#-for-loop)
14+
- [for in loop](#-for-in-loop)
15+
- [for of loop](#-for-of-loop)
16+
- [while loop](#-while-loop)
17+
- [do while loop](#-do-while-loop)
18+
- [Comparison](#-comparison)
19+
20+
### ✦ Conditional Statements:
21+
It is used to execute different code blocks based on specific conditions. There are 4 type of conditional statements.
22+
23+
### ✦ If Statement:
24+
It executes a block of code only if a condition is true.
25+
26+
*Syntax:*
27+
```javascript
28+
if (condition) {
29+
// Code to be executed if condition is true
30+
}
31+
```
32+
33+
```javascript
34+
let age = 25;
35+
36+
if (age >= 18) {
37+
console.log("You are an adult.");
38+
}
39+
```
40+
41+
### ✦ If Else Statement:
42+
It executes a block of code if the condition is true, otherwise it will execute an another block of code.
43+
44+
*Syntax:*
45+
```javascript
46+
if (condition1) {
47+
// Code to be executed if condition is true
48+
} else {
49+
// Code to be executed if condition is false
50+
}
51+
```
52+
53+
```javascript
54+
let age = 25;
55+
56+
if (age >= 18) {
57+
console.log("You are an adult.");
58+
} else {
59+
console.log("You are a minor");
60+
}
61+
```
62+
63+
### ✦ Else If Statement:
64+
It executes a block of code if the condition is true otherwise it executes another block of code when another condition is true. It also called as nested if statements.
65+
66+
*Syntax:*
67+
```javascript
68+
if (condition1) {
69+
// Code to be executed if condition1 is true
70+
} else if (condition2) {
71+
// Code to be executed if condition1 is false and condition2 is true
72+
}
73+
```
74+
75+
```javascript
76+
let score = 85;
77+
78+
if (score >= 90) {
79+
console.log("Excellent");
80+
} else if (score >= 80) {
81+
console.log("Good");
82+
} else {
83+
console.log("Needs improvement");
84+
}
85+
```
86+
87+
### ✦ Switch Statement:
88+
It executes different code blocks based on the value of an expression.
89+
90+
*Break statement is necessary to stop at specific case condition. Otherwise it will execute further statments as well.*
91+
92+
*Syntax:*
93+
```JavaScript
94+
switch (expression) {
95+
case value1:
96+
// Code to be executed if expression is equal to value1
97+
break;
98+
case value2:
99+
// Code to be executed if expression is equal to value2
100+
break;
101+
default:
102+
// Code to be executed if no case matches
103+
break;
104+
}
105+
```
106+
107+
```javascript
108+
let day = "Sunday";
109+
110+
switch (day) {
111+
case "Saturday":
112+
console.log("It's weekend!");
113+
break;
114+
case "Sunday":
115+
console.log("It's weekend!");
116+
break;
117+
default:
118+
console.log("It's working day!");
119+
}
120+
```
121+
122+
123+
### ✦ Loops:
124+
Loops are used to repeatedly execute a block of code until a certain condition is met.
125+
126+
*A Loop has 3 important states which are used to determine the number of times to execute.*
127+
- Iteration Initialization
128+
- Condition Check
129+
- Increment/Decrement of Iteration
130+
131+
### ✦ For Loop:
132+
It executes a block of code a specified number of times.
133+
134+
*For loop has iteration initialization, condition check, increment/decrement of iteration are inline.*
135+
136+
*Syntax:*
137+
```javascript
138+
for (initialization; condition; increment/decrement) {
139+
// Code to be executed
140+
}
141+
```
142+
143+
```javascript
144+
for (let i = 0; i < 5; i++) {
145+
console.log(i);
146+
}
147+
```
148+
149+
### &#10022; For In Loop:
150+
It iterates over the properties of an object.
151+
152+
*Syntax:*
153+
```javascript
154+
for (let property in object) {
155+
// Code to be executed
156+
}
157+
```
158+
159+
```javascript
160+
let person = { name: "kumar", age: 30 };
161+
162+
for (let key in person) {
163+
console.log(key + ": " + person[key]);
164+
}
165+
```
166+
167+
### &#10022; For Of Loop:
168+
It iterates over the values of an iterable object such as arrays, strings.
169+
170+
*Syntax:*
171+
```javascript
172+
for (let value of iterable) {
173+
// Code to be executed
174+
}
175+
```
176+
177+
```javascript
178+
let fruits = ["apple", "banana", "orange"];
179+
180+
for (let fruit of fruits) {
181+
console.log(fruit);
182+
}
183+
```
184+
185+
186+
### &#10022; While Loop:
187+
It executes a block of code as long as a condition is true. This kind of loop is useful for the condition is decided based on the block of code in this loop.
188+
189+
*Syntax:*
190+
```javascript
191+
while (condition) {
192+
// Code to be executed
193+
// condition is decided based on the calculation or process or execution
194+
}
195+
```
196+
197+
```javascript
198+
let number = 5;
199+
var factorial = 1;
200+
while (number > 0) {
201+
factorial *= number;
202+
number--;
203+
}
204+
console.log(factorial);
205+
```
206+
207+
### &#10022; Do While Loop:
208+
It executes a block of code at least once, then repeats as long as a condition is true.
209+
210+
*Syntax:*
211+
```javascript
212+
do {
213+
// Code to be executed
214+
} while (condition);
215+
```
216+
217+
```javascript
218+
let number = 5;
219+
var factorial = 1;
220+
do {
221+
factorial *= number;
222+
number--;
223+
}while (number > 0);
224+
console.log(factorial);
225+
```
226+
227+
### &#10022; Comparison:
228+
- Conditional Statements:
229+
- These statements are used to decide the flow of code or execution.
230+
- Loops:
231+
- These loops are used to execute certain process or code or statements again and again. To reduce repeated lines of code.
232+
233+
---
234+
[&#8682; To Top](#-control-flow)
235+
236+
[&#10094; Previous Topic](./operators.md) &emsp; [Next Topic &#10095;](./functions.md)
237+
238+
[&#8962; Goto Home Page](../README.md)

0 commit comments

Comments
 (0)