Reputation: 165
#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 2){
i++;
switch (c) {
case 'a':
printf("%c ", c);
break;
break;
}
}
printf("after loop\n");
}
What will be the output of the above code? Does the second break mean anything?
Upvotes: 1
Views: 187
Reputation: 518
There is no use for a second break statement. The break statement has the following two usages:
When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement.
In this case it terminates the case statement. So the second break doesn't get called. It is a useless statement.
Upvotes: 4
Reputation: 320641
break
is a jump statement in C. It unconditionally transfers control to a different location in the code. Which means that any code between that break
and the target point of the jump is unreachable, unless there's a label that allows one to reach it.
In your case there's no such label. The second break
is unreachable and has no effect.
Upvotes: 5