Welcome

首页 / 软件开发 / C语言 / 学点C语言(11):goto语句

学点C语言(11):goto语句2010-04-30 博客园 万一例1:

#include <stdio.h>
#include <string.h>

int main(void)
{
char str[256];

scanf("%s", str);

if (strlen(str) < 10) {
goto Label1;
} else {
goto Label2;
}

Label1:
printf(" 输入内容没有超过10个字符");
goto Label3;
Label2:
printf(" 输入内容达到或超过了10个字符");
goto Label3;

Label3: getchar(); getchar(); return 0;
}

例2:

#include <stdio.h>

int main(void)
{
int i = 0;
while (1) {
i++;
printf("%d ", i);
if (i == 10) goto AAA;
}

AAA: printf("OK! ");
getchar();
return 0;
}

例3: goto 一个空语句

#include <stdio.h>

int main(void)
{
int i = 0;
for (;;) {
i++;
printf("%d ", i);
if (i == 10) goto AAA;
}

AAA:; /* 这是个空语句 */
getchar();
return 0;
}