-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy path07-loops.c
More file actions
75 lines (62 loc) · 2.01 KB
/
07-loops.c
File metadata and controls
75 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
In programming, a loop is used to repeat a
block of code until the specified condition is met.
C programming has three types of loops:
for loop
while loop
do...while loop
*/
/*
for Loop
The syntax of the for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
How for loop works?
- The initialization statement is executed only once.
- Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is terminated.
- However, if the test expression is evaluated to true,
statements inside the body of the for loop are executed, and
the update expression is updated.
- Again the test expression is evaluated.
This process goes on until the test expression is false.
When the test expression is false, the loop terminates.
Practice Program
Print Numbers 1 - 10
Calculate the sum
*/
/*
C while loop
The syntax of the while loop is:
while (testExpression) {
// the body of the loop
}
How while loop works?
The while loop evaluates the testExpression inside the parentheses ().
If testExpression is true, statements inside the body of while
loop are executed. Then, testExpression is evaluated again.
The process goes on until testExpression is evaluated to false.
If testExpression is false, the loop terminates (ends).
Practice Program
Print Numbers 1 - 10
*/
/*
do...while loop
The do..while loop is similar to the while loop with one important
difference. The body of do...while loop is executed at least once.
Only then, the test expression is evaluated.
The syntax of the do...while loop is:
do {
// the body of the loop
}
while (testExpression);
How do...while loop works?
- The body of do...while loop is executed once. Only then, the testExpression is evaluated.
- If testExpression is true, the body of the loop is executed again and testExpression is evaluated once more.
- This process goes on until testExpression becomes false.
- If testExpression is false, the loop ends.
Practice Program
Ask for correct input continously
*/