您的位置:首页 > 编程语言 > C语言/C++

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]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: