Submitted by c-admin on Tue, 06/04/2019 - 22:06
Question:
Which of these for statements are valid?
1. for (int i=5; i=0; i--) { }
2. int j=5;
for(int i=0, j+=5; i<j ; i++) { j--; }
3. int i, j;
for (j=10; i<j; j--) { i += 2; }
4. int i=10;
for ( ; i>0 ; i--) { }
5. for (int i=0, j=10; i<j; i++, --j) {;}
Select 1 option
A. 1, 2
B. 3, 4
C. 1, 5
D. 4, 5
E. 5
Answer and Explanation (Click to expand) Explanation:
No 1. uses '=' instead of '==' for condition which is invalid. The loop condition must be of type boolean.
No 2. uses 'j +=5'. Now, this statement is preceded by 'int i=0,' and that means we are trying to declare variable j. But it is already declared before the for loop. If we remove the int in the initialization part and declare i before the loop then it will work. But if we remove the statement int j = 5; it will not work because compiler will try to do j = j+5 and you can't use the variable before it is initialized. Although the compiler gives a message 'Invalid declaration' for j += 5 but it really means the above mentioned thing.
No 3. i is uninitialized.
No 4. is valid. It contains empty initialization part.
No 5. This is perfectly valid. You can have any number of comma separated statements in initialization and incrementation part. The condition part must contain a single expression that returns a boolean.
All a for loop needs is two semi colons :- for( ; ; ) {} This is a valid for loop that never ends. A more concise form for the same is : for( ; ; );