C语言循环之for

很多人在写代码的时候,都希望自己能够写出强健的代码,今天,本文主要介绍循环,而循环共有三种,分别为:for,while,do while,各有千秋,选择循环,目的就是让代码更加的简洁,程序更加的智能。

(一)for

例如:用代码显示3个Hallo World!

没有使用for,代码是这样子写的。

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	printf("Hallo World!!\n");
	printf("Hallo World!!\n");
	printf("Hallo World!!\n");
	
	return 0;
}

运行效果:

使用for,代码如下:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	const int number = 3;
	int count;
	
	for(count = 1;count <= number;count++){
		printf("Hallo World!!\n");
	}
	return 0;
}

运行效果:

通过上面的案例对比,第一感觉就是第二个案例比较简单的,不过反过来思考一下,如果是要显示出100个Hallo World!呢?那么第二个案例就行不通了,在平时的程序编程应用中,都是要处理很多的数据,使用for也是常常见到,通过对比,就体现出for和没有使用的区别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值