C语言 -- 分支语句和循环语句

 

【C语言】

操作符https://mp.csdn.net/editor/html/115218055

数据类型https://mp.csdn.net/editor/html/115219664

自定义类型:结构体、枚举、联合https://mp.csdn.net/editor/html/115373785

变量、常量https://mp.csdn.net/editor/html/115230188

分支、循环语句https://mp.csdn.net/editor/html/115234118

字符串+转义字符+注释https://mp.csdn.net/editor/html/115231391

指针https://mp.csdn.net/editor/html/115281303

数组https://mp.csdn.net/editor/html/115266750

函数https://mp.csdn.net/editor/html/115265396

 

内容来自   B站  C语言教学视频   https://www.bilibili.com/video/BV1RX4y1u7Zh

 

 

分支、循环语句

1.分支语句:

1.1   if  语法结构:

 

1.2  switch 分支语句

2.循环语句

2.1  while  循环语句

2.2  for 循环语句

2.3  do while  循环语句

3. goto语句


 


 


C语言中如何实现循环?   while语句     for语句   do...while语句

什么是语句?
C 语言中由一个分号 ; 隔开的就是一条语句。 比如:
printf("hehe");
1+2;
 
 

1.分支语句:

1.1   if  语法结构:

//单分支
if(表达式)
   语句;


//双分支
if(表达式)   
   语句1;
else
   语句2;


//多分支    
if(表达式1)
   语句1;
else if(表达式2)
   语句2;
else
   语句3;


解释一下: 如果表达式的结果为真,则语句执行。

在C语言中如何表示真假?   0表示假,非0表示真。

 

注意: 
   int age = 6; 
   if (18<=age<28) //错误的表达方式,执行顺序 首先判断 18<=age 为假(0)下一步判断 0<28 真,所以执行内部语句
   printf("青年\n");//此时会打印青年,为什么呢
   if(18<=age && age<28)//正确的表达方式   &&-并且   ||-或
   printf("青年\n");//不会打印

 

悬空else   else跟离它最近的if匹配(同一个代码块中)对齐

   int a = 0;                                     int a = 0;
   int b = 2;                                     int b = 2;
   if(a == 1)                                     if(a == 1)
     if(b == 2)                     匹配格式>         if(b == 2)
       printf("hehe\n");//不打印                         printf("hehe\n");
   else                                              else
       printf("haha\n");  //不打印                       printf("hehe\n");
#include <stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
   {
        if(b == 2)
       {
            printf("hehe\n");
       }
   }
    else
   {
            printf("haha\n");
   }       
    return 0; }

 

1.2  switch 分支语句

 
switch(整型表达式)
{
   
case 整形常量表达式:
      语句;
}
 
switch 语句中的 break
switch 语句中,我们没法直接实现分支,搭配 break 使用才能实现真正的分支。
#include <stdio.h>
int main()
{
    int day = 0;
    switch(day)
   {
        case 1:
            printf("星期一\n");
            break;
        case 2:
            printf("星期二\n");
            break;
        case 3:
            printf("星期三\n");
            break;    
        case 4:
            printf("星期四\n");
            break;    
        case 5:
            printf("星期五\n");
            break;
        case 6:
            printf("星期六\n");
            break;
        case 7:
            printf("星期天\n");    
            break;
   }
    return 0; }

 

default 子句
如果表达的值与所有的 case 标签的值都不匹配怎么办?
其实也没什么,结构就是所有的语句都被跳过而已。
程序并不会终止,也不会报错,因为这种情况在 C 中并不认为适合错误。
但是,如果你并不想忽略不匹配所有标签的表达式的值时该怎么办呢?
你可以在语句列表中增加一条 default 子句,把下面的标签
default
写在任何一个 case 标签可以出现的位置。
switch 表达式的值并不匹配所有 case 标签的值时,这个 default 子句后面的语句就会执行。
所以,每个 switch 语句中只能出现一条 default 子句。
但是它可以出现在语句列表的任何位置,而且语句流会像贯穿一个 case 标签一样贯穿 default 子句。
 
