当前课程知识点:C Programming >  Chapter 4 Selection Structure >  4.1 Relational operators, logical operators, and if statements >  4.1 Relational operators, logical operators, and if statements.mp4

返回《C Programming》慕课在线视频课程列表

4.1 Relational operators, logical operators, and if statements.mp4在线视频

下一节:Discussion unit

返回《C Programming》慕课在线视频列表

4.1 Relational operators, logical operators, and if statements.mp4课程教案、知识点、字幕

大家好

我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天

我们讲解C语言的关系

逻辑运算符和if语句

C语言有两种选择语句

if语句

实现两个分支的选择结构

switch语句

实现多分支的选择结构

看一个实例

求一元二次方程

ax2+bx+c=0的根

要想求出上述方程的各种根

必须判断

b2-4ac的值

在什么范围

来看例4.1

求ax2+bx+c=0

方程的根

由键盘输入a,b,c

假设

a,b,c的值任意

并不保证b2-4ac>=0

需要在程序中进行判别

如果

b2-4ac>=0

就计算并输出方程的两个实根

否则就输出

方程无实根的信息

解题思路如图所示

输入a,b,c

计算disc=b2-4ac

判断disc<0是否成立

则输出无实根

则计算x1,x2

然后输出x1,x2

源程序如下

定义变量

a,b,c

disc

x1

x2

p、q

用scanf

输入

a,b,c的值

例如

6

3

1

计算b2-4ac

disc的值

变为-15

如果disc<0

输出无实根

else

否则

输出两个实根

运行输入6

3

1

由于-15<0

为真

输出无实根

下面介绍关系运算符

和关系表达式

关系运算符

用来对两个数值

进行比较的比较运算符

C语言提供6种关系运算符

<

<=

>

>=

==

!=

其中

<

<=

>

>=

在6号优先级

==

!=

在7号优先级

它们都低于算术运算符

高于赋值运算符

结合方式

自左向右

例如

a>b+c

等价于

a>(b+c)

a==b

等价于

