选择结构
1. 二选一
结构: if( ) //括号内为判断语句
{ //为满足if条件时需要执行的语句
}
else
{ //为不满足if条件时需要执行的语句
}
2.多选一
结构:
switch( )//括号内为整形变量或枚举类型
{ case 1:
break;
case 2:
break;
.
.
default :
break;
}
循环结构
知道次数的循环 for循环
for 循环的结构:
for(循环变量;循环条件;循环变量改变)
{
//循环内容
}
不知道次数的循环 while 循环 do while 循环
while 循环结构:
while()//括号内放循环条件
{
}
do while 循环结构:
do
{
}while();//循环条件
注 while 循环与do while 循环的区别
while 循环可能出现一次都不执行
do while 循环即使条件都不满足也会执行一次
do while 循环结构在while()后面有;
例题
打印9 9乘法表
//#include <stdio.h>
//
//int main()
//{
//
int h;
//
int l;
//
//
for(h=1;h<=9;h++)
//
{
//
for(l=1;l<=h;l++)
//
{
//
printf("%d*%d=%d\t",l,h,h*l);
//
}
//
//
printf("\n");
//
}
//
//
//
//
return 0;
//}
打印数字
/**
打印结果:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
*/
//#include <stdio.h>
//
//
//int main()
//{
//
int h;
//
int lie;
//
//
for(h=1;h<=4;h++)
//
{
//
for(lie=h;lie<=h+4;lie++)
//
{
//
printf("%d\t",lie);
//
}
//
//
printf("\n");
//
}
//
//
//
return 0;
//}
打印*
/** 打印如下图形
*
***
*****
*******
*********
*/
//#include <stdio.h>
//
//int main()
//{
//
int h;
//
int k;
//
int x;
//
//
for(h=1;h<=5;h++)
//
{
//
//输出空格
//
for(k=1;k<=5-h;k++)
//
{
//
printf(" ");
//
}
//
//输出星
//
for(x=1;x<=2*h-1;x++)
//
{
//
printf("*");
//
}
//
//
//换行
//
printf("\n");
//
}
//
//
//
return 0;
//}