Branch statement and loop statement (the most important statement in C language)

I. Introduction

Presumably everyone must have heard that C language is a structured programming language, and it is very important. Just like the title, the most important statements in C language are branch statements and loop statements. But why do you say that? It is because more than 90% of the things in life can be described by them. Branch statements and loop statements have three structural orders: sequence structure, selection structure, and loop structure.

1. Sequential structure: As the name suggests, it is to follow the things themselves step by step, from beginning to end.

2. Selection structure: it is to selectively do things that meet the requirements, just like the postgraduate entrance examination or work after graduating from senior year

3. Loop structure: It is to give you a condition and then continue to do this thing repeatedly when the condition is met until the condition is broken.

Everything in life can use one or more of the above statements to express and realize your own ideas. First, let's understand what is a statement in C language

C statements can be divided into the following five categories:

1. Expression statement

2. Function call statement

3. Control Statements

4. Compound statements

5. Empty statement

Branch statements and loop statements are control statements used to control the execution flow of the program to realize various structural modes of the program (C language supports three structures: sequential structure, selection structure, and loop structure), which are composed of specific statement definers , C language has nine control statements. Can be divided into the following three categories:

1. Conditional judgment statements are also called branch statements: if statement, switch statement.

2. Loop execution statement: do while statement, while statement, for statement.

3. Turn to statement: break statement, goto statement, continue statement, return statement.

Next, let's talk about their use in C language.

2. Branch statement (choice structure)

if statement

First, let's understand the grammatical structure of if

If the result of the expression is true, the statement executes

* In C language, 0 means false, non-zero means true (this is very important)

The single branch is as follows

int main()
{
    if(条件表达式)
    {
        条件满足执行的语句;
    }
    else
    {
        条件不满足执行的表达式
    }
    return 0;
}

In addition to single branch, it can also be multi-branch

int main()
{
    if(条件表达式1)
    {
        条1件满足执行的语句;
    }
    else if(条件表达式2)
    {
        条件2满足执行的表达式;
    }
    else if(条件表达式3)
    {
        条件3满足执行的表达式;
    }
    ......
    else (条件表达式)
    {
        条件都不满足时执行的表达式;
    }
    return 0;
}

The pair of { } here is a code block, and proper use of {} can make the logic of the code clearer.

switch statement

switch语句也是一种分支语句,常常用于多分支的情况。

比如:

输入1,输出星期一

输入2,输出星期二

输入3,输出星期三

输入4,输出星期四

输入5,输出星期五

输入6,输出星期六

输入7,输出星期日

那我没写成 if...else if ...else if 的形式太复杂,那我们就得有不一样的语法形式。

这就是switch 语句

switch(整型表达式)
{
    语句项;
}

语句项是什么呢?

//是一些case语句:
//如下:
case 整形常量表达式:
    语句;

在switch语句中的 break

在switch语句中,我们没办法直接实现分支,搭配break使用才能实现真正的分支。

比如:

#include <stdio.h>
int main()
{
    int day = 0;
    switch(day)
    {
        case 1:
            printf("星期一\n");
            break;
        case 2:
            printf("星期二\n");
            break;
        case 3:
            printf("星期三\n");
            break;
        case 4:
            printf("星期四\n");
            break;
        case 5:
            printf("星期五\n");
            break;
        case 6:
            printf("星期六\n");
            break;
        case 7:
            printf("星期天\n");
            break;
    }
    return 0;
}

default子句

如果表达的值与所有的case标签的值都不匹配怎么办?

其实也没什么,结构就是所有的语句都被跳过而已。

程序并不会终止,也不会报错,因为这种情况在C中并不认为是个错误。

但是,如果你并不想忽略不匹配所有标签的表达式的值时该怎么办呢?

你可以在语句列表中增加一条default子句,把下面的标签

default:

写在任何一个 case 标签可以出现的位置。当 switch 表达式的值并不匹配所有 case 标签的值时,这个 default 子句后面的语句就会执行。所以,每个switch语句中只能出现一条default子句。但是它可以出现在语句列表的任何位置,而且语句流会像执行一个case标签一样执行default子句。

在每个 switch 语句中都放一条default子句是个好习惯,甚至可以在后边再加一个 break 。

三、循环语句

while

while循环

