Skip to content

Commit d554468

Browse files
new file: Chapter_4/02_Loop_Types.c
new file: Chapter_4/03_While_Loop.c new file: Chapter_4/Quick_Quiz/Quick_Quiz.c new file: Chapter_4/Quick_Quiz/Solution.c
1 parent dc8655d commit d554468

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

Chapter_4/02_Loop_Types.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
There are three types of loops
3+
1. While Loop
4+
2. Do-While Loop
5+
3. For Loop
6+
*/

Chapter_4/03_While_Loop.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Syntax:
3+
while(condition){
4+
//code that runs in loop until the condition becomes false.
5+
}
6+
7+
The while loop keeps executing as long as the condition is true.
8+
*/
9+
//Lets take the example of printing "Happy Birthday" 100 times:
10+
// #include <stdio.h>
11+
// int main(){
12+
// int i = 0; // we will use this variable for condition
13+
// while(i<100){ // until the variable i stores value less than 100, the code inside while loop will execute in series
14+
// printf("Happy Birthday\n");
15+
// i = i+1; // increases the value of i by 1 each time the loop executes
16+
// }
17+
// return 0;
18+
// }
19+
20+
/*
21+
Here we increase the value of i (0) by 1 each time the loop executes, so that at a point of time i can become 100. And when it becomes 100, the loop executes because the condition will become false
22+
23+
This loop is mainly useful when we don't know how many times we want to execute the code.
24+
*/
25+
26+
/*
27+
Note:
28+
If the condition becomes false, the while loop keeps getting executed. Such loop is called an infinite loop.
29+
*/
30+
31+
//example of an infinite loop is:
32+
33+
// #include <stdio.h>
34+
// int main(){
35+
// int i = 0;
36+
// while(i<10){
37+
// printf("Value of i: %d", i);
38+
// }
39+
// return 0;
40+
// }
41+
// Here if we don't increase the value of i by 1, the condition (i<10) will always be true because 0 is less than 10 and it will never become false. so the loop will continue to loop that can create a lag in your system.
42+
43+
// another example is:
44+
#include <stdio.h>
45+
int main(){
46+
while(2<10){
47+
printf("Happy Birthday\n");
48+
}
49+
return 0;
50+
}
51+
52+
// this works same as the above example. The condition will always be true because 2 is less than 100. so the loop will continue to execute again creating lag in your system.

Chapter_4/Quick_Quiz/Quick_Quiz.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// write a program to print natural numbers from 10-20 when initial loop counter is initialized to 0.
2+
// The loop counter needs not to be int, it can be a float also.

Chapter_4/Quick_Quiz/Solution.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// answer 1
2+
// #include <stdio.h>
3+
// int main(){
4+
// int i = 0;
5+
// while(i<=20){
6+
// if(i>=10){
7+
// printf("%d\n", i);
8+
// }
9+
// i = i+1;
10+
// }
11+
// return 0;
12+
// }
13+
14+
// answer 2
15+
#include <stdio.h>
16+
int main(){
17+
int i = 0;
18+
int nn = 10;
19+
while(i <= 10){
20+
printf("%d\n", nn);
21+
nn = nn+1;
22+
i = i+1;
23+
}
24+
return 0;
25+
}

0 commit comments

Comments
 (0)