当前课程知识点:C Programming >  Chapter 5 Loop Structure >  5.3 Change the State of Loop Execution and Nested Loop >  5.3 Change the state of loop execution and nested loop .mp4

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

5.3 Change the state of loop execution and nested loop .mp4在线视频

下一节:【Source program】example 5.4 collecting charity donations

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

5.3 Change the state of loop execution and nested loop .mp4课程教案、知识点、字幕

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

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

今天我们讲解如何改变循环执行的状态

在循环中可以用break语句提前终止循环

在第4章的switch结构中

我们已经接触过break语句的使用

break语句可以使执行语句的流程跳出switch结构

继续执行switch选择结构下面的语句

同样的,break语句还可以用来从

循环结构体内跳出循环结构,提前结束循环

去执行循环结构下面的语句,break语句的格式

break;break专用于循环体和switch结构中

不能单独使用

利用break语句能够强迫终止

本层循环,转到后续语句执行

下面来看例5.4 在全系1000学生中,征集慈善募捐

当总数达到10万元时就结束,统计此时捐款的人数

以及平均每人捐款的数目

编程思路如下

循环次数不确定,但最多循环1000次

在循环体中累计捐款总数

用if语句检查是否达到10万元

如果达到就不再继续执行循环,终止累加

计算人均捐款数

变量amount,用来存放捐款数

变量total,用来存放累加后的总捐款数

变量aver,用来存放人均捐款数

定义符号常量SUM代表100000

程序如下:#include

#define SUM 100000

//指定符号常量SUM代表100000

int main()

{ float amount,aver,total;

int i; //定义相关变量

for (i=1,total=0;i<=1000;i++)

{ printf("please enter amount:");

scanf("%f",&amount); //输入金额

total= total+amount; //计算累加和

if (total>=SUM) break; }

aver=total / i ;

输出i和aver的值

i<=1000表示应该执行1000次

if (total>=SUM) break; 表示达到10万,提前结束循环

aver=total / i ; 这里的i表示实际捐款人数

程序运行结果如图所示

if (total>=SUM) break; 表示如果总计

超过SUM (10万元),则退出循环

break只能用于循环语句和switch语句之中

而不能单独使用

下面介绍continue语句

有时并不希望终止整个循环的操作

而只希望提前结束本次循环,而接着

执行下次循环。这时可以用continue语句

执行下次循环。这时可以用continue语句

continue功能:中断循环体的

本次执行(即跳过循环体中尚未执行的语句)

立即开始执行下一次循环。

while, do-while, for语句的

continue语句的流程图如下所示。

下面来看例5.5 要求输出100~200

之间的不能被3整除的数

编程思路:

对100到200之间的每一个整数进行检查

如果不能被3整除,输出,否则不输出

无论是否输出此数,都要接着检查下一个数(直到200为止)。

流程图如下:

n首先赋值为100,判断n<=20是否成立?

是,再判断n能被3整除吗?

当n能被3带整除时,流程跳过printf函数语句

结束本次循环,然后进行循环变量n的增值(n++)

只要n<=200,就会接着执行下一次循环

如果n不能被3整除,就不会执行continue语句

而执行printf函数语句,输出不能被3整除的整数

for语句如下:

for(n=100;n<=200;n++)

{ if (n%3==0) continue;printf("%d ",n);}

程序运行结果如图所示,输出100-200

之间不能被3整除的所有数

break语句和continue语句的区别在于

continue语句只结束本次循环

而不是终止整个循环的执行

break语句结束整个循环过程

不再判断执行循环的条件是否成立

循环还可以嵌套使用

如果循环语句的循环体内又包含了

另一条循环语句,则称为循环的嵌套

例如: 以下程序输出9×9乘法表

main( )

{ int i, j;

for ( i=1; i<10; i++ ) //外循环控制行号

for ( j=1; j<=i; j++ ) //内循环控制列号

且列号<=行号

printf ((j==i)?"%4d ":"%4d",i*j); //输出每一行i*j的值}

程序运行结果如图所示。

while、do-while、for循环语句可以并列

也可以相互嵌套,但要层次清楚,不能出现交叉

多重循环程序执行时,外层循环每执行一次

内层循环都需要循环执行多次。

例如:for(a=1;a<=10;a++)

{ for (b=0;b<=5;b++)

外循环执行了10次,内循环执行6次

循环正常结束时,内循环执行了10×6=60次

下面来看例5.6 输出以下4*5的矩阵。

解题思路:

可以用循环的嵌套来处理此问题

用外循环来输出一行数据

用内循环来输出一列数据(行号乘以列号)

按矩阵的格式(每行五个数据)程序如下:

int main()

{ int i,j,n=0;

for (i=1;i<=4;i++)

for (j=1;j<=5;j++,n++) //用n累计数据的个数

{ if (n%5==0) printf (“ ”); //控制一行内输出5个数据

printf ("%d\t",i*j);}

本例中使用双重for循环输出多行多列的数据

for (i=1;i<=4;i++) 控制输出4行

for (j=1;j<=5;j++,n++) 控制每行中输出5个数据

i=1时,j由1变到5,i*j的值是1,2,3,4,5

i=2时,j也由1变到5,i*j的值是2,4,6,8,10

程序运行结果如下

假如在以上程序的基础上,作一些改动

在内循环体中增加一个if语句,if (i==3 && j==1) break;

实际输出结果如下,第3行空白

即不输出第3行的5个数据

如果把上面的break语句改为continue语句

即if (i==3 && j==1) continue; 输出如下:

好了,同学们,如何改变循环执行的状态

我们就学习到这儿,下节课再见

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

5.3 Change the state of loop execution and nested loop .mp4笔记与讨论

也许你还感兴趣的课程:

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