For loop with Continue Program Output
What will be the output of the following program?
class ContinueExample
{
public static void main(String s[])
{
int sum = 0;
for(int i = 2; i <= 10; i++ )
{
if( i % 3 == 0 ) continue;
if( i == 8 ) break;
sum += i;
}
System.out.println("sum = " + sum);
}
}
A.
|
sum = 18
|
B.
|
sum = 36
|
C.
|
Compilation error since continue and break can not be used in the same for loop.
|
D.
|
sum = 27
|
Topic:
Java continue Statement
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
ans is A ,,, 2+4+5+7=18
Posted by Raju VarshnEy 2014-09-03 12:41:41
(y)... C
Posted by Akshay Bansod 2014-09-03 12:42:48
Ans is B sum is 36 as continue skips the statement and reexecutes loop with new condition
Posted by Naga Sinst 2014-09-03 14:23:34
ans a. for 3and 6 it skips,
for 8 it goes outofloop
so remaining 2,4,5,7 added sum 18
Posted by Maheshwari Natarajan 2014-09-03 15:22:00
a.
Posted by Shubham Bansal 2014-09-03 15:25:10
ans a. loop skips at i,=3,6. remaining i=2,4 ,5, 7 ..n brks at 8 ..are added n rslt is 18
Posted by Asma Mujtaba Khan 2014-09-03 23:54:56
Congratulations Raju VarshnEy. You are this dose winner. We will send you the link using which you can claim your recharge.
Posted by Merit Campus 2014-09-04 04:11:52
thnx Merit Campus ,,, bt i didnt get it yet,,,
Posted by Raju VarshnEy 2014-09-04 12:51:00