Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Give the output of the following program segment and also mention the number of times the loop is executed:
int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b ==0)
break;
}
System.out.println(a);
Answer:
Loop initialization: a = 6, b = 4
First iteration:
Condition check: a <= 24 — 6 <= 24 —- true
Loop is executed for the first time
Loop execution: a % b = 6 % 4 = 2 != 0 Hence, break is not executed
Loop increment operator: a = 1 + 6 — a = 6 + 6 = 12
Second iteration:
Condition check: a <= 24 — 12 <= 24 —- true
Loop is executed for the second time
Loop execution: a % b = 12 % 4 = 0 = 0 Hence, break is executed
System.out.println(a); — 12
Output is 12 and loop is executed two times
C. Sir| Created: 15-Apr-2019 | Updated: 15-Apr-2019|ICSE2017