Skip to content

Commit 7e780ef

Browse files
authored
Merge pull request avsingh999#12 from smitpatel24/main
Added if_statement.md
2 parents cf8ee0b + 5631170 commit 7e780ef

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

if_statement.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# if statements in JavaScript
2+
Decision Making in programming is similar to decision making in real life. In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled.
3+
A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program.
4+
5+
JavaScript’s conditional statements:
6+
7+
- if
8+
- if-else
9+
- nested-if
10+
- if-else-if
11+
12+
These statements allow you to control the flow of your program’s execution based upon conditions known only during run time.
13+
14+
## if:
15+
if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
16+
### Syntax:
17+
```if(condition)
18+
{
19+
// Statements to execute if
20+
// condition is true
21+
}
22+
```
23+
Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements under it.
24+
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block. For example,
25+
26+
```
27+
if(condition)
28+
statement1;
29+
statement2;
30+
31+
// Here if the condition is true, if block
32+
// will consider only statement1 to be inside
33+
// its block.
34+
```
35+
### Example:
36+
```
37+
var i = 10;
38+
39+
if (i > 15)
40+
document.write("10 is less than 15");
41+
42+
// This statement will be executed
43+
// as if considers one statement by default
44+
document.write("I am Not in if");
45+
```
46+
47+
### Output:
48+
```
49+
I am Not in if
50+
```
51+
## if-else:
52+
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
53+
### Syntax:
54+
```
55+
if (condition)
56+
{
57+
// Executes this block if
58+
// condition is true
59+
}
60+
else
61+
{
62+
// Executes this block if
63+
// condition is false
64+
}
65+
```
66+
### Example:
67+
```
68+
var i = 10;
69+
70+
if (i < 15)
71+
document.write("10 is less than 15");
72+
else
73+
document.write("I am Not in if");
74+
```
75+
### Output:
76+
```
77+
i is smaller than 15
78+
```
79+
## nested-if:
80+
A nested if is an if statement that is the target of another if or else. Nested if statements means an if statement inside an if statement. Yes, JavaScript allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
81+
### Syntax:
82+
```
83+
if (condition1)
84+
{
85+
// Executes when condition1 is true
86+
if (condition2)
87+
{
88+
// Executes when condition2 is true
89+
}
90+
}
91+
```
92+
### Example:
93+
```
94+
var i = 10;
95+
96+
if (i == 10) {
97+
98+
// First if statement
99+
if (i < 15)
100+
document.write("i is smaller than 15");
101+
102+
// Nested - if statement
103+
// Will only be executed if statement above
104+
// it is true
105+
if (i < 12)
106+
document.write("i is smaller than 12 too");
107+
else
108+
document.write("i is greater than 15");
109+
}
110+
```
111+
### Output:
112+
```
113+
i is smaller than 15
114+
i is smaller than 12 too
115+
```
116+
## if-else-if ladder:
117+
Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
118+
### Syntax
119+
```
120+
if (condition)
121+
statement;
122+
else if (condition)
123+
statement;
124+
.
125+
.
126+
else
127+
statement;
128+
```
129+
### Example:
130+
```
131+
var i = 20;
132+
133+
if (i == 10)
134+
document.write("i is 10");
135+
else if (i == 15)
136+
document.write("i is 15");
137+
else if (i == 20)
138+
document.write("i is 20");
139+
else
140+
document.write("i is not present");
141+
```
142+
### Output:
143+
```
144+
i is 20
145+
```

0 commit comments

Comments
 (0)