KnowledgeBoat Logo

Computer Applications

Find the output of the given code.

int a, b = 100; 
for (a = 10; a <= 12; a++)
{
    b += a;
}
System.out.print("a:" + a + " " + "b:" + b);

Java

Java Iterative Stmts

7 Likes

Answer

a:13 b:133

Working

The values of a and b are evaluated as follows in the for loop:

IterationabRemarks
  100Initial value
110110b = b + a = 100 + 10 = 110
211121b = b + a = 110 + 11 = 121
312110b = b + a = 121 + 12 = 133, Loop terminates

The print statements print the final values of a and b.

Answered By

4 Likes


Related Questions