Skip to content

Commit 68bb420

Browse files
For Loop
1 parent c350e81 commit 68bb420

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

23.For_Loops/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Java Tutorial: The for Loop in Java
2+
3+
### For loop:
4+
- For loop in java is used to iterate a block of code multiple times.
5+
- Use for loop only when the exact number of iterations needed is already known to you.
6+
- Syntax :
7+
```
8+
/* for (initialize; check_bool_expression; update){
9+
//code;
10+
} */
11+
```
12+
- Initializer: Initializes the value of a variable. This part is executed only once.
13+
- check_bool_expression: The code inside the for loop is executed only when this condition returns true.
14+
- update: Updates the value of the initial variable.
15+
- Example :
16+
```
17+
for (i=7; i!=0; i--){
18+
System.out.println(i);
19+
}
20+
```
21+
- The above for loop initializes the value of i=7 and keeps printing as well as decrementing the value of i till i do not get equals to 0.
22+
23+
### Flow control of for loop :
24+
25+
![image](https://user-images.githubusercontent.com/70385488/151354811-2f095c94-83f7-4579-9f65-0898531cdbc5.png)
26+
27+
**Quick Quiz 1:** Write a program to print first n odd numbers using a for loop.
28+
29+
**Quick Quiz 2:** Write a program to print first n natural numbers in reverse order.
30+
31+
### Code as described in the video :
32+
33+
```
34+
public class cwh_23_for_loop {
35+
public static void main(String[] args) {
36+
// for (int i=1; i<=10; i++){
37+
// System.out.println(i);
38+
// }
39+
// 2i = Even Numbers = 0, 2, 4, 6, 8
40+
// 2i+1 = Odd Numbers = 1, 3, 5, 7, 9
41+
//int n = 3;
42+
//for (int i =0; i<n; i++){
43+
// System.out.println(2*i+1);
44+
//}
45+
46+
for(int i=5; i!=0; i--){
47+
System.out.println(i);
48+
}
49+
}
50+
}
51+
```
52+
53+
**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-23/Chapter_5.pdf)**
54+
55+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-23/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)