Which of the following for loop declaration is not valid?for ( int i = 20; i >= 2; - -i )for ( int i = 7; i <= 77; i += 7 )for ( int i = 99; i >= 0; i / 9 )for ( int i = 2; i <= 20; i = 2* i )
Question
Which of the following for loop declaration is not valid?for ( int i = 20; i >= 2; - -i )for ( int i = 7; i <= 77; i += 7 )for ( int i = 99; i >= 0; i / 9 )for ( int i = 2; i <= 20; i = 2* i )
Solution
The third for loop declaration is not valid.
Here's why:
for ( int i = 99; i >= 0; i / 9 )
In this loop, the increment/decrement operation is i / 9. This operation will divide i by 9, but it does not assign the result back to i. Therefore, i will remain 99, creating an infinite loop.
To make it valid, you should use the /= operator to divide and assign the result to i, like so:
for ( int i = 99; i >= 0; i /= 9 )
Similar Questions
Which of the following for loops will be an infinite loop? for(; ;)for(i=0 ; i<1; i--) for(i=0; ; i++) All of the above
Which of the following is a valid for loop in Python?for(i=0; i < n; i++)for i in range(0,5):for i in range(0,5)for i in range(5)
Which of the following will compile successfully?*for(int j = 0,int k=5; j < k; k--) ;for(;;System.out.print(“a”)) ;for();for(int k = 10; k--; k>0 ) ;None of above.
Problem statementSend feedbackWhich of the following 'for' loops would not produce any error?A) for(var i=0; i<5; ++i) { console.log("Hello") ; }B) var i , j ; for(i=0, j = 10; i < 10, j < 100) { console.log("Hello"); }C) var i ; for(i=0; i<5) { console.log("Hello"); }D) var i , j ; for(i=0, j=10; i<5; ++i) { console.log("Hello"); }Options: One or more answers may be correctABCD
Which of the following is the first step in FOR loop?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.