b. public class Dk3
public static void main(String args[])
inta;
for (a = 10; a < 50; a++)
if (a == 18)
break;
System.out.println(a);
find value of a
Answers
at_answer_text_other
a=18
at_explanation_text_other
simply it break out of loop when a=18
Sample Program:
(i)
public class Dk3
public static void main(String args[])
int a;
for(a = 10; a < 50; a++)
if(a == 18)
break;
System.out.println(a);
Output:
18
Explanation:
a == 18
System.out.println(a); ---- 18
----------------------------------------------------------------------
(ii)
public class Dk3
{
public static void main(String args[])
{
int a;
for(a = 10; a < 50; a++)
{
if(a == 18)
{
break;
}
System.out.println(a);
}
}
}
Output:
10
11
12
13
14
15
16
17
Explanation:
When a break statement is encountered inside a loop, the control comes directly out of loop and loops gets terminated for rest of iterations.
In the above program(when a == 18), We break out of the 18th iteration and break out of the loop, so the code after the break statement during the 18th iteration and code after that the 18th iteration isn't executed.
Hope it helps!