5.流程控制语句

流程控制语句

  • 顺序语句

特点:语句逐句执行

  • 选择语句

特点:出现了一种以上的情况时使用

  • 循环语句

在某个条件成立的情况下,循环执行某个语句、

关键字:

  • 选择:if-else, which-case;
  • 循环:while, do-while,for,if-goto;
  • 辅助控制:continue,break,and so on.

1.if-else语句

语句格式:

if(表达式)
    命令1;
[else
    命令2;
]
注意:使用该种表达式时,else默认与最近的if语句匹配。

if(表达式)
{
    命令1;
}
[else
{
    命令2;
}]

Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    while(1)
    {
		    float s;
      printf("Enter the Scores:");
        scanf("%f",&s);
        //首先规定分数必须0~100
        if(s < 0 || s > 100)
        {
            printf("请输入一个合理的分数!\n");
            //fprintf(stderr,"Input Error!\n");
            //break; 
			exit(1);
        }
        //判断scores属于哪个区间
        if(s >= 90 && s <= 100)
        {
            printf("分数等级为A!\n");
            //puts("A");字符专用输出语句
			continue;
		}
        if(s >= 80 && s < 90)
        {
            printf("分数等级为B!\n");
			continue;
        }
        if(s >= 70 && s < 80)
        {
            printf("分数等级为C!\n");
			continue;
        }
        if(s >= 60 && s < 70)
        {
            printf("分数等级为D!\n");
			continue;
        }
        else
        {
			printf("分数等级为E!\n");
			continue;
        }

	}

	exit(0);
}

输出结果:

tianxun@tianxun:~/Desktop/C-Language/progress$ ./if-else 
Enter the Scores:-1
请输入一个合理的分数!
tianxun@tianxun:~/Desktop/C-Language/progress$ ./if-else 
Enter the Scores:101
请输入一个合理的分数!
tianxun@tianxun:~/Desktop/C-Language/progress$ ./if-else 
Enter the Scores:99
分数等级为A!
Enter the Scores:87
分数等级为B!
Enter the Scores:78
分数等级为C!
Enter the Scores:65
分数等级为D!
Enter the Scores:59
分数等级为E!
Enter the Scores:105
请输入一个合理的分数!

Example2:判断闰年与平年

变量:year;
输入:year;
判断条件:year 可以被4整除且不能被100整除或者可以被400整除。

#include <stdio.h>
#include <stdlib.h>

int main()
{
  while(1)
  {
        int year;
        printf("Please Input Year : ");
        scanf("%d",&year);
    
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        {
             printf("%d is leap year!\n",year);
        }
        else
        {
            printf("%d isn't leap year!\n",year);
            exit(1);
        }
  }

  exit(0);
}

输出结果:

xs@jxs-ubuntu:~/Desktop/c  language/if-else$ ./year 
Please Input Year : 2020
2020 is leap year!
Please Input Year : 2022
2022 isn't leap year!

2.switch-case语句

语句格式

switch(表达式)
{
    //常量表达式:字符、字符串、常量等,不能是表达式
    case 常量表达式:
    [CMD];
    break;
    case 常量表达式:
    [CMD];
    break;
    ···
    default:  //当表达式均不满足上述常量表达式时,默认执行此命令
    [CMD];
    break;
}

Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    ch = getchar();
    
    switch(ch)
    {
        case 'A':
        case 'a':
        printf("Ahahahhahah\n");
        
        default:
        printf("Please Repeat Input Again!\n");
    }
    exit(0);
}

输出结果:


break表示退出当前switch语句,如果没有break,就会继续执行下一个case语句,出现程序执行达不到目的。

3.while ,do-while语句

语句格式

while:

while (表达式)   //满足该表达式时,开始循环
    {
       循环体语句;
    }
    
do-while:

do
{
    循环体语句;
} while(表达式);  //满足表达式时继续循环,不满足就只执行一次退出循环
  • while逻辑结构:
  • do-while逻辑结构:

两者的区别在于:

  • while 中的循环体至少执行的次数为0次,原因在于进入循环之前要先判断条件是否满足,不满足不会执行!
  • do-while中的循环体至少执行的次数为1次,原因在于终止条件在执行一次语句之后,因此是先执行循环体,在判断是否满足终止条件,无论什么情况下都要执行一次循环体。

4.for & if-goto循环语句

语句格式

//for由初始条件判断,因此至少执行0次
for(表达式1;表达式2;表达式3)  //先执行表达式1,在判断表达式2是否成立,成立的话就执行循环体,最后执行表达式3
{
    循环体;
}

if-goto  //无条件执行跳转,慎用,且不能跨函数执行。
  • for 逻辑结构:

    Example:
for(i = LEFT; i <= RIGHT; i++)
{
    sum += i;
}
<==>
for( ; ; )  //分号不能少
{
    sum += i;
    i++;
    if(i > RIGHT)
        break;
}

5.Quiz

A以每年10%的单利息投资了100美元,B以每年5%的复合利息投资了100美元,使用程序计算需要多少年B的投资总额才会超过A,并显示该时刻两个人各自的资产总额为多少?
利不生利本利 = 本金 + 单利息 =本金 + (本金 * 利率 * 期限)= 本金 * (1 + 利率 * 期限)
利滚利本利 = 本金 + 复利息 =本金 + 本金 * 利率 ^期限 = 本金 * (1 + 利率 ^期限)

A的总金额 : 100 \times (1 + 10\% \times n)\\
B的总金额 : 100 \times (1 + 5\% ^{n})

待求解,即需要定义的两个变量:n 和 总金额total,
针对两个变量的操作:打印输出total;
逻辑:循环判断total_A 与 total_B的值,直到两者相等,并打印输出总金额。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int year;
    float total_A = 0,total_B = 0;

        for(year = 1; year > 0; year++)
        {
                total_A = 100 * (1 + 0.1 * year);
                total_B = 100 * pow((1 + 0.05),year);

                if(total_A == total_B)
                {
                        float total = total_A;

                        printf("The total investment of A and B is equal when n = %d ,the investment is %f",year, total);
                        break;
                }

        }
        exit(0);
}

posted @ 2023-06-29 17:06  假行僧me  阅读(4)  评论(0编辑  收藏  举报