我有一个简单的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);
}
我怎样才能使 while 循环在用户输入后退出end_of_input
特点'&'
?
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;
}
}
break
关键字可用于立即停止和跳出循环。它用于大多数编程语言。
还有一个有用的关键字可以稍微影响循环处理:continue
。它立即跳转到下一个迭代。
Examples:
int i = 0;
while (1) {
if (i == 4) {
break;
}
printf("%d\n", i++);
}
将打印:
0
1
2
3
继续:
int i = 0;
while (1) {
if (i == 4) {
continue;
}
if (i == 6) {
break;
}
printf("%d\n", i++);
}
将打印:
0
1
2
3
5
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)