Which of the following for loop declaration is not valid?
A) for ( int i = 7; i <= 77; i += 7 )
B) for ( int i = 99; i >= 0; i / 9 )
C) for ( int i = 2; i <= 20; i = 2* i )
D) for ( int i = 20; i >= 2; – -i )
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Answer: B) for ( int i = 99; i >= 0; i / 9)
Explanation: Above question is which is not a valid declaration for loop in given statements so the correct answer is option B.
Java for loop syntax is:
for(initialization expression; test expression; update expression)
{ // code block to be executed }
Initialization Expression: This expression, we have to initialize the loop counter of some value. Example: int i=1;
Test Expression: This expression, we have to test the condition. If the condition evaluates to true then, we will execute the body of the loop and go to update expression. Otherwise, we will exit from the for loop. Example: i<10;
Update Expression: This expression increments/decrements the loop variable by some value, After executing the loop body. Example: i++;
Given expression option B) for ( int i = 99; i >= 0; i / 9) here 3rd or update expression is i/9 not declared correctly. third expression should be increments/decrements variable. It would not execute, and an exception is thrown, as shown below.
Exception in thread “main” java.lang.Error: Unresolved compilation problem: Syntax error on token “/”, invalid AssignmentOperator
Hence the other 3 options are valid and execute. option B) is Not Valid.