英文:
C while statement in switch clause
问题
The result of the code is:
4
34
In the switch
block, the while
loop with the condition a < b
is executed when a
is equal to 4. Inside the loop, you have case 4
followed by {a = a+5;}
.
Here's how it works:
- When
a
is equal to 4, it enters thewhile
loop. - The
case 4
is just a label, and it doesn't have its own condition. It's essentially acting as a marker. - The code within
{}
is executed, which incrementsa
by 5 (a = a + 5;
). - After that, the
break
statement is encountered, which exits theswitch
block.
So, the case 4
itself doesn't have a condition. Instead, the condition that controls whether this block of code executes is the while (a < b)
statement. The loop continues executing as long as a
is less than b
, and in each iteration, it adds 5 to a
. This continues until a
reaches 34, at which point the loop terminates, and the program proceeds to the next printf
statement, resulting in the output you provided.
英文:
This question comes from my C language exam. It asks for the result.
#include <stdio.h>
int main()
{
int a = 4, b = 30;
printf("%d\n", a);
switch(a){
case 1: a = a*10; break;
case 2: a = a*10; break;
case 3: a = a*10;
while (a<b) case 4: {a = a+5;} break; //???
case 5: a = a*10; break;
default:
b = a*10;break;
}
printf("%d\n", a);
return 0;
}
The result is:
>4 <br/>
>34
I know the basic idea of while statement and switch statement, but I don't understand what's going on in the fourth line of the switch block.
My guess is that the while condition is tested 7 times until variable a reaches 34 (4<30, 4+5+5+5+5+5+5 = 34). But how about the 'case 4' condition? Shouldn't this condition be tested before adding 5 to variable a? And how about the 'break' after it? I'm totally confused.
Thanks!
答案1
得分: 3
case 4
只是一个标签。
这相当于以下内容:
while (a < b)
some_label:
{
a = a + 5;
}
break; //这将跳出包围它的switch语句。
英文:
while (a<b) case 4: {a = a+5;} break; //???
case 4
is just a label.
This is equivalent to the below:
while (a<b)
some_label:
{
a = a+5;
}
break; //This will break out of the surrounding switch.
答案2
得分: 2
以下是翻译好的部分:
while (a < b) case 4: { a = a + 5; } break;
这行代码位于一个 switch
语句内部。switch
首先执行的是跳转到 case
标签 4
。
但是这个 case
标签位于一个 while
循环内部。在 a = a + 5
指令结束后,程序会“回到”while
指令。
程序所做的是在 while
循环内部执行 goto
。
通常不建议使用 goto
语句,除非在像下面这样完全可读的情况下使用 switch
块内:
switch(a)
{
case 1: a = a * 10; break;
case 2: a = a * 10; break;
case 5: a = a * 10; break;
default:
b = a * 10; break;
}
如果你试图巧妙地省略 break
,在 break
和下一个 case
之间插入指令... 你必须明白自己在做什么。其他人阅读这段代码可能会难以理解。
这也意味着不要跳转到循环结构的中间或其他奇怪的操作。
如果在实际工作中遇到这样的代码,请重新编写它。等效的代码将使用 do
... while
指令(第一次测试被跳过,即使 a < b
,我们也可以进入循环):
case 4: do { a = a + 5; } while (a < b); break;
这段代码可以得到相同的结果,但可以编译而且每个人都能理解它。
英文:
while (a<b) case 4: {a = a+5;} break;
This line of code is within a switch
statement. The first thing that the switch
does is to jump to the case
label 4
.
But this case label is within a while loop. At the end of the a=a+5
instruction, the program goes "back" to the while instruction.
What the program does is performing a goto
inside the while
loop.
goto
statements are discouraged, unless when within switch
blocks with perfectly legible constructs like:
switch(a)
{
case 1: a = a*10; break;
case 2: a = a*10; break;
case 5: a = a*10; break;
default:
b = a*10;break;
}
If you're trying to be smart, omitting the break
, inserting instructions between the break
and the next case
... you have to be aware of what you're doing. And someone else reading the code may have a hard time to interpret it.
That also implies not jumping in the middle of a loop construct or strange things like that.
If you encounter such code in real life, rewrite it. The equivalent would use a do
... while
instruction (the first test is skipped, we can enter the loop even if a<b
:
case 4: do {a = a+5;} while (a<b); break;
You get the same result with this code, except that it compiles without warnings and everyone understands what it does.
答案3
得分: 0
正如你所说,当 (a < b) 时,每次都会重复执行 case 4 并将 5 添加到 a,直到 a 大于 30 为止,然后它将跳出 while 循环并切换作用域。然后它将打印变量,该变量现在等于 34。
英文:
As you said, while (a < b) each time it will repeat case 4 and add 5 to a, by the time a is more than 30, it will break out of the while loop and switch scope. Then it will print the variable which is now equal to 34.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论