Instance explains the branch structure of the C language

zhaozj2021-02-17  75

Since it is an example analysis, there is of course an example. Ok, give a test questions first: Enter a percentage of scores, requiring the output of this grade to pass.

Obviously, the branch structure should be used to print in both cases. Given the solution procedure as follows:

/ * Example 1 * / # INCLUDE VOID Main () {INT N; Printf ("please input the score:"); scanf ("% d", & n); if (n <60) {printf ("fail / n ");} else {printf (" 及 了 / N ");}}

Such a program is very simple, but it demonstrates the most basic usage of the IF branch structure:

IF (condition) {statement 1} else {statement 2}

When the condition is true when the condition is true, the condition is executed in the hypothesis of the statement 2. In the C language, all non-0 values ​​are true, 0 indicate false. Braces in the IF structure are not necessary. When you only contain one statement in your braces, you can omitted the braces. As the IF statement in Example 1, it can be rewritten as:

IF (a <60) Printf ("fail / N"); Else Printf ("及 了 / N");

In this way, the program seems to be simpler. But if you need to add a statement in the future, you may cause an error because of the addition of braces. Therefore, it is recommended to use a write method that does not illegally parenthery.

The basic IF statement can also simplify - omitted the ELSE branch. As in Example 1. We can also write:

/ * Example 2 * / # include void main () {INT N; Printf ("please input the score:"); scanf ("% d", & n); if (n <60) {printf ("fail / n "); Return;} printf (" 及 了 / N ");}

Example 2 describes: If the score is less than 60, the "fail" is printed, and the main () function is ended. If the score is not less than 60 points, all statements in the IF branch do not perform, and printf ("," and "," and grid "are executed directly.

Think about it, if you don't use Return in the IF statement; how to force the main () function, what will it? As a result, in the case where the score is less than 60 minutes, it will be printed "fail" and "and". " The reason is obvious: because the printed "and grid" statement is not in the ELSE branch, it is not in the IF structure, no matter what the situation is executed.

The IF statement can become more complex - nested, that is, the IF structure in the IF structure. As in the topic, we plus a little fault tolerance, first judge whether the input score is between 0 and 100:

#include void main () {INT N; Printf ("please input the score:"); scanf ("% d", & n); if (n> = 0 && n <= 100) {IF (n <60) {Printf ("fail / N"); return;} printf ("and grid / n");} else {printf ("Input Different Value Error!");}}}} Example 3 Description: If n is 0 to 100 Between, then make the error message, otherwise print the error message. In this way, maybe it is a bit confused: Which IF is this ELSE? Behind each if or else, a group of statements should be hosted by a statement or from a pair of braces. Moreover, you can only follow one statement or a statement that is enclosed by a large bracket. It is easy to judge by the relationship between braces. Example 3 is a nested IF structure in the IF branch, and the IF structure can also be nested in the ELSE branch. Please see example 4: / * example 4 * / # include void main () {int N; printf ("please input the score : "); Scanf ("% D ", & n); if (n <0 || n> 100) {Printf (" input score error! ");} Else {ix (n <60) {printf (" Leg / N "); Return;} Printf ("} 了 / N ");}} Of course, if branch and else branch may nested the IF structure in a more complicated program, I hope the reader can give an Anti-three, here Not much to say. Now let's change the experimental entity, not requiring input whether it is comparable, but requires the A-E level corresponding to the percentage score.

How to do? Yes, use a nesting - multiple nested: / * Example 5 * / # include void main (int N; printf ("please input the score:"); scanf ("% d", & n ); If (n <0 || n> 100) {printf ("input score error!");} Else {if (n <60) {Printf ("e / n");} else {if (n <70) {Printf ("D / N");} else {IF (n <80) {Printf ("C / N");} else {IF (n <90) {Printf ("b / n") Else {Printf ("a / n");}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

God! I feel dizzy. Can you be simple? Of course, do you know the Elseif statement? Sorry, C language is not. However, the C language can achieve a structure similar to the elseif statement, because - Each IF structure is just a statement, do the else can pick a statement? Look at me: / * Example 6 * / # include void main () {INT N; Printf ("please input the score:"); scanf ("% d", & n); if (n <0 || n> 100) {Printf ("input score error!");} Else {IF (n <60) {Printf ("e / n");} else if (n <70) {printf ("d / n") } Else if (n <80) {Printf ("c / n");} else if (n <90) {printf ("b / n");} else {printf ("a / n");} }}, Now it is more clear, but it is really troublesome with so many Else if. Why not use Switch? The C language Switch branch is often the Case branch, which is also a multi-branch. The IF structure can only have two branches, while the Switch can have the most ... countless branches! Let's take a look at how it is: / * Example 7 * / # include void main () {int N; Printf ("please input the score:"; scanf ("% d", & n); if (n < 0 || n> 100) {Printf ("Input Differential Error!");} Switch (N / 10) {Case 9: Printf ("A / N"); Break; Case 8: Printf ("B / N "); Break; Case 7: Printf (" C / N "); Break; Case 6: Printf (" D / N "); Break; Default: IF (n == 100) {Printf (" A / N " } Else {printf ("e / n");}}} is not to understand why N / 10 sentence? This statement is to take N to remove the integer, abandon the remainder. The reason why this is because Case can only pick a constant (or constant), I can't use CASE N> 90 as in FoxBase. Then why do you want to use so many Break? Break means that the Switch statement is jumped out.

Each Case is actually just a label, just like using the goto statement to jump to the label, it will start from the position of the label, always executed. If you don't have Break, you try, haha, unless it is full, it is E. Oh, there is DEFAULT forgot to tell you. If all CASE conditions do not match, the part behind the default is executed. Of course, the DEFAULT branch is omitted, so, if you can't find a matching condition, you don't do anything. Interest! But actually used two IF branches, embarrassment. Since the Switch branch is multi-branch, it should be able to include all if of the IF branch! Try again: / * Example 8 * / # include void main () {int N; Printf ("please input the score:"); scanf ("% d", & n); switch (n / 10) {Case 10 : Case 9: Printf ("A / N"); Break; Case 8: Printf ("B / N"); Break; Case 7: Printf ("C / N"); Break; Case 6: Printf ("D / N "); Break; Case 5: Case 4: Case 3: Case 2: Case 1: Case 0: Printf (" E / N "); Break; Default: Printf (" Input Differential Errors! ");} } Nowadays, there are many years! However, what does two cases have been used continuously? Case is not just a label, but continuous use of two cases, but it is to let two labels represent the same location. This trick is very easy! However, from Case 5 to Case 0, write such a long, if I use an IF branch, I can write some code less. So, don't use IF when using Switch, you have to check the situation, let your code simpler! The Switch structure is also a multiple nested - of course, after a certain case, I don't have to give it, how can people learn how can the program? ! To put it bluntly, the branch structure of the C language is also two kinds, but as long as you use it flexible, this branch can be universal! Single nine sword, focus on swords, not in the sword ... "Hey!" What is thinking, be played! = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - // /

__________ / lb / ___ Outinn

/ _ [] _ ​​/ ____ / /

/ _________ / | () | / __ /

Http://outinn.yeah.net/

| ____ / - | __ | - / | Welcome to Visit Outinn!

| __ | == | ___ | || | __ |

- = - = - = - | _ || _ | = - FANCY,

Outinn@china.com

转载请注明原文地址:https://www.9cbs.com/read-31334.html

New Post(0)