由于我们发现生活中很多的实际的例子是:同一件事情我们需要完成很多次。

那我们怎么做呢?

C语言中给我们引入了: while 语句,可以实现循环。

//while 语法结构
while(表达式)
    循环语句;

while语句执行的流程:

比如我们实现:

在屏幕上打印1-10的数字。

#include <stdio.h>
int main()
{
    int i = 1;
    while(i<=10)
    {
        printf("%d ", i);
        i = i+1;
    }
    return 0;
}
while语句中的break和continue
break

其实在循环中只要遇到break,就停止后期的所有的循环,直接终止循环。

所以:while中的break是用于永久终止循环的。

break 代码实例

#include <stdio.h>
int main()
{
    int i = 1;
    while(i<=10)
    {
        if(i == 5)
            break;
        printf("%d ", i);
        i = i+1;
    }
    return 0;
}
continue

continue在while循环中的作用就是:

continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再执行,

而是直接跳转到while语句的判断部分。进行下一次循环的入口判断

#include <stdio.h>
int main()
{
    int i = 1;
    while(i<=10)
    {
        i = i+1;
        if(i == 5)
            continue;
        printf("%d ", i);
    }
    return 0;
}

for

首先来看看for循环的语法:

for(表达式1; 表达式2; 表达式3)
    循环语句;

表达式1

表达式1为初始化部分,用于初始化循环变量的。

表达式2

表达式2为条件判断部分,用于判断循环时候终止。

表达式3

表达式3为调整部分,用于循环条件的调整。

使用for循环 在屏幕上打印1-10的数字。

#include <stdio.h>
int main()
{
    int i = 0;
    //for(i=1/*初始化*/; i<=10/*判断部分*/; i++/*调整部分*/)
    for(i=1; i<=10; i++)
    {
        printf("%d ", i);
    }
    return 0;
}

现在我们对比一下for循环和while循环

int i = 0;
//实现相同的功能,使用while
i=1;//初始化部分
while(i<=10)//判断部分
{
    printf("hehe\n");
    i = i+1;//调整部分
}
//实现相同的功能,使用while
for(i=1; i<=10; i++)
{
    printf("hehe\n");
}

可以发现在while循环中依然存在循环的三个必须条件,但是由于风格的问题使得三个部分很可能偏离较远,这样查找修改就不够集中和方便。所以,for循环的风格更胜一筹;for循环使用的频率也最高。

break和continue在for循环中

我们发现在for循环中也可以出现break和continue,他们的意义和在while循环中是一样的。

但是还是有些差异:

//代码1
#include <stdio.h>
int main()
{
    int i = 0;
    for(i=1; i<=10; i++)
    {
        if(i == 5)
        break;
        printf("%d ",i);
    }
    return 0;
}
//代码2
#include <stdio.h>
int main()
{
    int i = 0;
    for(i=1; i<=10; i++)
    {
        if(i == 5)
        continue;
        printf("%d ",i);
    }
    return 0;
}

for语句的循环控制变量

1. 不可在for 循环体内修改循环变量,防止 for 循环失去控制。

2. 建议for语句的循环控制变量的取值采用“前闭后开区间”写法

int i = 0;
//前闭后开的写法
for(i=0; i<10; i++)
{}
//两边都是闭区间
for(i=0; i<=9; i++)
{}

do while

do...while()循环

do语句的语法:

do
循环语句;
while(表达式);

do语句的特点

循环至少执行一次,使用的场景有限,所以不是经常使用

#include <stdio.h>
int main()
{
    int i = 1;
    do
    {
        printf("%d ", i);
        i=i+1;
    }while(i<=10);
    return 0;
}

do while循环中的break和continue

#include <stdio.h>
int main()
{
    int i = 1;
    do
    {
        if(5 == i)
        break;
        printf("%d ", i);
        i=i+1;
    }while(i<=10);
    return 0;
}
#include <stdio.h>
int main()
{
    int i = 1;
    do
    {
        if(5 == i)
        continue;
        printf("%d ", i);
        i=i+1;
    }while(i<=10);
    return 0;
}

四、总结

分支语句和循环语句也就这些基本知识了,希望大家一键三连。不要忘了关注!关注!关注!

不支持下次一定【doge】

Guess you like

Origin blog.csdn.net/m0_75215937/article/details/129367744