Back to questions
JuniorTheoryCommon
What does the program print?
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2)
{
break;
}
Console.Write(i);
}
}123
- Expecting
1:breakends only the nearest loop, the inner one, while the outer loop goes on and prints2and3. - Expecting
112233:breakdoes not skip one iteration the waycontinuedoes; it ends the inner loop, soj = 3never comes.
- How would you change the program so that
breakstops both loops? - How many times does
Console.Write(i)run if you remove theiftogether with thebreak?