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语言之控制语言:分支和跳转的更多相关文章

  1. 第7章,c语言控制语句:分支和跳转

    7.1 if语句 通用形式:if(expression) statment 7.2 if else语句 通用形式:if(expression) startment else startment2 7. ...

  2. C控制语句--分支和跳转

    /*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...

  3. 【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转

    学习总结 1.if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else. 2.根据个人几年的编程经验,太多的if…else…嵌套会加大代码的 ...

  4. C 语言 - 分支、跳转和循环语句

    if 条件判断语句 if 语句结构 格式: if (表达式) { 语句; } 如果表达式成立,就执行大括号中的语句:否则跳过该 if 语句 #include <stdio.h> int m ...

  5. C Primer Plus学习笔记(六)- C 控制语句:分支和跳转

    if 语句: if 语句被称为分支语句(branching statement)或选择语句(selection statement) if 语句的通用形式: if (expression) state ...

  6. C语言讲义——结构化编程(分支、循环)

    顺序结构(从上到下) 分支结构(也叫选择结构) 循环结构 分支结构 if...else 最基本的分支结构是if(){}else{}. 为了代码的安全,同时也是出于代码规范的考虑,if()后面一定要加花 ...

  7. C语言进阶_分支语句

    勇气是在压力之下展现出的优雅. 一.简介 C语言提供了两种分支语句可供选用,一是if.......else....类型,一种是Switch语句.两种语句都能根据条件判断结果执行不同的指令,且能进行替换 ...

  8. [C语言入门笔记]分支结构与数组

    分支结构与数组 什么是分支结构? 分支结构是用户或者程序可以选择下一步执行哪个语句 分支结构有哪些? If If Else If Else If Switch 在初学者的学习过程中第一种和第二种比较普 ...

  9. C控制语句:分支和跳转

    小技巧:程序return前加个getchar();可以让程序停住.%%可以打印使printf()中打印出%号 #include<stdio.h>#define SPACE ''int ma ...

随机推荐

  1. Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  2. Spring Cloud Alibaba基础教程:使用Nacos作为配置中心

    通过本教程的前两篇: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方 ...

  3. [PHP] defunct僵尸进程

    1.如果子进程先于父进程退出, 同时父进程又没有调用wait/waitpid,则该子进程将成为僵尸进程 2.如果fork完就不管了可以使用 忽略子进程信号, 防止僵尸进程 pcntl_signal(S ...

  4. Java开发笔记(三十五)字符串格式化

    前面介绍了字符串变量的四种赋值方式,对于简单的赋值来说完全够用了,即便是两个字符串拼接,也只需通过加号把两个目标串连起来即可.但对于复杂的赋值来说就麻烦了,假设现在需要拼接一个很长的字符串,字符串内部 ...

  5. 委托(3).net 2.0中的委托

    由于.net 2.0引入了匿名方法,所以创建委托的方式可以更简化了. .net 2.0中的委托 有了匿名方法,上一篇的例子可以简化为: namespace DelegateDemo { //声明委托 ...

  6. 搞懂 JavaScript 继承原理

    在理解继承之前,需要知道 js 的三个东西: 什么是 JS 原型链 this 的值到底是什么 JS 的 new 到底是干什么的 1. 什么是 JS 原型链? 我们知道 JS 有对象,比如 var ob ...

  7. 常用weblogic搜索关键字

    NOTE:876004.1 - How to Apply WebLogic Server (WLS) Patches Using Smart Update [Video]NOTE:942815.1 - ...

  8. 使用ip开头的工具,而不是只会ifconfig

    结论: 1.使用ip 开头的工具,比ifconfig显示的信息更多,并且支持的功能更强大. 2.常用的功能有: 显示接口基本信息: ip link show dev eth0 设置端口up/down: ...

  9. MongoDB副本集功能及节点属性梳理

    副本集的主要功能 副本集是MongoDB高可用的基础,其主要作用 归纳为以下几点: (1)高可用,防止设备(服务器.网络)故障.提供自动FailOver功能. (2)无需配置高可用性虚拟节点:无论是S ...

  10. linux kernel内存碎片防治技术

    Linux kernel组织管理物理内存的方式是buddy system(伙伴系统),而物理内存碎片正式buddy system的弱点之一,为了预防以及解决碎片问题,kernel采取了一些实用技术,这 ...