Continue or Break? An article tells you

Table of contents

introduction

Loop structure in C language:

C,or  B ?

 The use of break in branch statements

Application examples

Why is this happening?


introduction

In branch statements, if...else is commonly used to write branch statements to make judgments and choices under various conditions;

The following is a program written in C++ to determine leap years in ordinary years.

#include <iostream>
using namespace std;
int main()
{
    int year = 0; //这是北京大学李戈教授在授课时编写的
    cin >> year;
    if (year %4 == 0) //判断方法:能被4整除,但不能被100整除
    {
        if (year % 100 == 0)//或能被100整除,又能被400整除,即为闰年。
        {
                if (year % 400 == 0)
                        cout <<"Y";
                else
                        cout << "N";
        }
        else
                cout << "Y";
    }
    else
            cout <<"N";
    return 0;
}

 However, when multiple/repeated judgments are required to obtain results, it is obviously impractical to repeatedly use the if statement of a single judgment, and a loop statement must be used to solve the problem:

Loop structure in C language:

Taking C++ as an example, loops can be roughly divided into four categories:

  • for statement loop
  • while statement loop
  • do...while statement loop
  • goto statement and if statement form a loop

However, an infinite loop that does not terminate (or abort) is meaningless in most cases, so there is a need to "jump out of the loop" and "end the loop", from which "continue" and "break" were born. (*╹▽╹*)

for(;;) //只打两个分号,便是最简单的一个无条件死循环;也可替换为while(true)
{
  ****** //这一行想填啥填啥  
}

C,or  B ?

continue and break are both steering control statements and can be used in for, while, and do...while ;

But break is mostly used to jump out of the inner loop , that is, to stop the current loop and execute the next statement ;

Continue is used to end this cycle and determine whether the second cycle can be carried out .

If you really can’t remember, you can look at the definitions of these two words: (*/ω\*)

break /breɪk/ v. break; interrupt; end, end

continue v. (make) continue, (make) continue, (after interruption) continue, start again

 (The left side of the flow chart is break and the right side is continue)

 

 The use of break in branch statements

In fact, not only loop statements, but also branch statements sometimes require break.

A typical one is the swi tch statement: (←Guess why it is colored like this?)

Application examples
#include<iostream>
using namespace std;
int main()
{
    char grade = 'a';
    cin>>grade;
    switch (grade)
    {
    case 'a': cout <<"85~100"<<endl;
    case 'b': cout <<"70~84"<<endl;
    case 'c': cout <<"60~69"<<endl;
    case 'd': cout <<"<60"<< endl;
    default: cout << "error" <<endl;
    }
    return 0;
}

The above is an academic grade conversion program written with a switch statement. It can convert the five academic grades A, B, C, D, and E.

Convert it into the corresponding score range ; (eg: Enter A, it will display 85~100, which means 85-100 points can be rated as A level.)

However, the output is the following:

a //这是我们输入的字符a
85~100//下方是输出结果
70~84
60~69
<60
error

That's right, although a is input , the output end reads all abcde , even the error is not missed...

why?

Because there is no break.

If the value of the constant expression after a case in the switch is equal to the value of the expression, then the case and all subsequent cases will be executed, including default (if it follows it). Therefore, break should be added after each case to jump out of the branch.

The modified procedure is as follows:

#include<iostream>
using namespace std;
int main()
{
    char grade = 'a';
    cin>>grade;
    switch (grade)
    {
    case 'a': cout <<"85~100"<<endl;break;
    case 'b': cout <<"70~84"<<endl;break;
    case 'c': cout <<"60~69"<<endl;break;
    case 'd': cout <<"<60"<< endl;break;
    default: cout << "error" <<endl;
    }
    return 0;
}

Try typing again:

a //输入a
85~100
d //输入d
<60
f //输入f
error
''互三连,互关注''  //这句话是我瞎打的,但是话是真的,主打一个话糙理不糙(手动狗头)
error//没错,也会输出error。

This will work fine.

Finished spreading flowers! ✿ヾ(◍°∇°◍)ノ゙

Guess you like

Origin blog.csdn.net/m0_54909552/article/details/131725754