C语言之控制语言:分支和跳转

时间:2022-02-01 17:33:59

if语句

#include<stdio.h>

int main(void)
{
const int FREEZING = 0;
float temperature;
int cold_days = 0;
int all_days = 0;
printf("Enter the list of daily low temperatures.\n");
printf("Use Celsius,and enter q to quit.\n");
while (scanf_s("%f", &temperature)) {
all_days++;
if (temperature < FREEZING)
cold_days++;
}
if (all_days != 0)
printf("%d days total:%.lf%% were below freezing.\n", all_days,
100.0*(float)cold_days / all_days);
if (all_days == 0)
printf("No data entered!\n");
system("pause");
return 0;
} /*
if (expression) 分支语句。如果expression为真,则执行statement
statement
*/

if else语句

/*取自上个例子*/
if (all_days != 0)
printf("%d days total:%.lf%% were below freezing.\n", all_days,
100.0*(float)cold_days / all_days);
else
printf("No data entered!\n");
/*
if (expression) 如果条件为真,执行statement1;如果条件为假,则执行statement2
statement1
else
statement2
*/
getchar()和putchar()

两者只处理字符

#include<stdio.h>
#define SPACE ' ' int main(void)
{
char ch;
ch = getchar();
/*该函数不带任何其他参数,它从输入队列中返回一个字符*/
while (ch != '\n')
{
if (ch == SPACE)
putchar(ch); //打印字符
else
putchar(ch + 1);
ch = getchar();
}
putchar(ch);
system("pause");
return 0;
}
ctype.h头文件
函数名 含义判断
isalnum() 字母或数字
isalpha() 字母
isblank() 标准的本地化空白字符
iscntrl() 控制字符
isdigit() 数字
isgraph() 除空格之外的任意可打印字符
islower() 小写字母
isprint() 可打印字符
ispunct() 除空格或字母数字字符以外的任何可打印字符
isspace() 空白字符
isupper() 大写字母
isxdigit() 十六进数字符
tolower() 返回小写字符
toupper() 返回大写字符
多重选择else if
#include<stdio.h>
#define RATE1 0.13230
#define RATE2 0.15040
#define RATE3 0.30025
#define RATE4 0.34025
#define BREAK1 360.0
#define BREAK2 468.0
#define BREAK3 720.0
#define BASE1 (RATE1*BREAK1)
#define BASE2 (BASE1+(RATE2*(BREAK2-BREAK1)))
#define BASE3 (BASE1+BASE2+(RATE3*(BREAK3-BREAK2))) int main(void)
{
double kwh;
double bill;
printf("Please enter the kwh used.\n");
scanf_s("%lf", &kwh);
if (kwh <= BREAK1)
bill = RATE1 * kwh;
else if (kwh <= BREAK2)
bill = BASE1 + (RATE2*(kwh - BREAK1));
else if (kwh <= BREAK3)
bill = BASE2 + (RATE3*(kwh - BREAK2));
else
bill = BASE3 + (RATE4*(kwh - BREAK3));
printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill);
system("pause");
return 0;
}
else与if配对

如果没有花括号,else与离他最近的if匹配,除非最近的if被花括号括起来了。

逻辑运算符

逻辑运算符 含义
&&
||
备用拼写:ios646.h头文件
逻辑运算符 替代
&& and
|| or
! not
优先级

!运算符只比圆括号优先级低,&&运算符比||高,但是二者的优先级都比关系运算符低,比赋值运算符高。

求值顺序

求值顺序是从左到右,程序在从一个运算对象执行到下一个运算对象之前,所有副作用都会生效。而且,C保障一旦发现某个元素让整个表达式无效,便立即停止求值。

范围
if (range >=90&&range<=100)

统计单词的程序

#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#define STOP '|' int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
bool inword = false;
printf("Enter text to be analyzed(| to terminate):\n");
prev = "\n";
while ((c = getchar()) != STOP) {
n_chars++;
if (c == '\n') {
n_lines++;
}
if (!isspace(c) && !inword) {
inword = true;
n_words++;
}
if (isspace(c) && inword) {
inword = false;
}
prev = c;
}
if (prev != '\n') {
p_lines = 1;
}
printf("characters = %ld,words = %d,lines=%d,", n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);
system("pause");
return 0;
}

条件运算符:?:

expression1?expression2:expression3
/*
如果expression1为真,那么整个条件表达式的值与expression2的值相同;
如果expression1为假,那么整个条件表达式的值与expression3的值相同;
*/
max = (a>b)?a:b;

循环辅助:continue和break

continue语句
#include<stdio.h>

int main(void)
{
const float MIN = 0.0f;
const float MAX = 100.0f;
float score;
float total = 0.0f;
int n = 0;
float min = MAX;
float max = MIN;
printf("Enter the first score(q to qiut):");
while (scanf_s("%f",&score)==1)
{
if (score<MIN||score>MAX)
{
printf("%0.1f is an invalid value.Try again:", score);
continue; //继续执行while
}
printf("Accepting %0.1f:\n", score);
min = (score < min) ? score : min;
max = (score > max) ? score : max;
total += score;
n++;
printf("Enter next score(q to qiut):");
}
if (n > 0)
{
printf("Average of %d scores is %0.1f.\n", n, total);
printf("Low = %0.1f,high=%0.1f\n", min, max);
}
else
printf("No valid scores were entered.\n");
system("pause");
return 0;
}
break
#include<stdio.h>

int main(void)
{
float length, width;
printf("Enter the length of the rectangle:\n");
while ((scanf_s("%f", &length)) == 1)
{
printf("Length = %0.2f\n", length);
printf("Enter its width:\n");
if ((scanf_s("%f", &width) != 1))
break; //使得跳出这个循环
printf("Width = %0.2f", width);
printf("The area of the rectangle is %f", length*width);
printf("Enter the length of the rectangle:\n");
}
printf("Done!\n");
system("pause");
return 0;
}

多重选择:switch和break

switch(expression) //expression只能是一个值,而不能是范围
{
case expression_1: //只会读取首字母
statement1;
break;
case expression_2:
statement2;
break;
case expression_3:
statement3;
break;
......
default:expression_end;//如果没有对应的case,则跳转到default来
}
多重标签:
#include<stdio.h>

int main(void)
{
char ch;
int a_ct, e_ct, i_ct, o_ct, u_ct;
a_ct = e_ct = i_ct = o_ct = u_ct = 0;
printf("Enter some text;enter # to quit.\n");
while ((ch=getchar())!='#')
{
switch (ch)
{
case 'a':
case 'A':a_ct++;
break;
case 'e':
case 'E':e_ct++;
break;
case 'i':
case 'I':i_ct++;
break;
case 'o':
case 'O':o_ct++;
break;
case 'u':
case 'U':u_ct++;
break;
default:break;
}
}
printf("number of vowels:A:%4d E:%4d I:%4d O:%4d U:%4d\n",a_ct,e_ct,i_ct,o_ct,u_ct);
system("pause");
return 0;
}

goto语句

goto part;//跳转到part去
part:statement;//必须有一个标签