Loop

In programming, iterative process is quite popular. Here, we study how to use for-loop and while-loop for iterative process.

for-loop

Fundamental structure of for-loop is shown in below.

```for (variable initialization; condition; variable update) {

statement to be executed when condition is true
(multi-line statements are possible to list)

}
```

For example, when we want to print 1 to 5, we can program as follows.

```for (i=1; i<=5; i++) {

printf("i = %d\n", i);

}
```

The expression "i++" in the above represents "i+=1" and "i=i+1". Execution result is shown in below.

```(Execution result)
\$ ./a.out
i = 1
i = 2
i = 3
i = 4
i = 5
```

Process in for-loop is done stepwise as follows.

1. Variable is initialized. (In the above example, "i" is set as 1)
2. Conditon is evaluated (i <= 5) If it is true, statement is executed (printf-function). If false, iterative process is finished.
3. After a statement is executed when the condition is true, variable is updated (i++)
4. As long as condition is true, step 2 and 3 are repeated.

In below, examples of for-loop are shown.

Calculation of sum of 1 to 100

```int i, sum=0;

for (i = 1; i <= 100; i++) {
sum += i;
}
```

Show a same message for 10 times

```int i;

for (i = 0; i < 10; i++) {
printf("I'm a student.\n");
}
```

while-loop

As well as for-loop, while-loop can be used for iterative process.

```while (condition) {

statement to be executed when condition is true
(multi-line statements are possible to list)

}
```

Compared to for-loop, while-loop has no initialization or variable update. Therefore, they should be intentionally clarified. In below, an example to print 1 to 5 is shown. The result is same as the result in case of using for-loop.

****** while-loop to print 1 to 5 *****
```i=1;
while (i<=5) {

printf("i = %d\n", i);
i++;
}
```

Do you understand the difference between for-loop and while-loop? Variable is initialized before while-loop and variable is updated after the statement in while-loop is done. Here again, if-loop is shown as follow. Please exchange both expressions of for-loop and while-loop without problem.

****** if-loop to print 1 to 5 *****
```for (i=1; i<=5; i++) {

printf("i = %d\n", i);

}
```

Exit by break

Now, you understand you can iterate processing by for-loop or while-loop. Sometimes, we want to stop iteration when a certain criteria is satisfied. In such case, we can use break. Assume that we want to know when it is greater than 1000 if we continute to sum integers from 1. An example is shown as follows, and "break" is used in the example.

********* if-loop **********
```int i, sum=0;

for (i = 1; ; i++) {
sum += i;
printf("i = %d, sum = %d\n", i, sum);

if (sum > 1000) {
break;
}

}
```
********* while-loop **********
```int i, sum=0;

i = 1;
while (1) {
sum += i;
printf("i = %d, sum = %d\n", i, sum);

if (sum > 1000) {
break;
}

i++;
}
```
```% ./a.out
(skip)
i = 38, sum = 741
i = 39, sum = 780
i = 40, sum = 820
i = 41, sum = 861
i = 42, sum = 903
i = 43, sum = 946
i = 44, sum = 990
i = 45, sum = 1035
```

Lessons for C programming
Junichi Susaki, Kyoto University