-
Notifications
You must be signed in to change notification settings - Fork 121
/
convert_for_to_while.c
65 lines (59 loc) · 1.69 KB
/
convert_for_to_while.c
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
/*******************************************************************************
*
* Program: Convert A For Loop To A While Loop Demonstration
*
* Description: Example of how to convert a for loop to a while loop in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=q7vdQJ3Y0CE
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
int main()
{
// for loop to output the numbers from 1 to 10
//
// Notice the different parts of the for-loop...
//
// Initialization statement: int i = 1;
// Condition (test expression): i <= 10
// Update statement: i++
// Body:
// {
// printf("%d\n", i);
// }
//
for (int i = 1; i <= 10; i++)
{
printf("%d\n", i);
}
// We can follow the below template to convert a for loop to a while loop,
// replacing the different parts of the template with the associated parts
// of the for loop.
//
// /* initialization statement */
// while ( /* condition (test expression) */ )
// {
// /* body */
//
// /* update statement */
// }
//
// This will work for any for loop, as the initialization statement will
// execute before anything else in the loop, the condition will be checked
// before each time the loop body executes, and the update statement will
// always execute after the body. We reproduce the exact same behaviour
// and control-flow of the for-loop by respecting this order/behaviour in
// the for loop!
//
// The while loop that results from applying the template:
//
int i = 1;
while (i <= 10)
{
printf("%d\n", i);
i++;
}
return 0;
}