当前位置:网站首页>MOOC Weng Kai C language fourth week: further judgment and circulation: 3. Multiple branches 4. Examples of circulation 5. Common errors in judgment and circulation

MOOC Weng Kai C language fourth week: further judgment and circulation: 3. Multiple branches 4. Examples of circulation 5. Common errors in judgment and circulation

2022-07-28 07:03:00 W.934

One 、 Multiple branches

switch-case

switch( Control variable expression ){

case Constant :

    sentence

   ......

case Constant :

    sentence

   ......

default:

    sentence

   ......

}

Control expressions can only be integer results .( The expression can only be int Type of )

Constants can be constants , It can also be an expression for constant calculation .(C99 You can use )

break

case Can't stop the sentence from going on , and break Sure .

switch Statement can be seen as a calculation based jump , After calculating the value of the control expression , The program will jump to the matching case( Branch label ) It's about . The branch label just indicates switch Road signs at internal locations , After executing the last statement in the branch , If not at the back break, It will be executed in sequence to the following case In go to , Until I met a break, perhaps switch End .

Two 、 An example of a cycle

1. Cycle calculation

A little routine : Save the original value before calculating , It may be useful later .

2. Count cycle

A little routine : If you want to simulate running a large number of cycles , It can simulate fewer cycles , And then infer .

3. Calculate the average

4. Guess the game

random number :rand   

Every call rand() You get a random integer .

%100:x%n The result is [0,n-1] It's an integer of

5. Decomposition of integers

An integer is composed of 1 To multiple digits , How to decompose the numbers in each bit of an integer , Then calculate .

Do... To an integer %10 The operation of , You get its single digits ;

Do... To an integer /10 The operation of , Just remove its single digits ;

And then to ② What are the results %10, You get the original ten digits ;

And so on ......

3、 ... and 、 Common mistakes in judgment and circulation

1.if Common mistakes in sentences

1) Forget the braces    // Forever if and else Followed by {}, Even if there was only one sentence behind .

2)if Semicolon after    //if No semicolon after the statement ;

3) Misuse == and =   //if Only required () The value in is zero or non-zero ( Yes or no )//= It's assignment // Be careful warning Tips

4)else Use

Code style. :

stay if and else Then you must add braces to form a statement block ; The statements in braces are indented by a tab The location of .

原网站

版权声明
本文为[W.934]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207280520329351.html