C语言循环结构篇
2015-07-16 00:17
375 查看
Three Day一、while、do_while语法形式:while (表达式) {
循环体 }
二、for语句
for语句是C语言中最灵活功能最强的循环语句
语法形式:for(表达式1;表达式2;表达式3){
循环体 }
例:计算1+2+3+4的和
#include <stdio.h>int main(void) {int s = 0;int i;for(i = 1; i < 5; i++) {s += i;}printf("%d", s);}
输出结果:10
三、break语句
break语句用于终止循环的执行(break所在最近循环体)
1、跳出循环
#include<stdio.h>int main(void) {int num;for(num = 0; num < 5; num++) {if (num == 3) {break; //当num=3时,跳出循环for}printf("%d\n", num);}}输出结果:0 1 2[/code]
<pre><span style="font-family:microsoft yahei;"><span style="white-space: pre-wrap;">2、跳出最近的循环</span></span>
#include<stdio.h>int main(void) {<span style="white-space:pre"> </span>int num = 0;while(num < 1) {printf("while\n");for(; num < 1; num++) {break;//跳出离它最近的循环,并且该break语句包含在该循环内printf("for\n");}<pre name="code" class="csharp"><span style="font-family: 'microsoft yahei'; white-space: pre-wrap;"><span style="white-space:pre"> </span>//break后的地方</span>}}[/code]//输出结果:无限输出while[/code]
[code]四、contiue语句continue结束本次循环[/code]
#include <stdio.h>int main(void) {int i;for(i = 0; i < 6; i++) {<span style="white-space:pre"> </span>if(i == 3) {<span style="white-space:pre"> </span>continue; //跳出本次循环}printf("%d", i);}}输出结果:0 1 2 4 5[/code]
五、语句练习
1、九九乘法表(while)
#include<stdio.h>int main(void) {int j = 0, i = 0;while(j < 9) {j++;i = 1;while(i <= j) {printf("%d*%d=%d\t", i, j, i * j);i++;}printf("\n");}}输出结果:[/code]2、有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
#include<stdio.h>int main(void) {int a, b, c, d = 0;for (a = 1; a < 5; a++) {for (b = 1; b < 5; b++) {if (a == b) {continue;}for (c = 1; c < 5; c++) {if (a == b || b == c) {continue;}printf("%d%d%d\n", a, b, c);d++;}}}printf("总数为:%d", d);}输出结果:36[/code]
3、打印矩形
#include<stdio.h>int main(void) {int i, j;for (j = 0; j < 2; j++) {for (i = 0; i < 4; i++) {printf("*");}printf("\n");}}输出结果:[/code]
**********
4、打印平行四边形
#include <stdio.h>int main(void) {int i, j;for(j = 0; j < 3; j++) {for(i = 0; i < 6 - j; i++) {if(i < (2 - j)) {printf(" ");}else {printf("*");}}printf("\n");}}输出结果:[/code]
*****
**********
5、左直角三角形
#include<stdio.h>int main() {int x, y;for (y = 0; y <= 4; y++) {for (x = 0; x <= 4; x++) {if (x >= 0 && y <=4 && y >= x) {printf("*");} else {printf(" ");}}printf("\n");}}输入结果:***************[/code]
6、实心菱形
#include<stdio.h>int main() {int x, y;for (y = -3; y <= 3; y++) {for (x = -3; x <= 3; x++) {if (y <= x + 3 && y >= -x -3 && y >= x - 3 && y <= -x + 3) {printf("*");} else {printf(" ");}}printf("\n");}}输出结果:*************************[/code]
7、空心菱形
#include <stdio.h>int main(void) {int x, y;for(y = 0; y <= 6; y++) {for(x = 0; x <= 6; x++) {if(y == x + 3 || y == x - 3 || y == 3 - x || y == 9 - x){printf("*");} else {printf(" ");}}printf("\n");}}输入结果:** ** ** ** ** **[/code]
8、爱心
#include <stdio.h>int main(void) {int x, y;for (y = 0; y <= 8; y++) {for (x = -6; x < 0; x++) {if (y <= x + 8 && x >= -6 && y >= -x -5 && y >= 0 && y >= x + 3 ) {printf("*");} else {printf(" ");}}for (x = 0; x <= 6; x++) {if (y <= -x + 8 && x <= 6 && y >= x -5 && y >= 0 && y >= -x + 3 && x >= 0) {printf("*");} else {printf(" ");}}printf("\n");}}输出结果:*** ******** *********** ******************************************[/code]
相关文章推荐
- C语言基础2
- C语言程序设计 6章
- C++指针(我所了解的)
- 菜鸟学习c++—实现学生登陆管理系统
- 我的C++笔记(语句基本结构)
- 我的C++笔记(Hello World)
- 用C语言编写一系列简单的程序
- C/C++的自动类型转换
- C语言初级之strstr、strcmp
- C语言宏定义使用技巧
- C语言--程序设计基础--6章
- C++获取当前系统时间
- jni 简单实践 eclipse支持c++11
- C语言函数之可变参数原理:va_start、va_arg及va_end
- ChooseColor函数
- 【学习笔记】【OC语言】继承
- 关于c++的成员初始化列表
- C语言基础之分支结构—if...else—elseif—switch...case
- [C语言]进阶|程序结构
- C语言语句篇