What is the output of the code below?

for (int j = 1; j <= 10; j++)

{

if (j == 3)

{

break;

}

cout << j << endl;

}

Output:

for (int j = 1; j <= 10; j++)

{

if (j == 3)

{

continue;

}

cout << j << endl;

}

Output:

Expert

Respuesta :

Answer:

The first output for the following code is  given below :

1

2

The second output for the following code is given below

1

2

4

5

6

7

8

9

10

Explanation:

In the first code the loop will  iterated

When j=1 the condition of if block will not be executed  so it print 1 on console and increment the value of j.

After increment of j it becomes 2 again if block does not executed  because the condition of  if block is not  true so it print 2 on console and increased the value of j.

When j=3 if condition block is executed and   it terminate the loop immediately .

Therefore output is :

1

2

In the second code the loop will iterate

When j=1 the condition of if block is not executed  so it print 1 on console and increment the value of j.

After increment the value of j it becomes  2 .Again if block will not be executed  so it print 2 on console and increased the value of j.

After increment the value of j becomes 3.This time if block will  executed  ,it will moved directly control to the beginning of the loop and  skip the current  execution of statements that means it skip the value j=3 and increment the value of j .

Now j=4 this process is same follow untill  when j<=10 when j=11 the loop is false and loop will terminate .

Therefore output is :

1

2

4

5

6

7

8

9

10