标签:case tmp puts switch C语言 break while continue
一般在的,while, for 这样含有循环体的语句中,某些情况下我们会需要立即跳出当前循环。这时使用block语句就可以实现直接从当前循环体直接跳出开始执行while, for语句的下一条语句。
另外,break 这种可以从一段复合语句中跳出的功能也被switch 广泛借用,来跳过switch 复合语句,也就是被花括号框住的代码块中剩余的部分,转去执行switch 语句的下一条语句。因此在switch 中,break 常被用来控制控制流跳出,使得switch 在运用上更加灵活。
而Continue 略不同于break 。虽然同样在while, for 语句中体现出“跳过”的效果。但continue是跳过循环体的剩余部分,而后重新执行循环体的句头而非下一条语句。
另continue使用范围被限制在循环体中,不可用于switch。
我们可以通过一些简单的代码来验证他们的效果:
break 语句在while…do 句型中的效果:
#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
//break in while...do
//初始化 initialize
int tmp = 0;
puts ( "while criculation\n" );
//while循环
while ( tmp <= 5 ){//while循环5次
printf_s ( "%d\t", tmp );
printf_s ( "%d\n", tmp );
tmp++;
}//end while
//初始化 initialize
tmp = 0;
puts ( "\nwhile circulation, execute the break when tmp = 3\n" );
//while循环
while ( tmp <= 5 ){//while循环5次
printf_s ( "%d\t", tmp );
if ( 3 == tmp ){//当tmp为3时,执行break
puts ( "break...");
break;
}//end if
printf_s ( "%d\n", tmp );
tmp++;
}//end while
}
相似的,在do…while 语句中的效果:
#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
//break in do...while
//初始化 initialize
int tmp = 0;
puts ( "while criculation\n" );
//while循环
do{//while循环5次
printf_s ( "%d\t", tmp );
printf_s ( "%d\n", tmp );
tmp++;
}while ( tmp <= 5 );//end while
//初始化 initialize
tmp = 0;
puts ( "\nwhile circulation, execute the break when tmp = 3\n" );
//while循环
do{//while循环5次
printf_s ( "%d\t", tmp );
if ( 3 == tmp ){//当tmp为3时,执行break
puts ( "break...");
break;
}//end if
printf_s ( "%d\n", tmp );
tmp++;
}while ( tmp <= 5 );//end while
}
而break 在多重嵌套的while语句中
#include <stdio.h>
//break在while多重嵌套中的使用效果
int main () {
//initialize
int tmp = 0, loop = 0;
puts ( "multiple while nesting" );
//the first layer while
while ( loop <= 2 ){
loop++;
puts ( " in the first layer while");
//the second layer while
while ( tmp <= 2 ){
tmp++;
puts ( "\tin the second while");
if ( tmp >= 3){// execute break when tmp >= 3
puts ( "\tbreak");
break;
}//end if
puts ( "\tout of the second while");
}//end while
puts ( "out the frist layer while");
}//end while
}
break在for语句中体现:
#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
//break in for
//if there is no break
puts ( "if there is no break ");
for (int tmp = 1; tmp <= 5; tmp++){//5 loops
printf_s ( "\nthis is No.%d", tmp );
}//end for
//when we add break
puts ( "\n\nexecute the break when tmp = 2");
for (int tmp = 1; tmp <= 5; tmp++){//5 loops
printf_s ( "\nthis is No.%d", tmp );
if ( 2 == tmp ){//execute when tmp = 5
puts ( "\tbreak" );
break;
}//end if
}//end for
}
在switch 中的运用:
#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
//break in switch
//initialize
int tmp = 1;
//a switch without break
puts ( "a switch without break");
switch ( tmp ){
case 0:
puts ( "0");
case 1:
puts ( "1");
case 2:
puts ( "2");
case 3:
puts ( "3");
case 4:
puts ( "4");
case 5:
puts ( "5");
default:
puts ( "switch over\n");
}//end switch
//add break before case 4
puts ( "add break before case 4" );
switch ( tmp ){
case 0:
puts ( "0");
case 1:
puts ( "1");
case 2:
puts ( "2");
case 3:
puts ( "3");
break;
case 4:
puts ( "4");
case 5:
puts ( "5");
default:
puts ( "switch over");
}//end switch
}
continue 在while 循环中的效果体现:
#include <stdio.h>
//比较验证continue在while、for语句中的运用
int main () {
//initialize
int tmp = 0;
puts ( "while circulation" );
//while with out continue
while ( tmp <= 5 ){
tmp++;
printf_s ( "%d\t", tmp );
printf_s ( "%d\n", tmp );
}//end while
//initialize
tmp = 0;
puts ( "\nwhile loop with the continue statement");
//while loop with the continue statement
while ( tmp <= 5 )
{
tmp++;
printf_s ( "%d\t", tmp );
if ( tmp >= 3 ){//when tmp >= 3, excute continue
puts ( "" ); // 保证输出结果的规整
continue;
}//end if
printf_s ( "%d\n", tmp );
}//end while
}
continue在多重的while循环中表现出的作用范围同break一致,只对其所在的最近一级嵌套起作用。
#include <stdio.h>
//continue在while多重嵌套中的使用效果
int main () {
//initialize
int tmp = 0, loop = 0;
puts ( "multiple while nesting" );
//the first layer while
while ( loop <= 2 ){
loop++;
puts ( " in the first layer while");
//the second layer while
while ( tmp <= 2 ){
tmp++;
puts ( "\tin the second while");
if ( tmp >= 2){// execute continue when tmp >= 3
puts ( "\tcontinue");
continue;
}//end if
puts ( "\tout of the second while");
}//end while
puts ( "out the frist layer while");
}//end while
}
而尝试在switch 中使用continue 则会直接报错,指明continue 只能用于循环体
#include <stdio.h>
//continue 在while、for、switch语句中的运用
int main (){
//continue in switch
//initialize
int tmp = 1;
//a switch without continue
puts ( "a switch without continue");
switch ( tmp ){
case 0:
puts ( "0");
case 1:
puts ( "1");
case 2:
puts ( "2");
case 3:
puts ( "3");
case 4:
puts ( "4");
case 5:
puts ( "5");
default:
puts ( "switch over\n");
}//end switch
//add continue before case 4
puts ( "add continue before case 4" );
switch ( tmp ){
case 0:
puts ( "0");
case 1:
puts ( "1");
case 2:
puts ( "2");
case 3:
puts ( "3");
continue;
case 4:
puts ( "4");
case 5:
puts ( "5");
default:
puts ( "switch over");
}//end switch
}
标签:case,tmp,puts,switch,C语言,break,while,continue 来源: https://blog.csdn.net/qq_36790234/article/details/101214483
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。