How can I create loops in C.

 

C course - grinding - for when you often want to do almost the same thing

Imagine you have created an array, xvalues ​​[10000], and should now fill it with values. write now

xvalues ​​[0] = 0;
xvalues ​​[1] = 1;
xvalues ​​[2] = 2;
.
.

xvalues ​​[9999] = 9999;

is also possible, of course, but there should also be an easier way. There are even three different ones:

  • the for loop
  • the while loop
  • the do - while loop

 

1. for loop

the general form is:

for (initialization; condition; step size)
{

Instructions;

}

Now to the practice, with a for loop we need a run variable, e.g.

int i;

Looping variables often have names like i, j, k or something. On the one hand this is due to the fact that this is so nice and short and on the other hand it comes from good old FORTRAN. In FORTRAN, all variables that begin with the letters i to n are implicitly integer by default, unless the type is specified directly. I think I can remember that a Mars probe went perdu because the FORTRAN programmer started a variable that was actually a real number with a letter between i to n.

So and now the first loop, please write a small main program in your compiler and try the following:

for (i = 0; i <10; i ++)
printf ("i:% d \ n", i);

If there is only one statement that should be executed each time the loop is run, you can skip the curly braces. Short to printf Instruction (print formatted, we will get later): Here it is written on the console, first "i:" and then the number i even has and then the return character "\ n".

First is the loop variable initialized: i = 0;
Then the Running condition fixed: i <10; will this not correctbreaks off the loop.
Finally, the Increment specified: i ++

If you run the program you should get the following output:

i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9

You can now play with these parameters:

for (i = 10; i> 0; i--)
printf ("i:% d \ n", i);

results in the following output:

i: 10
i: 9
i: 8
i: 7
i: 6
i: 5
i: 4
i: 3
i: 2
i: 1

 

for (i = 0; i <1000; i + = 100)
printf ("i:% d \ n", i);

results in the following output:

i: 0
i: 100
i: 200
i: 300
i: 400
i: 500
i: 600
i: 700
i: 800
i: 900

 

for (i = 1000; i> -1; i = i - 100)
printf ("i:% d \ n", i);

results in the following output:

i: 1000
i: 900
i: 800
i: 700
i: 600
i: 500
i: 400
i: 300
i: 200
i: 100
i: 0


You can also initialize several variables in the loop head, query their running conditions and define their increments:

for (i = 0, j = 100; i <10, j <150; i ++, j + = 10)
printf ("i:% d j:% d \ n", i, j);

Here i and j are initialized: i = 0, j = 100;
Your running condition is queried: i <10, j <150; both conditions must be true.
And their step size is determined: i ++, j + = 10
This results in the following output:

i: 0 j: 100
i: 1 j: 110
i: 2 j: 120
i: 3 j: 130
i: 4 j: 140

The loop breaks off because the run condition j <150; becomes wrong. You have to decide for yourself whether such constructions increase the readability and clarity of the program. Personally, I only use such loops with several run variables very, very rarely.

So now let's use a loop to initialize arrays:

int i, count, xvalues ​​[10], yvalues ​​[10], zvalues ​​[10];
float veclength [10];

count = 0;
for (i = -10; i <0; i ++)
{
xvalues ​​[i + 10] = i;
yvalues ​​[i + 10] = count * 10;
zvalues ​​[i + 10] = 1000 + i;
veclength [i + 10] = 0.0;
count ++;
}

for (i = 0; i <10; i ++)
printf ("x:% d y:% d z:% d vec:% f \ n", xvalues ​​[i], yvalues ​​[i], zvalues ​​[i], veclength [i]);

results in the following output, please think about it for a moment and try to understand the logic:

x: -10 y: 0 z: 990 vec: 0.000000
x: -9 y: 10 z: 991 vec: 0.000000
x: -8 y: 20 z: 992 vec: 0.000000
x: -7 y: 30 z: 993 vec: 0.000000
x: -6 y: 40 z: 994 vec: 0.000000
x: -5 y: 50 z: 995 vec: 0.000000
x: -4 y: 60 z: 996 vec: 0.000000
x: -3 y: 70 z: 997 vec: 0.000000
x: -2 y: 80 z: 998 vec: 0.000000
x: -1 y: 90 z: 999 vec: 0.000000

And now one multidimensional Array in a triple nested for loop:

int i, j, k;
char errstrings [20] [5] [256];

for (i = 0; i <20; i ++)
for (j = 0; j <5; j ++)
for (k = 0; k <256; k ++)
errstrings [i] [j] [k] = 0;

Here the multi-dimensional array of char is initialized with zeros. The output is not that exciting, everything is zero.

It is allowed to change the loop variable in the loop body, I recommend this on no case at all to do. You can actually wait for it to go wrong, the construction becomes obscure and you always have the option to somehow solve the problem differently:

int i, x [10];

for (i = 0; i <100; i ++)
{
x [i% 10] = i; // i% 10, remember the modulus operator?
i + = 10; // O God, the loop run variable in the loop body is changed here, please don't do that!
}
for (i = 0; i <10; i ++)
printf ("i:% d \ n", x [i]);

results in the following output:

0
11
22
33
44
55
66
77
88
99

 

And of course you can also nest loops, i.e. insert loops within loops within loops ..., here is an example:

int i, j, k, k = 0;

for (i = 0; i <5; i ++)
for (j = 0; j <4; j ++)
for (k = 0; k <3; k ++)
x ++;