switch  输入1-5是weekday  6-7是weedend
    #include <stdio.h>
   int main()
   {
    int i;
    scanf("%d\n", &i);
    switch(day)
    {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
         printf("工作日\n");
         break;
      case 6:
      case 7:
         printf("休息\n");
         break;
      default;
         printf("输入错误\n");
         break;
    }
     return 0;
   }

break语句的实际效果是把语句列表划分为不同的部分。

 switch 中的 break 和 continue
   break用于永久终止循环,同代码块后面语句不再读取,如果没遇到break那么后面代码继续执行 ; continue 终止本次循环,下次可再读取同代码块后面语句

#include <stdio.h>
   int main()
   {
     int x = 3; int y = 3;
     switch(x % 2)   // 1
     case 1:
        switch(y)
        {
          case 0:
            printf("first");
          case 1:
            printf("second");
            break;
          default: printf("hello");
        }
      case 2:
         printf("third");
     return 0;
   }  //最后打印结果为 hello third


 

 

2.循环语句

2.1  while  循环语句

 
while 语法结构
while(表达式)
   循环语句;
#include <stdio.h>
int main()
{
   int i = 1;
   while(i<=10)
   {
     printf("%d ", i);
     i = i+1;
   }
   return 0;
}

 

while语句中的breakcontinue 

break

//break 代码实例
#include <stdio.h>
int main()
{
   int i = 1;
   while(i<=10)
   {
      if(i == 5)
      break;
      printf("%d ", i);
      i = i+1;
   }
   return 0;
}
打印结果  1 2 3 4

总结: break在while循环中的作用:
其实在循环中只要遇到break,就停止后期的所有的循环,直接终止循环。 所以:while中的
break是用于永久终止循环的。
 

contiune

//continue 代码实例1
#include <stdio.h>
int main()
{
   int i = 1;
   while(i<=10)
   {
     if(i == 5)
     continue;
     printf("%d ", i);  
     i = i+1;
   }
   return 0; 
}  
打印结果:1 2 3 4


//continue 代码实例2
#include <stdio.h>
int main()
{
   int i = 1;
   while(i<=10)
   {
      i = i+1;
      if(i == 5)
      continue;
      printf("%d ", i);
   }
 return 0;
 }
 结果  :2 3 4 6 7 8 9 10


总结: continue在while循环中的作用就是:
continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再执行,而是直接
跳转到while语句的判断部分。进行下一次循环的入口判断。

 

 getchar()函数     putchar()函数

 #include <stdio.h>
   int main()
  {
      int ch = 0 ;
                                      //EOF end of file 文件结束标志 本质上为 -1
      while ((ch = getchar()) != EOF) //输入函数getchar() 键盘输入字符   输入 ctrl + z 结束
                                      //int ch = putchar();//输出函数 putchar()
      {
          putchar(ch);                //显示打印ch的值
                                      //printf("%c\n", ch);
      }
      return 0;
  }
  

   #include <stdio.h>
   int main()
  {
    int ret = 0;
    int ch = 0;
      char password[20] = {0};
      printf("请输入密码:");  
      //输入12345 但缓存区内为 12345\n
      scanf("%s", password); //输入密码,并放在password数组中  
      //缓存区还剩一个\n
      //读取一下\n  
      while ((getchar()) != '\n') //循环直到读取到\n  多个多余字符如12345asc
      {
        ;//空语句
      }
      printf("请确认(Y/N):");
      ret = getchar();// Y/N
      if(ret == 'Y')
        printf("确认成功\n");
      else
        printf("确认失败\n");
      return 0;
  }


//代码1
#include <stdio.h>
int main()
{
	int ch = 0;
	while ((ch = getchar()) != EOF)
		putchar(ch);
	return 0;
}

//代码2
#include <stdio.h>
int main()
{
	while ((ch = getchar()) != EOF)
	{
		if (ch < ‘0’ || ch > ‘9’)
			continue;
		putchar(ch);
	}
	return 0;
}
数据读取,缓存区
 
 
 
 
 
 
 

