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 ...
随机推荐
- ROS笔记3 理解nodes
http://wiki.ros.org/ROS/Tutorials/UnderstandingNodes 介绍几个命令行工具用法 roscore rosnode rosrun A node reall ...
- [转]Centos 7搭建Gitlab服务器超详细
本文转自:https://blog.csdn.net/duyusean/article/details/80011540 可参考:https://about.gitlab.com/install/#c ...
- [Redis] redis的设计与实现-对象系统
1.redis并没有直接使用前面的数据结构实现键值对数据库,而是基于数据结构创建了一个对象系统,字符串对象/列表对象/哈希对象/集合对象/有序集合对象都用到了至少一种前面的数据结构2.针对不同的使用场 ...
- Java开发笔记(七十六)如何预防异常的产生
每个程序员都希望自己的程序稳定运行,不要隔三岔五出什么差错,可是程序运行时冒出来的各种异常着实烦人,令人不胜其扰.虽然可以在代码中补上try/catch语句捕捉异常,但毕竟属于事后的补救措施.与其后知 ...
- arcgis api 3.x for js 入门开发系列八聚合效果(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- Azure WebJob-Custom Schedule for Azure Web Job Timer Triggers
如果想实现Azure Schedule WebJob,有两种方法: 1. 配置CRON Expression,网上有在线CRON配置工具,根据业务需要配置即可 注意:Azure的CRON Expres ...
- GsonFormat插件
GsonFormat插件可以根据JSONObject格式的字符串,自动生成实体类参数. 要使用这个插件,首先要做的事下载它.方法如下: 方法一: 1.Android studio File->S ...
- JPasswordField密码框,JList列表框
[JPasswordField密码框] //导入Java类 import javax.swing.*; import java.awt.*; import java.awt.event.ActionE ...
- Netty学习笔记(三) 自定义编码器
编写一个网络应用程序需要实现某种编解码器,编解码器的作用就是讲原始字节数据与自定义的消息对象进行互转.网络中都是以字节码的数据形式来传输数据的,服务器编码数据后发送到客户端,客户端需要对数据进行解码, ...
- js计算剩余分钟
// 剩余时间提醒 function checkTime() { if (timeCompare()) { document.getElementById('distanceDeadline').in ...