c语言基础(二)

控制流
控制流语句是在程序中控制程序逻辑结构的,常用的有分支结构和循环结构。
一、分支结构
1、if语句
int x = 10;
int y = 20;

(一)当只有两种可能情况的时候:
if (x < y)
{
         printf("x<y\n");
}
else
{
        printf("x>=y\n");
}
(二)当有多种可能情况的时候:

if (x < y)
{
         printf("x<y\n");
}
else if(x = y)
{
        printf("x=y\n");
}
else
{
        printf("x>y\n");
}
2、switch语句
int x = 10;
int y = 20;
int ret = (x>y);

switch(ret)
{
        case 0 :
                    printf("x<=y");
                    break;
        case 1:
                   printf("x>y");
                   break;
        default:
                   break;      
}
二、循环语句
1、 入口条件循环:在进入循环之前先判断条件,条件成立才进入循环

int count;
count = 0;
while (count<100)
{
printf("%d\n",count);
count++;
}
exit(0);
2、出口条件循环:先执行一遍循环,再判断条件是否成立,如果条件成立再次进入循环,如果条件不成立则退出循环。
int count;
count = 0;
do{
        printf("%d\n",count);
        count++;
}while(count<100);
3、for循环
int count;
for(count = 0;count<100;count++)
{
        printf("%d\n",count);
}
4、goto
弊端:会使程序结构变得特别复杂,也会使程序出现漏洞。
int count = 0;
lable:------------------------>标签名:标记作用
        printf("x\n") ;
        if(count < 100)
        {
                 count++;
                 goto lable;
        }
        printf("%d\n",count);








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值