C语言之控制语言:分支和跳转
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;//必须有一个标签
C语言之控制语言:分支和跳转的更多相关文章
- 第7章,c语言控制语句:分支和跳转
7.1 if语句 通用形式:if(expression) statment 7.2 if else语句 通用形式:if(expression) startment else startment2 7. ...
- C控制语句--分支和跳转
/*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...
- 【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转
学习总结 1.if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else. 2.根据个人几年的编程经验,太多的if…else…嵌套会加大代码的 ...
- C 语言 - 分支、跳转和循环语句
if 条件判断语句 if 语句结构 格式: if (表达式) { 语句; } 如果表达式成立,就执行大括号中的语句:否则跳过该 if 语句 #include <stdio.h> int m ...
- C Primer Plus学习笔记(六)- C 控制语句:分支和跳转
if 语句: if 语句被称为分支语句(branching statement)或选择语句(selection statement) if 语句的通用形式: if (expression) state ...
- C语言讲义——结构化编程(分支、循环)
顺序结构(从上到下) 分支结构(也叫选择结构) 循环结构 分支结构 if...else 最基本的分支结构是if(){}else{}. 为了代码的安全,同时也是出于代码规范的考虑,if()后面一定要加花 ...
- C语言进阶_分支语句
勇气是在压力之下展现出的优雅. 一.简介 C语言提供了两种分支语句可供选用,一是if.......else....类型,一种是Switch语句.两种语句都能根据条件判断结果执行不同的指令,且能进行替换 ...
- [C语言入门笔记]分支结构与数组
分支结构与数组 什么是分支结构? 分支结构是用户或者程序可以选择下一步执行哪个语句 分支结构有哪些? If If Else If Else If Switch 在初学者的学习过程中第一种和第二种比较普 ...
- C控制语句:分支和跳转
小技巧:程序return前加个getchar();可以让程序停住.%%可以打印使printf()中打印出%号 #include<stdio.h>#define SPACE ''int ma ...
随机推荐
- Centos 7 Puppet之foreman介绍安装测试
一.简介 1.前言(引用一下网上的资料) 随着企业的 Linux 系统数量越来越多,管理问题便成为一个相对麻烦并需要急 迫解决的问题,这里有 2 个 Key Message:1)统一管控体系非常重要, ...
- [JavaScript] audio在浏览器中自动播放
audio 在浏览器中自动播放 autoplay 属性 autoplay 属性规定一旦音频就绪马上开始播放. 如果设置了该属性,音频将自动播放. 使用 autoplay 属性进行播放 //使用auto ...
- 【响应式编程的思维艺术】 (1)Rxjs专题学习计划
目录 一. 响应式编程 二. 学习路径规划 一. 响应式编程 响应式编程,也称为流式编程,对于非前端工程师来说,可能并不是一个陌生的名词,它是函数式编程在软件开发中应用的延伸,如果你对函数式编程还没有 ...
- (摘)sql-索引的作用(超详细)
(一)深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonc ...
- linux 内核的优化
修改下面的这些参数,如果没有的话.直接复制进去就可以了 vim /etc/sysctl.conf 参数修改 vm.swappiness = net.ipv4.neigh. net.ipv4.conf. ...
- 18、实现strStr()
18.实现strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ( ...
- 原生js及H5模拟鼠标点击拖拽
一.原生js 1.拖拽的流程动作 鼠标按下 触发onmousedown事件 鼠标移动 触发onmousemove事件 鼠标松开 触发onmouseup事件 2.注意事项: 要防止div移出可视框,要限 ...
- React常见的15个问题
在 jsComplete,我们管理一个专门用于帮助编程学习者 slack 帐户.我们常常会收到一些有趣的问题,但大多数问题都是常见问题. 我创建这个资源为了帮助 React.js学习者遇到这些常见的问 ...
- Asp.Net MVC WebAPI的创建与前台Jquery ajax后台HttpClient调用详解
1.什么是WebApi,它有什么用途? Web API是一个比较宽泛的概念.这里我们提到Web API特指ASP.NET MVC Web API.在新出的MVC中,增加了WebAPI,用于提供REST ...
- console对象探究
作为一个前端,console.log()可能是你最常用的方法,打印打印再打印,但是其实console对象上有用的方法有很多,来,各位看官上眼 分类输出 厌倦了 console.log 单调的输出?欢迎 ...