Please take the time machine of switch, if and while

Source: Internet
Author: User
Tags case statement

I. Switch, if and while time machine

Do you know what a choice is? What is judgment, or do you know what a loop is? For example, a car and a bike, what would you choose? So will you judge right and wrong? Rain, not always in circulation! These are choices, judgments, and loops. So next, I'll take a look at the fantasy world of switch, if and while. Hope to meet the requirements of everyone, but also to answer your questions, say no more we come together to see it!

1.switch...case ...

How much do you know about switch statements? Know the golden Partner of it! Yes, case, of course, you might say, there is a break! Yes, you're right, but you want to know why I'm saying this, and then I'm going to give you one by one of your way. First look at the program, switch after the expression 1, can not be real, can only be a constant expression or judgment statement (not recommended) with a judgment statement, because the judgment statement only 0 and 1 points, no meaning.

switch (expression 1) {case-expression 2:break;case expression 3:break;                                    ..... default:break;}

The following code will be executed with an error, because the expression after switch must be a constant or constant expression.

Switch (1.1) {case 1:break;case 2:break; ...; Default:break;}

  

And then we'll describe the case. The expression followed by the case must also be an integer constant and cannot be a real number. Of course, it can be a character, because when the case is followed by a character, the program will automatically turn to the ASCII value corresponding to that character when it executes, so there is no problem with the character. The following code in the execution of the time will certainly be wrong, so we must remember, can not be used so.

Switch (1) {case 1.1:break;case 2.2:break; Default:break;}

So then there's the two dispensable brothers, break and default, behind switch and case. Why do you say that, first of all, say break, because the expression of break is to terminate or jump out of the meaning, where we call it jump. What's the difference between having and not? The 1th is that when the program executes behind a case statement, if there is no break, it will execute the next instance statement again, knowing that it will stop if it encounters the next break statement or default. Here I'll give you a word to listen to. Is "penetrating", that is, case penetrating, no break,case is going to continue to execute. So, when you write case statements, you must remember which places to add break, which can be omitted to write.

Switch (1) {case 1:case 2:break; Default:break;}

Again, the default bar, the sentence can also be omitted, because if you want to wait until the completion of the program is not selected to the appropriate statement, such a program is not meant to write. The following code is completely correct, but you have to look at a person's liking. OK switch statement is finished, if there is anything missing, please give me a good news, I will accept it humbly.

Switch (1) {case 1:case 2:break; ...;}
2.if ... And if's family

Speaking of If, you will certainly say, "if" people say and think is also true, that is, if the meaning, but also added a judgment statement after the. It also constitutes the if...else and other sentences that we have been using. Let's look at how large the If family is.

if (expression 1) {code block 1;}if (expression 2) {code block 2;} ...

  

The code written above is a small if code, which is also judged by the expression. For example, if the expression 1 is set up, execute code block 1, anyway continue to judge, know to find satisfactory so far. It is necessary for us to say something about the expression. The expression can only be judged at 0 or 1, that is, if 1 executes the code block 1 and vice versa. And then talk to you about the next few statements about if.

if (expression 1) {code block 1;}else if (expression 2) {code block 2;}

 

if (expression 1) {code block 1;}else if (expression 2) {code block 2;}else{code block 3;} ...;

Here to specifically explain, the above two code, as long as there is a condition, then the following code is no longer executed, this can also be described as the if sentence code of the enhanced version of it, must be particularly remembered. So if, this statement is finished, if there is to add, please give me a message.

3.while and Do...while's revolving mission

If you want to do something all the time, then you have to use while or do...while. These two statements are basically used on a dead loop to get someone, or something, to keep doing something until you give it a statement to end it. Let's take a look at the specific implementation of the code.

while (expression) {loop statement block;}

If you want to implement a dead loop, that is, to operate on something, the simplest dead loop of the general expression is to have an expression value of 1 so that it can reach the dead loop. Of course, if you want to end the cycle of death, then you have to use the break in the case statement, which is also the simplest to jump out of the dead loop. Then let's say Do...while, what's the difference between it and while? The first is the while statement if the condition is not satisfied will not execute, but Do...while is not. There is a good saying, while the while is not necessarily executed, but Do...while will certainly execute. Why is it? Let's take a look at Do...while's code to implement it.

 

do{  Statement 1;  statement 2; ...  } while (expression);

Do...while, the statement is exactly what is implemented, we first look at the code. Do...while is implemented from the Do after statement, which is what we often say, the code is executed from the top to the bottom when the computer is compiled, so Do...while executes the code inside the curly braces first, then executes to the end, that is, the expression after the while. At this time the computer in the judgment, whether there is the need to carry out, if you want to execute, you have to return to do the execution, and then know that the condition of the expression is not satisfied, then stopped. Often do...while and while the expression is 1, then there is a dead loop, this is the simplest of the dead loop, if you want to end the loop, with break is also possible, specific what way to jump out of the loop, I here is unclear.

Two. Nesting is the most beautiful scenery

Next, I'll say more about the nesting between them. If can be used with while and do...while, it is often used to help do...while and while jump out of a dead loop. Of course if can also be used in switch, often used to determine whether you can execute to the beginning of the switch statement. The overall code implementation can roughly be written like this.

while (expression) {switch (1) {case 1:case 2:break; Default:break;} if (expression 1) {code block 1;}}

This is usually the location of the placement. The while is used to determine whether to start executing the statement, and then the specific function you want to implement is written in the switch statement. The switch statement then uses break to determine whether to jump out of the switch statement, or continue to execute, until the next break in the jump or switch inside there is no satisfactory answer. And then, if you want to do the work over and over again, let the program go on, but if you want the program to run to a certain stage and then jump out of the loop under the conditions, then you have to use the IF statement to make a judgment.

Three. Concluding remarks

In short, the C language is extensive and profound, the above statement is only a part, there are a variety of nested use between them, I am not here to say, C language or a door can let us spend a lifetime of life to study the course, I hope we can learn from each other, I here is only a small part, there is no shortage of places, I sincerely hope that everyone can give me advice, let us improve together.!!

Please take the time machine of switch, if and while

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.