In: Computer Science
The following is an infinite loop:
for (int i = 0; i < 10; i++);
System.out.println(i + 4);
TRUE OR FALSE?
Answer: FALSE
Explanation:
Program:
Explanation:
Line 1: //Here taking class name as MyClass
Line 2: //Start of main()
Line 3: //for loop iteration
Line 4: //Printing the value of i+4
Line 5:// End of for loop
Line 6://End of main()
Line 7://End of MyClass class
Program:
public class MyClass {
public static void main(String args[]) {
for (int i = 0; i < 10; i++){
System.out.println(i+4);
}
}
}
Output:
Explanation:
Step 1:initially value of i=0
Checking the condition i<10 => 0<10 True
Print i+4 => Print 0+4 => Print 4
i = i++ => i = 4+1 => i = 5
Step 2: value of i=5
Checking the condition i<10 => 5<10 True
Print 5
i = i++ => i = 5+1 => i = 6
Step 3: value of i=6
Checking the condition i<10 => 6<10 True
Print 6
i = i++ => i = 6+1 => i = 7
Step 4: value of i=7
Checking the condition i<10 => 7<10 True
Print 7
i = i++ => i = 7+1 => i = 8
Step 5:value of i=8
Checking the condition i<10 => 8<10 True
Print 8
i = i++ => i = 8+1 => i = 9
Step 6:value of i=9
Checking the condition i<10 => 9<10 True
Print 9
i = i++ => i = 9+1 => i = 10