a==(b

a=b

等价于a=(b

注意

在C语言中

关系表达式的值

只有1或0

其中1表示真

0表示假

例如

4>5的值为0

5>4的值为1

区分

等于运算符

和赋值运算符

关系

等于运算符是两个连续的等号

赋值运算符

是一个单独的等号

同学们来思考一下

关系表达式

a>b>c为1

能否保证 a是a,b,c中

最大值

下面介绍逻辑运算符

和逻辑表达式

有3种逻辑运算符

&&(逻辑与)

||(逻辑或)

!(逻辑非)

与、或、非

运算表如下

仅当

a,b均为非零值时

逻辑与

运算结果为1

a,b有其一为0

或均为0时

逻辑与运算结果为0

仅当a,b均为零时

逻辑或运算结果为.0

a,b

有其一为非零值

或均为非零值时

逻辑与运算结果为1

逻辑非

为单目运算符

a为非零值时

!a为0

a为0时

!a为1.

编译系统在表示逻辑运算结果时

逻辑表达式的值

只有1或0

其中1表示真

0表示假

但在判断一个量

是否为真时

以0

代表假

以非0代表真

关系运算和逻辑运算

结果只能用1表示真

用0表示假

逻辑运算符的优先次序

!=在2号优先级

&&在11号优先级

||在12号优先级

&&和||的优先级

低于算术运算符

关系运算符

但高于赋值运算符

例如

表示年龄age

在13至17岁之间的逻辑表达式为

age>=13

&&

age<=17

表示年龄age

小于12

大于65的逻辑表达式为

age<12

|| age>65

表示a是a,b,c中的最大值的

逻辑表达式为

a>b && b>c

注意

在求解逻辑表达式时

若&&运算符

左边表达式的值为0

即假

则不再进行&&右边表达式的运算

且得到

&&表达式的值为0;

在求解逻辑或表达式时

若||运算符左边表达式的值为1

即真

则不再进行||右边表达式的运算

且得到||表达式的值为1

这称为短路现象

只有在必须执行逻辑运算符

右边表达式

才能得出整个逻辑表达式的值时

才进行右边表达式的计算

例如

若有int a=2

b=4

x=1,y=2;

则执行表达式

(x=a>b)

&&(y=b>a)后

x=0,y=2

下面介绍if语句

我们来看例4.2

输入两个实数

按代数值由小到大的顺序

输出这两个数

解题思路为

只需做一次比较

然后进行一次交换即可

用if语句实现条件判断

关键是怎样实现两个变量值的互换

不能把两个变量直接互相赋值

为了将a和b对换

不能用a=b

b=a

为了实现互换

必须借助于第3个变量

可以这样考虑

将A和B

两个杯子中的水互换

用两个杯子的水

倒来倒去的办法

是无法实现的

必须借助于第3个杯子C

先把

A杯的水倒在C杯中

再把

B杯的水倒在A杯中

最后

再把C杯的水倒在B杯中

这就实现了两个杯子中

水互换

这是在程序中

实现两变量换值的算法

程序如下

如果a>b

通过复合语句

{ t=a;

a=b;

b=t; }

实现两变量值的交换

为a,b输入值

3.6和

-3.2

则输出a,b 的值为-3.2

3.6

有if语句单分支

if语句双分支

和if语句多分支语句

if语句单分支

格式为

if (表达式) 语句

其中

表达式可为算术

关系

逻辑

赋值等表达式

语句可为

赋值语句

函数调用语句

控制语句

复合语句

或者空语句

功能是

计算表达式的值

如果是一个非0值

即逻辑真

就执行内嵌语句

否则

即逻辑假

跳过内嵌语句

顺序执行后续语句

流程图

如图4.1所示

if双分支语句

格式为

if (表达式)

语句1

else

语句2

功能

计算表达式的值

如果它的值

是一个非0值

即逻辑真

则执行内嵌语句1

之后

跳过内嵌语句2

执行后续语句

否则

跳过内嵌语句1

执行内嵌语句2

之后执行后续语句

流程图

如图4.2所示

if 多分支语句

格式为

if (表达式1)

语句1

else if

(表达式2)

语句2

等等

else if (表达式n)

语句n

else

语句 n+1

功能为

依次计算并判断表达式i

为非0时

执行后面的语句

都为0时

执行语句n+1

无论执行完哪个语句分支

都转到后续语句

if 多分支语句流程图

如图4.3所示

下面

请完成课堂练习

输入商品价格

求打折后的价格

流程图如下所示

请根据流程图

写出程序

选择结构还可以嵌套

在if语句中又包含

一个或多个if语句

称为if语句的嵌套

一般形式为

if( )

if( ) 语句1

else 语句2

else

if( ) 语句3

else 语句4

流程图如图所示

if语句的嵌套也可以形如

if ()

{ if () 语句1 }

else 语句2

下面我们来看一个例子

有一个分段函数

y =-1

0

1

编一个程序

输入一个x的值

要求输出相应的y值

解题思路

用if语句检查x的值

根据x的值

决定赋予y的值

由于y的可能值

不是两个而是三个

因此

不可能只用一个简单的

(无内嵌if)的if语句来实现

实现方法有以下几种

方法1

第一

先用3个独立的if语句处理

输入x

若 x < 0,

则y =-1

若 x = 0,

则y = 0

若 x > 0,

则y = 1

最后输出x和y

方法2

可以用一个嵌套的if语句处理

输入x

若x < 0,

则y = -1

否则

若 x = 0,

则y = 0

否则

y = 1

输出x和y

我们提倡内嵌的if放在else当中

方法3

用一个嵌套的if语句处理

输入x

若x >=0,

若x>0,

则y = 1

否则

y=0

否则

y =-1

输出x和y

好了

同学们

C语言的关系

逻辑运算符

和if语句

我们就学到这儿

下节课再见

C Programming课程列表:

Chapter 1 Introduction

-1.1 The development and characteristics of C language

--1.The development and characteristics of C language .mp4

--1.1 Self test questions

-1.2 A simple C Language program

--1.2 A simple C Language program.mp4

--Discussion unit

--【Source program】 Example 1.1 output a line of text Hello, world! "

--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers

--1.2 Self test questions

-1.3 Program, Programming Language and C Program Running Steps

--1.3 Program, Programming Language and C Program Running Steps.mp4

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--2.1 Self test questions

-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming

--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-3.1 Simple Structure and Identifier of C Language Program

--3.1 Simple Structure and Identifier of C Language Program.mp4

--【Source program】 Example 3.1 input two integers and output the sum of two numbers.

--【Source program】 Example 3.2 input two integers and output the average value.

--【Source program】 Example 3.3 output the value of the character variable.

--3.1 Self test questions

-3.2 Constants, Variables and Assignments

--3.2 Constants, Variables and Assignments.mp4

--Discussion unit

--3.2 Self test questions

-3.3 Arithmetic, assignment, increment and decrement operators

--3.3 Arithmetic, assignment, increment and decrement operators.mp4

--3.3 Self test questions

-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--【Source program】 example of sizeof

--3.4 Self test questions

-3.5 Input and Output Examples and Character Input and Output

--3.5 Input and Output Examples and Character Input and Output.mp4

--【Source program】 Example. Find the root of quadratic equation of one variable.

--【Source program】 Example 1. Successively output three characters of BOY.

--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen

--3.5 Self test questions

-3.6 Formatted output printf function

--3.6 Formatted output printf function.mp4

--【Source program】 Example 3.4 output of integer data.

--【Source program】 Example 3.5 real data output.

--【Source program】 Example 3.6 character data output.

--【Source program】 Example 3.7 uses %s to output a string.

--3.6 Self test questions

-3.7 Formatted input scanf function

--3.7 Formatted input scanf function.mp4

--Discussion unit

--【Source program】 Example 3.8 Input and output integer data

--【Source program】Example 3.9 Input and output single precision and double precision real data.

--【Source program】Example 3.10 input and output character data.

--【Source program】Example 3.11 input and output string.

--3.7 Self test questions

-3.8 Basic Data Types of C Language

--3.8 Basic Data Types of C Language.mp4

--3.8 Self test questions

-Chapter 3 Self test questions

-Operator and expression self test questions

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【source example】Example 4.1 Find the root of the quadratic equation of one variable.

--【Source program】Example 4.2 Input two real numbers and output them from small to large.

--4.1 Self test questions

-4.2 Switch Statement

--4.2 Switch Statement .mp4

--Discussion unit

--【Source program】Example 4.3 uses switch statement to implement simple menu program

--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.

--4.2 Self test questions

-4.3 Examples of Selection Structural Programs

--4.3 Examples of Selection Structural Programs.mp4

--【Source program】 Example 4.5 determines whether a year is a leap year.

--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.

--【Source program】Example 4.7 calculate the transportation cost for the user.

--4.3 Self test questions

-Chapter 4 Self test questions

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100

--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100

--5.1 Self test questions

-5.2 For Statement

--5.2 for statement.mp4

--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100

--5.2 Self test questions

-5.3 Change the State of Loop Execution and Nested Loop

--5.3 Change the state of loop execution and nested loop .mp4

--【Source program】example 5.4 collecting charity donations

--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.

--【Source program】multiplication table

--【Source program】example 5.6 Output the following 4*5 matrix.

--5.3 Self test questions

-5.4 Loop Structure Program example 1

--5.4 Loop structure program example 1.mp4

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--5.4 Self test questions

-5.5 Loop Structure Program example 2

--5.5 Loop structure program example 2.mp4

--【Source program】example 5. Output the following figure.

--【Source program】example6. The problem of 100 chickens and 100 coins

--【Source program】example 7. Find the approximate value of PI

--5.5 Self test questions

-Chapter 5 Self test questions

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--6.1 Self test questions

-6.2 One-Dimensional Array Programming

--6.2 One-Dimensional Array Programming.mp4

--【Source program】example 1. Fibonacci sequence

--【Source program】example 2. Find the maximum value and the minimum value.

--【Source program】example 3 exchanging array elements in reverse order.

--【Source program】example 4. Bubble sorting.

--6.2 Self test questions

-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays

--6.3 Definition, Reference of Two-Dimensional Arrays.mp4

--6.3 Self test questions

-6.4 Two-Dimensional Array Programming

--6.4 Two-Dimensional Array Programming.mp4

--【Source program】example 1. Get the average score of each subject.

--【Source program】Example 2. The elements of row and column of a 2D array are interchanged

--6.4 Self test questions

-6.5 Definition, initialization and input and output of character arrays

--6.5 Definition, initialization and input and output of character arrays .mp4

--Discussion unit

--【Source program】Example 6.6 Output a known string.

--【Source program】Example 6.7 Output a diamond.

--【Source program】Example 6.10 Sorting of strings.

--6.5 Self test questions

-6.6 String Processing Function

--6.6 String Processing Function.mp4

--6.6 Self test questions

-6.7 Character Array Programming

--6.7 Character Array Programming.mp4

--6.7 Self test questions

-Chapter 6 Self test questions

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--7.1 Self test questions

-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value

--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4

--Discussion unit

--【Source program】Example 7.2

--7.2 Self test questions

-7.3 Declarations of called functions and nested calls to functions

--7.3 Declarations of called functions and nested calls to functions .mp4

--【Source program】Example 7.4 use a function to find the sum of t

--7.3 Self test questions

-7.4 Recursive call to function

--7.4 Recursive call to function.mp4

--【Source program】Example 7.6. How old is the fifth student?

--【Source program】Example 7.7 Use recursion method to find n!

--7.4 Self test questions

-7.5 Array as function parameter (1)

--7.5 Array as function parameter (1).mp4

--Discussion unit

--【Source program】Example 7.10

--7.5 Self test questions

-7.6 Array as function parameter (2)

--7.6 Array as function parameter (2).mp4

--【Source program】Selection sorting.

--【Source program】Example 7.13 find the maximum of a 3×4 matrix.

--7.6 Self test questions

-7.7 Local and global variables, internal and external functions

--7.7 Local and global variables, internal and external functions.mp4

--【Source program】Example 7.14

--7.7 Self test questions

-7.8 The Survival Period of Variables and the Storage Mode of Local Variables

--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4

--【Source program】Example 7.17

--7.8 Self test questions

-7.9 Storage Categories of Global Variables

--7.9 Storage Categories of Global Variables.mp4

--7.9 Self test questions

-Chapter 7 Self test questions

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【Source program】Example 8.3. Exchange two data with function call.

--8.2 Self test questions

-8.3 Pointer of array element, operation of pointer and reference of array element by pointer

--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4

--【Source program】Point to array elements with pointer variables.

--8.3 Self test questions

-8.4 Using Array Name as Function Parameter

--8.4 Using Array Name as Function Parameter.mp4

--【Source program】Store array elements in reverse order.

--8.4 Self test questions

-8.5 Reference to multidimensional arrays by pointers

--8.5 Reference to multidimensional arrays by pointers.mp4

--【Source program】Pointer variable pointing to one-dimensional array.

--8.5 Self test questions

-8.6 Referencing strings through pointers

--8.6 Referencing strings through pointers.mp4

--【Source program】Reference string by pointer.

--8.6 Self test questions

-8.7 Character pointer as function parameter

--8.7 Character pointer as function parameter.mp4

--【Source program】Character pointer as function parameter.

--8.7 Self test questions

-8.8 Pointer pointing to function

--8.8 Pointer pointing to function.mp4

--【Source program】Pointer variable pointing to the function.

--8.8 Self test questions

-8.9 Functions that return pointer values

--8.9 Functions that return pointer values.mp4

--【Source program】Intercept substring.

--8.9 Self test questions

-8.10 Pointer arrays and multiple pointers

--8.10 Pointer arrays and multiple pointers.mp4

--【Source program】Sorting of strings.

--8.10 Self test questions

-8.11 Dynamic memory allocation and pointer variables pointing to it

--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-9.1 Define and use structural variables

--9.1 Define and use structural variables.mp4

--【Source program】Structure variable

--9.1 Self test questions

-9.2 Using structure arrays

--9.2 Using structure arrays.mp4

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

4.1 Relational operators, logical operators, and if statements.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。