August said:
danu said:
Hello all,
How to write a for loop ( or any other loop) to get this kind of a
desired result?
/* al and a2 are int arrays */
a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];
thanks. appreciate you'r help.
If you're stuck at this level, try to switch to a safer (simpler)
language, Java perhaps. C is definitely not a beginner's language.
If you're stuck at this level Java is not going to help.
For the OP; the for loop is a way of running a set of
statements multiple times. There are many possible
uses but the simplest is to keep a counter variable
which gets incremented every time the loop is run.
C99 style:
for (int ii=0; ii<some_value;ii++) {
do_something_without(); /* ii */
do_something_with(ii);
}
The first time the loop is run, we have ii=0,
and check ii<some_value, if true we run the loop.
The next time it is run we increment ii, check
ii<some_value and run the loop again. When we
get round to ii==some_value the check will fail
and the loop ends, the loop statements are not
run with ii=some_value.
In C89/ANSI C you must have declared all your
variables at the start of the function, so the
start of the loop looks like:
for (ii=0; ii<some_value; ii++) {
Once the loop has ended in this case ii is left
equal to some_value (but, again, the loop
contents are not run on ii=some_value).