nan*_*zen 2 c break while-loop
我有一个简单的server-client终端.服务器从客户端接收字符串并对其进行处理.服务器只有在收到end_of_input我的情况下的字符后才会开始处理'&'.下面的while循环旨在允许用户输入许多不同的字符串,并且应该在收到时停止执行'&'.
while(1) {
printf("Enter string to process: ");
scanf("%s", client_string);
string_size=strlen(client_string);
//I want to escape here if client_string ends with '&'
write(csd, client_string, string_size);
}
Run Code Online (Sandbox Code Playgroud)
如何在用户输入end_of_input字符后退出while循环'&'?
while(1) {
printf("Enter string to process: ");
scanf("%s", client_string);
string_size=strlen(client_string);
write(csd, client_string, string_size);
if (client_string[string_size -1 ] == '&') {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
break关键字可用于立即停止和转义循环.它在大多数编程语言中使用.还有一个有用的关键字可以轻微影响循环处理:continue.它会立即跳转到下一次迭代.
示例:
int i = 0;
while (1) {
if (i == 4) {
break;
}
printf("%d\n", i++);
}
Run Code Online (Sandbox Code Playgroud)
将打印:
0
1
2
3
Run Code Online (Sandbox Code Playgroud)
继续:
int i = 0;
while (1) {
if (i == 4) {
continue;
}
if (i == 6) {
break;
}
printf("%d\n", i++);
}
Run Code Online (Sandbox Code Playgroud)
将打印:
0
1
2
3
5
Run Code Online (Sandbox Code Playgroud)