循环语句(第二部分)

循环语句(第二部分)

一、for循环

(一)for循环 语法:

for(表达式1;表达式2;表达式)
    循环语句;

表达式****1

表达式1为初始化部分,用于初始化循环变量的。

表达式****2

表达式2为条件判断部分,用于判断循环时候终止。

表达式****3

表达式3为调整部分,用于循环条件的调整。

举例子:在屏幕上打印1~10的数字。

#include <stdio.h>
int main()
{
    int a = 1;
    for(a=1;a<=10;a++)
    {
        printf("%d",a);
    }
    return 0;
}

image-20220418164552310

(二) break和continue在for循环中的作用

【1】break在for循环中的作用

#include <stdio.h>
int main()
{
    int a = 1;
    for(a=1;a<=10;a++)
    {
        if(a==5)
            break;
        printf("%d",a);
    }
    return 0;
}

这里的循环,当a在1~4范围内时,正常打印,当a为5时会直接跳出循环,因此打印结果为1234

image-20220418165015378

【2】continue在for循环中的作用

#include <stdio.h>
int main()
{
    int a = 1;
    for(a=1;a<=10;a++)
    {
        if(a==5)
            continue;
        printf("%d",a);
    }
    return 0;
}

这里与while循环有不同之处,这里当a=5时,不会执行打印而是继续循环,因此结果应该是1234678910

image-20220418165354343

(三)一些关于for循环的建议

建议:

\1. 不可在for 循环体内修改循环变量,防止 for 循环失去控制。

\2. 建议for语句的循环控制变量的取值采用“前闭后开区间”写法。

(四)for循环的变种

【1】for循环中判断条件不要省略,容易造成无线循环

如:

#include<stdio.h>
int main()
{
	for (;;)
	{
		printf("NB");
	}
	return 0;
}

image-20220418165829036

【2】

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	for (a = 0; a < 3; a++)
	{
		for (b = 0; b < 3; b++) 
			printf("ABC ");
	}
	return 0;
}

第八行的初始化如果删除,其出现的结果也有所不同:

image-20220418170322097

image-20220418170354585

出现这两种情况的原因是当a=0,的三次循环结束后,a++得到结果为1,欲再次循环时,b没有初始化,依然等于3,会直接跳出循环。

【3】可以多个变量控制for循环。

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    for(a=0,b=0;a<2&&b<3;a++,b++)
        printf("abc ")
    return 0 ;
}

如上段代码所示,这里的for循环由a,b两者共同控制,一共打印2次。

image-20220418171149462

二、do while循环

(一)语法

do
    循环语句;
while(表达式)

举例:打印1~10:

#include <stdio.h>
int main()
{
    int a = 1;
    do{
        printf("%d",a);
        a++;
    }  
    while(a<=10);
    return 0;
}

image-20220418180532854

(二)do while循环中的break和continue

【1】break:

#include <stdio.h>
int main()
{
    int a = 1;
    do
    {

        if (a == 5)
            break;
        printf("%d", a);
        a++;
    }
    while (a <= 10);
    return 0;
}  //当a增加到5时,直接跳出循环

image-20220418180909763

【2】continue

#include <stdio.h>
int main()
{
    int a = 1;
    do
    {

        if (a == 5)
            continue;
        printf("%d", a);
        a++;
    } while (a <= 10);
    return 0;
}     // 当a为5时,遇见continue,会直接开始新循环,但是由于a数字未得到增加,因此结果是1234死循环。

image-20220418181246965

三、goto语句

从理论上 goto语句是没有必要的,实践中没有goto语句也可以很容易的写出代码。但是某些场合下goto语句还是用得着的。

举例:

for()
{
    for()
    {
        for()
            if(condition1)
                goto(condition2);
    }
}
condition2:
    printf("ABC");

要想从最内侧的条件跳出来,如果不用goto,需要用三个‘break’,一步一步跳出循环。而利用‘go to‘,可以直接跳出循环,直接跳转到制定目标。


以上就是本次循环语句的部分学习内容,有不正确之处,请各位大佬指正。

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值