最近学习obs源码,插件库里是有许多goto语句,由于对goto语句不太了解,踩了很多坑。记录一下:
1 含义,goto语句也称为无条件转移语句;goto语句的语义是改变程序流向, 转去执行语句标号所标识的语句;goto语句通常与条件语句配合使用。可用来实现条件转移, 构成循环,跳出循环体等功能。
2 例子
1)跳出循环
void
main()
{
int
i, j;
for
( i = 0; i < 10; i++ )
{
printf
(
"Outer loop executing. i = %d\n"
, i );
for
( j = 0; j < 3; j++ )
{
printf
(
" Inner loop executing. j = %d\n"
, j );
if
( i == 5 )
goto
stop;
}
}
/* This message does not print: */
printf
(
"Loop exited. i = %d\n"
, i );
stop:
printf
(
"Jumped to stop. i = %d\n"
, i );
}
2)switch判断
void
main()
{
int i;
switch(i)
{
case 0:
break;
case 1:
goto stop;
default:
break;
}
stop:
printf
(
"Jumped to stop. i = %d\n"
, i );
}