2.2  for 循环语句

for 循环
我们已经知道了 while 循环,但是我们为什么还要一个 for 循环呢? 首先来看看 for 循环的语法:
for(表达式1;表达式2;表达式3)
 循环语句;

表达式1 表达式1为初始化部分,用于初始化循环变量的。 表达式2 表达式2为条件判断部分,用于判断
循环时候终止。 表达式3 表达式3为调整部分,用于循环条件的调整。


使用for循环 在屏幕上打印1-10的数字。
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)  // for(i=1/*初始化*/; i<=10/*判断部分*/; i++/*调整部分*/)
	{
		printf("%d ", i);
	}
	return 0;
}


现在我们对比一下for循环和while循环。
int i = 0;
                     //实现相同的功能,使用while
i = 1;               //初始化部分
while (i <= 10)      // 判断部分
{
 printf("hehe\n");
 i = i + 1;          //调整部分
}
                     //实现相同的功能,使用while
for (i = 1; i <= 10; i++)
{
    printf("hehe\n");
}
可以发现在while循环中依然存在循环的三个必须条件,但是由于风格的问题使得三个部分很可能偏离
较远,这样查找修改就不够集中和方便。所以,for循环的风格更胜一筹。 for循环使用的频率也最高。
break continue for 循环中
我们发现在 for 循环中也可以出现 break continue ,他们的意义和在 while 循环中是一样的。 但是还是
有些差异:
//代码1
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		if (i == 5)
			break;
		printf("%d ", i); //  1 2 3 4
	}
	return 0;
}

//代码2
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		if (i == 5)
			continue;
		printf("%d ", i); // 1 2 3 4 6 7 8 9 10
	}
	return 0;
}

 

for 的变种

#include <stdio.h>
int main()
{
    int i = 0;
    int j = 0;
    //for循环的初始化、判断、调整都可以省略,但是for循环的判断部分被省略,
    //那判断条件就是恒为真,如果不是非常熟练,建议不要随便省略
/*  for( ; ; )  //进入死循环
    {
       printf("hehe\n");
    }
*/
    for (; i < 10; i++)  //省略条件部分 i=0 开始
    {
        for (; j < 10; j++)
            //从上个的第一次for循环开始,进入本次for循环,j从0到9循环十次并且打印了十次,j=10时,
            //结束本次for循环,然后到上个for循环,i=1,进入到本次for循环时,j没有销毁,此时j=10;跳出
        {
            printf("哈哈\n"); //打印十次 哈哈
        }
    }
    return 0;
}

 

 

2.3  do while  循环语句

语法:
do
循环语句;
while(表达式);
do 语句的特点
循环至少执行一次,使用的场景有限,所以不是经常使用。
#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}
do while 循环中的 break continue
#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		if (5 == i)
			break;
		printf("%d\n", i);  //10
	} while (i < 10);
	return 0;
}

#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		if (5 == i)
			continue;
		printf("%d\n", i); // 10
	} while (i < 10);
	return 0;
}

 

3. goto语句

C 语言中提供了可以随意滥用的 goto 语句和标记跳转的标号。
从理论上 goto 语句是没有必要的,实践中没有 goto 语句也可以很容易的写出代码。
但是某些场合下 goto 语句还是用得着的,最常见的用法就是终止程序在某些深度嵌套的结构的处理过
程,例如一次跳出两层或多层循环。
这种情况使用 break 是达不到目的的。它只能从最内层循环退出到上一层的循环
一个关机程序
#include <stdio.h>
int main()
{
	char input[10] = { 0 };
	system("shutdown -s -t 60");
again:
	printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
	scanf("%s", input);
	if (0 == strcmp(input, "我是猪"))
	{
		system("shutdown -a");
	}
	else
	{
		goto again;
	}
	return 0;
}

goto语言真正适合的场景如下:
for (...)
for (...)
{
	for (...)
	{
		if (disaster)
			goto error;
	}
}
…
error :
if (disaster)
// 处理错误情况

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值