Skip to content

Commit 184d360

Browse files
committed
Add Day 5: Loops
1 parent bf870d7 commit 184d360

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

Day 5/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Getting started with Loops
2+
3+
1. [Why do we need loops?](loopMotivation.c)
4+
2. [For loop](forLoop.c)
5+
3. [Exercise based on for loop](exerciseForLoop.c)

Day 5/exerciseForLoop.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* A simple execise based on for loop
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
int n;
10+
for (n = 9; n != 0; n --) {
11+
printf("%d ", n--);
12+
}
13+
14+
/* When the test condition evaluates to false
15+
* the loop stops
16+
*/
17+
18+
/* Dry run: helps get a better idea
19+
/* For loop:
20+
1. Perform the initialization: n = 9
21+
2. Peform the check, n != 0 TRUE
22+
3. First execute the loop body
23+
Print n-- (Postfix unary decrement)
24+
[postfix: use and update]
25+
[prefix: update and use]
26+
First the value of n is used, then updated
27+
9 is printed
28+
n = 8
29+
4. The loop update statement is executed
30+
n = 7
31+
5. 7 is printed
32+
n = 6 (from the loop body)
33+
n = 5 (from the loop update statement)
34+
6. 5 is printed
35+
7. 3 is printed
36+
8. 1 is printed
37+
n = 0 (from the loop body)
38+
n = -1 (from the loop update)
39+
9. n != 0 condition is checked, it evalues to true
40+
n is negative
41+
Its always going to decrement
42+
Loop never stops.....
43+
INFINITE LOOP!!!!!!!
44+
*/
45+
return 0;
46+
}

Day 5/forLoop.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Program to sum numbers from 1 to n.
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
int n;
10+
printf("Please enter the value of n: ");
11+
scanf("%d", &n);
12+
13+
int sumNumbersFrom1toN = 0;
14+
15+
// pseudo code:
16+
// result = 0
17+
// for number = 1 to n
18+
// result = result + number
19+
// result now gives you the sum of numbers from 1 to n
20+
21+
/*
22+
* Syntax to use a for loop:
23+
* for (initialization ; test expression; update expression) {
24+
* // block of statements
25+
* }
26+
*/
27+
28+
for (int number = 1; number <= n; number += 1) {
29+
// sumNumbersFrom1toN = sumNumbersFrom1toN + number
30+
sumNumbersFrom1toN += number;
31+
}
32+
33+
/*
34+
* Dry run:
35+
* Let's say user enters n = 3
36+
* sumNumbersFrom1toN = 0
37+
* For LOOP:
38+
* number <- 1 // number is initialized to 1
39+
* number <= 3 // condition is true
40+
* // first the loop body is executed, then the update statement is executed
41+
* sumNumberFrom1toN <- 0 + 1 = 1
42+
* // loop body has finished
43+
* number = number + 1
44+
* number = 2
45+
* // next iteration of loop starts
46+
* number <= 3, yes it is true
47+
* sumNumbersFrom1toN <- 1 + 2 = 3
48+
* number = number + 1 = 3
49+
* // next iteration of the loop
50+
* number <= 3, true
51+
* sumNumberFrom1toN <- 3 + 3 = 6
52+
* number = number + 1 = 4
53+
* // next iteration of the loop
54+
* number <= 3, NOW THE CONDITION IS FALSE
55+
* you go out of the loop
56+
*/
57+
58+
printf("The sum of numbers from 1 to %d: %d\n", n, sumNumbersFrom1toN);
59+
60+
return 0;
61+
}

Day 5/loopMotivation.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Program to print numbers from 1 to 10 to the screen
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
/*
10+
printf("1\n");
11+
printf("2\n");
12+
printf("3\n");
13+
printf("4\n");
14+
printf("5\n");
15+
printf("6\n");
16+
printf("7\n");
17+
printf("8\n");
18+
printf("9\n");
19+
printf("10\n");
20+
*/
21+
22+
// variable to store the number printed to the screen
23+
int number = 1;
24+
25+
// loop entry bounded by a condition
26+
// entry controlled loop, the condition is checked
27+
// before running the loop for the first time
28+
while (number <= 10) {
29+
// once you enter the loop
30+
// execute the loop body
31+
32+
printf("%d\n", number);
33+
34+
// update the value of number
35+
number += 1;
36+
}
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)