So today I went to an interview and one of the questions was the following (C# context).
//Print the output for the following code:
for (int i = 10, j = 0; j <= 10; j++, i--)
{
if (i > j)
Console.WriteLine(j.ToString());
}
I have never seen such a construct before and having asked my colleagues, 4 of 5 at my workplace didn't know either (Perhaps more a reflection on us but I digress). Using some basic logic, I was able to answer the question correctly but this knowledge has radically altered my understanding of how for loops can be structured.
So I guess my question boils down to this.
for (statement1; statement2; statement3)
{
/* body */
}
(1) First the statement1
is executed.
(2) Next statement2
is executed.
(3) If the evaluation of statement2
is true then the body is executed
(4) Then statement3
is executed.
(5) Repeat from step (2)
| +<-----------------+
| | ^
V V |
for ( (s1); -------->(s2 true? | false?); (s3) )
{ | | ^
| | |
| | |
V | |
(body)-----|--------->+
} |
|
V
(come out)
The structure you have shown is the same normal structure as above. The statement n
could be any statement. In your example, you have separated by comma operators in statement1
and statement3
. You can separate any number of statements by comma operators.
Generally for
loops are used with the statement1
with initialization as it is executed only once. The statement2
is used for the loop termination condition checking, because the evaluation value of this statement is used to decide if to enter the body of break out. And the statement3
is used for update of the loop termination variable as it is executed after the body. But generally they could be used in any way.
First statement1
is i=10, j=0;
this initializes the variables. Next in the statement2
is j <= 10
if this is true then the body is executed. After the body is executed, statement3
which is i--,j++
is executed. The loop will iterate 11
times 0
to 10
. But will print 5
times, as at one point i
and j
will become same and the if (i > j)
will evaluate false.
EDIT Here is an example where it might be used, not much practical but a sample use, to check for a palindrome string.
int i, j, n, flag;
char str[128];
printf ("\nEnter string: ");
scanf ("%s", &str);
n = strlen (str);
for (flag=1, i=n-1, j=0; j<n/2; j++, i--)
{
if (str[i] != str[j])
{
flag = 0;
break;
}
}
if (flag)
printf ("\n\"%s\" is a palindrome");
else
printf ("\n\"%s\" is not a palindrome");
We should always try to write code which is easy to read and which does not create confusion. This helps the code writer as well as others who read the code.
In C and Java for
loops you may specify an unlimited (or zero) number of comma separated initialisers (of the same data type) and end-of-loop actions.
The initializer section is really just one java statement; java variable definitions may be grouped if they are of the same type, ie:
int i = 0;
int j = 0;
is equivalent to:
int i = 0, j = 0;
You can do what you like in the end-of-loop section - any number of statements separated by commas.
Since java 5, there is also the foreach
syntax, eg:
List<String> list;
for (String element : list) {
// do something with the variable element
}
The syntax works with Iterable
types, which includes arrays and Collections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With