Computer Science, asked by ghezta6, 11 months ago

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

Answered by sswaraj04
0

at_answer_text_other

a=18

at_explanation_text_other

simply it break out of loop when a=18


ghezta6: so a = 18??
sswaraj04: yup
ghezta6: no
Answered by siddhartharao77
8

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!


ghezta6: but it says System. out.println is unreachable statement..
ghezta6: please help brother
siddhartharao77: There are no errors in your program
ghezta6: so why its unreachable??
ghezta6: ok...say what will be the output??
siddhartharao77: The output will be 18
ghezta6: thx brother
siddhartharao77: Welcome
Similar questions