"0 Basic Learning iOS development" "02-c language" 09-Process Control

Source: Internet
Author: User


directory of this document
    • Objective
    • First, sequential structure
    • Second, select structure 1-IF statement
    • III. Select structure 2-switch statement
    • IV. cyclic structure 1-while cycle
    • V. Cyclic structure 2-do while loop
    • VI. cyclic structure 3-for cycle
    • Vii. Break and Continue
Back to top Preface 1. Default Run Flow


By default, the process runs like this: After you run the program, each line of code in the program is executed in the written order. such as the following program



 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     
 6     printf("Hello-1\n");
 7     printf("Hello-2\n");
 8     printf("Hello-3\n");
 9     
10     return 0;
11 }


After the program runs, the 6th, 7, and 8 lines of statements are executed sequentially, and the output is:








2. Other Operating Procedures


However, many times, we do not want to follow the default running process to go, such as want to be in a certain condition to execute a piece of code, or do not execute. For example, this interface:






If the user clicks the Register button, we execute the "Jump to registration Screen" code, if the user clicked the login button, we will execute "jump to the Login Interface" code. If the user does not do anything, the preceding two-segment code is not executed. To realize this kind of function, it is necessary to learn how to control the procedure.





3. Process structure


In order to facilitate the operation of our control program, C language provides 3 kinds of process structure, different process structure can realize different running flow. These 3 process structures are:


    • Sequential structure: The default process structure. Each statement is executed in the written order.
    • Select structure: Judge a given condition and then decide which piece of code to execute based on the result of the decision.
    • Loop structure: Executes a piece of code repeatedly when a given condition is true.


Here are the 3 kinds of structure of the flowchart, a general preview can








Back to top one, sequential structure


The sequential structure is the simplest of the 3 structures and is the default process structure: The statements in the program are executed in the written order. The code snippets listed at the beginning of the article are sequential structures, which are not covered here.





Back to top second, select structure 1-IF statement


There are two ways to implement a select structure in C: The IF statement and the switch statement. Let's take a look at the use of the IF statement, and the IF statement has many kinds of forms.


1. Form 1


First look at the simplest form of the IF statement


1> Introduction

1 if (condition)
2 {
3 statement 1;
4 statement 2;
5 ....
6}


If the condition in the If Right parenthesis () is true, then the statement in line 2nd to 6th curly braces {} is executed, and the statement in curly braces {} is not executed if the condition is false. The IF is the keyword here.


2> Example

1 int a = 7;
2 
3 if (a)
4 {
5 printf ("Condition a holds \ n");
6 printf ("The value of a is true");
7}


The C language specifies that all non-0 values are "true", whereas A's value is 7, so the condition of line 3rd is established, followed by the execution of the 5th and 6 lines of code. The output results are as follows:



1 condition A value of 2 A is true


If the value of a is changed to 0, then the condition of line 3rd is not true and the 5th and 6 lines of code are not executed.


3> omitting curly braces {}


You can omit curly braces if there is only one line of code in curly braces {} after the IF. The form is as follows:



if (conditional)    statement 1;


Note: If the condition is true, only the 1th statement after if is executed, and if the condition is not true, the 1th statement after the IF is not executed.



1 int a = 7;
2 
3 if ( a > 9 )
4     printf("aaa");
5     printf("bbb");


Because the a>9 of line 3rd is not true, the 4th line of code is not executed. The 5th line of code has no practice with the IF statement, so the 5th line of code executes as usual. You will then see only the output on the screen:.



Because the 5th line of code is not associated with an if statement, the code is generally written as follows:



1 int a = 7;
2 
3 if ( a > 9 )
4     printf("aaa");
5 printf("bbb");


To ensure the readability of the code, it is not recommended to omit curly braces!!!


4> Statement Nesting


Inside an If statement, you can nest other if statements, as in the following example



1 int a = 7;
  2 
  3 if (a> 0)
  4 {
  5 printf ("The value of a is greater than 0 \ n");
  6
  7 if (a <9)
  8     {
  9 printf ("The value of a is less than 9");
10}
11}


The a>0 of line 3rd is set up, so the code in the 4th to 11th curly braces is executed sequentially. When it executes to line 7th, A<9 is also established, so the 9th line of code is executed. Output Result:



The value of 1 A is greater than the value of-A is less than 9
5> Use NOTE 1


Some people are used to writing a line of code that is appended with a semicolon ";", so when they write the IF statement, they may write:



1 int a = 6;
2 if (a> 8);
3 {
4 printf ("a is greater than 8");
5}


If the semicolon at the end of line 2nd is actually a semicolon, it is called an empty statement. The a>8 of line 2nd is not true, so the following "empty statement" is not executed. The following brace {} is not associated with the IF statement, so it executes normally and you see the output:



A greater than 8


So be very careful not to add a semicolon after the parentheses of the IF.



The content of line 3rd to 5th is a separate "code block":



1 {
2 printf ("a is greater than 8");
3}
6> Use NOTE 2


The following wording is right from a grammatical point of view:



int a = 10;

if (a = 0) {
     printf ("condition is true");
} else {
     printf ("condition does not hold");
}


The above code is completely reasonable, the compiler will not error, just a warning. Because a is 0, so "false", the output is: "Condition is not established"



There is a big trap hidden here:



Suppose you want to judge whether a is 0, then Ben should write if (a = = 0), if you mistakenly write if (a = 0), it would be a very scary thing, because the compiler does not error, such a bug is difficult to find. Therefore, the expression like a==0, preferably written 0==a, if you mistakenly write 0=a, the compiler will directly error.



// Not recommended
if (a == 0) {
}

// recommended
if (0 == a) {
}
7> Use Note 3


In the C language, you can not save the results of the relational operation. Therefore, the following wording is legal:



1 int a = 10;2 a > 10;3 a = = 0;


Here is another trap, assuming that your intention is to assign a value of 0, then Ben should write a = 0; , if you mistakenly write a = = 0; , it will be a very hard to find bug, because the compiler won't make any errors at all. In 1993, the bug almost blew up a $20 million hardware product because if the bug didn't work, the product wouldn't work.





2. Form 2


If can also be used with the keyword else


1> Introduction

1 if (condition)
2 {
3 statement 1;
4}
5 else
6 {
7 statement 2;
8 }


If the condition is true, the statement in braces {} after if is executed, and if the condition is not true, the statement in the curly brace {} After the else is executed. In summary, there are 1 of the two curly braces that will be executed, and only 1 can be executed.



To reduce the number of lines of code, you can also write in the following format:



1 if (condition) {
2 statement 1;
3} else {
4 statement 2;
5}


Of course, you can also omit the curly braces, written in the following format:



1 if (condition)
2 statement 1;
3 else
4 statement 2;


If the condition is true, the 1th statement after if is executed, and if the condition is not true, then the 1th statement after the else is executed. However, it is still not recommended to omit the curly braces {}.


2> Example

1 int a = 10;
2 if (a == 0) {
3 printf ("a equals 0");
4} else {
5 printf ("a is not equal to 0");
6}


The 2nd line of a==0 is not true, so the 5th line of code is executed, and the result is:



A is not equal to 0




3. Form 3


If and else there is a more complex usage


1> Introduction

1 if (condition 1)
  2 {
  3 statement 1;
  4}
  5 else if (condition 2)
  6 {
  7 statement 2;
  8 }
  9 else if (condition 3)
10 {
11 statement 3;
12}
13 ...
14 else
15 {
16 other statements;
17}
    • If condition 1 is true, the contents of the brace {} after condition 1 are executed: line 2nd to 4th
    • If condition 1 is not true and condition 2 is established, the contents of the brace {} after condition 2 are executed: line 6th to 8th
    • If condition 1, Condition 2 is not established, condition 3 is established, the content of the following brace {} is executed on condition 3: line 10th to 12th
    • The 13th line of ... Indicates that there can be an unlimited else if
    • If all of the conditions are not true, the contents of the curly braces {} After else are executed: line 15th to 17th


Note: In so many curly braces, only the code within the 1 curly braces is executed. As before, all curly braces can be omitted, but not omitted. If necessary, the last side of the Else section (line 14th to 17th) can be omitted.


2> Example

1 int a = 10;
2 if (a == 0) {
3 printf ("a equals 0");
4} else if (a> 0) {
5 printf ("a is greater than 0");
6} else {
7 printf ("a is less than 0");
8 }


The a==0 in line 2nd is not true, and then the 4th line is checked. The a>0 of Line 4th is established, so the 5th line of code is executed. Output Result:



A greater than 0


If the value of a is negative, then the 2nd and 4 rows are not valid, so the 7th line of code is executed.





Back to top three, select structure 2-switch statement 1. Form


Let's take a look at the use of the switch statement:



1 switch (integer expression)
  2 {
  3 case value 1:
  4 statement 1;
  5 break;
  6 case Value 2:
  7 statement 2;
  8 break;
  9     ... ...
10 case value n:
11 statement n;
12 break;
13 default:
14 statements n + 1;
15 break;
16}
    • When the value of an integer expression equals "value 1", "Statement 1" is executed, and a subsequent break means exiting the entire switch statement, which is to jump to the 16th line of code;
    • When the value of the shaping expression equals "value 2", "Statement 2" is executed, followed by and so on. If there is no value equal to the value of the integer expression in the numeric 1~ value N, then the statement n+1 in default will be executed.
    • Since all of the case has a break behind it, after executing the statement in either case, the switch statement will be exited directly




2. For example

1 int a = 10;
  2 
  3 switch (a) {
  4 case 0:
  5 printf ("This is a 0");
  6 break;
  7 case 5:
  8 printf ("This is a 5");
  9 break;
10 case 10:
11 printf ("This is a 10");
12 break;
13 default:
14 printf ("Nothing");
15 break;
16}


Because the value of a is exactly equal to the 10 following the case of Line 10th, the 11th line of code is executed, and the result is output:



This is a 10




3.break


The break keyword is the function of exiting the entire switch statement. In the default format, there is a break behind each case, so when you finish executing the statement in the case, you exit the switch statement.



1> If there is no break behind a case, it means that after executing the statement in that case, the statements in all subsequent case and default are executed sequentially until a break is encountered



1 int a = 0;
  2 
  3 switch (a) {
  4 case 0:
  5 printf ("This is a 0 \ n");
  6 case 5:
  7 printf ("This is a 5 \ n");
  8 case 10:
  9 printf ("This is a 10 \ n");
10 break;
11 default:
12 printf ("Nothing \ n");
13 break;
14}
    • Because the value of variable A is equal to 0 after the case of line 4th, the 5th line of code is definitely executed.
    • Because there is no break statement in case 0, the switch statement is not exited and execution continues down the code.
    • Because the value of a is already equal to the value of the 4th case, the value of a is no longer determined to be equal to the value of the other case, and the 7th and 9 lines of code are executed sequentially. There is a break in line 10th, and then the switch statement exits.
    • The output is:

1 This is a 02 which is a 53 which is a 10


If you change the value of a to 5, the output is:



1 This is a 52 which is a 10





2> at some point, we do not really need to add a break after each case. Here's an example: a good grade for judging the score (100 points out).



1 int score = 77;
  2 
  3 switch (score / 10) {
  4 case 10:
  5 case 9:
  6 printf ("Excellent");
  7 break;
  8         
  9 case 8:
10 printf ("Good");
11 break;
12
13 case 7:
14 case 6:
15 printf ("medium");
16 break;
17
18 default:
19 printf ("Poor");
20 break;
twenty one }
    • When the score range is 90~100,SCORE/10 with a value of 10 or 9 o'clock, the 6th line of code is executed, and then the switch statement is exited;
    • When the score range is 80~89,SCORE/10 with a value of 8 o'clock, the 10th line of code is executed, and then the switch statement is exited;
    • When the score range is 60~79,SCORE/10 with a value of 7 or 6 o'clock, the 15th line of code is executed, and then the switch statement is exited;
    • When the range of score is not 60~100,SCORE/10 and the value is not within the 6~10 range, the 19th line of code is executed, and then the switch statement is exited;
    • The value of score is 77, so the value of SCORE/10 is 7, output: Medium




4. Defining variables in case


Sometimes we might want to define some variables in the case, and at this point we have to enclose all the statements in the case with curly braces {}.



1 int a = 10;
  2 int b = 4;
  3
  4 char op = ‘-’;
  5
  6 switch (op)
  7 {
  8 case ‘+‘:
  9     {
10 int sum = a + b;
11 printf ("a + b =% d \ n", sum);
12 break;
13}
14
15 case ‘-‘:
16 {
17 int minus = a-b;
18 printf ("a-b =% d \ n", minus);
19 break;
20}
twenty one         
22 default:
23 printf ("Unrecognized symbol");
24 break;
25}


Two variables are defined in 10th and 17 respectively. Output Result:



A-b=6




Back to top four, circular structure 1-while cycle


What would you do if you had to repeat the output of Hello World 10 times on the screen? Simple, copy the following code 10 copies of the line.



1 printf ("Hello world\n");


Yes, the last code was written 10 times, it is true that the function can be achieved. But this kind of code is too garbage, there are a lot of duplicate code, this will make the code very bloated, the reuse rate is low. Therefore, it is not recommended to do so.



The next time you encounter a repeat operation like the one above, the first thing you should think about is the looping structure. The so-called loop, is to repeat a certain operation, C language There are many ways to achieve the loop structure. Let's look at the while loop first.


1. Form

1 while (condition)
2 {
3 statement 1;
4 statement 2;
5 ....
6}
    • If the condition is true, the statement in the loop body is executed (the "loop Body" is the contents of the curly brace {} after the while). Then judge the condition again and repeat the process until the condition is not set to end the while loop
    • While loop features: if the condition in the while is not in the beginning, then the statement in the loop body will never be executed


You can omit the curly braces {}, but only the first statement that follows the while is affected. Omitting curly braces is not recommended.



1 while (condition) 2     statement 1;




2. For example


Repeat the output 10 times on the screen Hello World, one line at a time for each output.



1 int count = 0;
2 while ( count < 10 )
3 {
4     printf("Hello World\n");
5    
6     count++;
7 }


If the Count++,count of line 6th is always 0, then count<10 is always set up, and the while loop will fall into a "dead loop", repeating the 4th line of code.





3. Note


If written as follows, it also causes the program to enter the "dead Loop"



1 int count = 0;
2 
3 while ( count < 10 );
4 {
5     printf("Hello World\n");
6     
7     count++;
8 }
    • Note the 3rd line, while the back is accidentally added a semicolon; , a semicolon represents an empty statement.
    • As you can see, the while loop affects only the empty statement on line 3rd, and the code block of line 4th to 8th is not affected by the while loop.
    • Since count is 0, the count<10 is always set up, and the program will repeat the empty statement of line 3rd and go into a dead loop.




Back to top v. Loop structure 2-do while loop


The form is as follows:



1 do {
2 statement 1;
3 statement 2;
4 ...
5) while (condition);
    • Note the 5th line, followed by a semicolon;
    • When the Do-while loop is executed, the statement in the loop body is executed first (the "loop Body" is the contents of the curly brace {} behind do). Then the condition in the while is judged, and if the condition is true, the statement in the loop body is executed. Then judge the condition again and repeat the process until the condition is not set to end the while loop
    • Features of the Do-while loop: The statement in the loop body is executed at least once, regardless of whether the condition in the while is true
    • In fact, the use of the Do and loop is similar to the while loop, and this is not an example.




Back to top six, circular structure 3-for cycle 1. Form


The For loop is the most complex of all loop structures.



1 for (statement 1; condition; statement 2) {
2 statement 3;
3 statement 4;
4 ...
5}
    • When the For loop starts, statement 1 is executed first, and the statement is executed only once during the entire loop 1
    • The condition is then determined, and if the condition is true, the statement in the loop body is executed (the "loop Body" is the contents of the brace {})
    • After the completion of the loop body execution, the next execution of statement 2, and then judge the condition again, repeat the process until the condition is not set to end the For loop




2. For example

. 
1 for (int i = 0; i<5; i++)
2 {
3     printf("%d  ", i);
4 }


The output is:



0  1  2)  3  4  


It is important to note that the scope of the variable i is line 1th to 4th. Once you leave the for loop, the variable i is invalidated.





3. Supplement


If the for loop's initialization statement and the statement executed once after the loop are composed of multiple statements, use commas to separate



1 for (int x = 0, y =0; x<3; x++, y+=2)
2 {
3     printf("x=%d, y=%d \n", x, y);
4 }


Output Result:







Back to top seven, break and continue


Next, we introduce two more important statements: break and continue.


1.break


A break has been used in the switch statement before, and its function is to jump out of the switch statement. It can also be used in the loop structure, when it is the function of jumping out of the entire loop statement.


1> Example


Here, for example, break can also be used in a while loop, Do-while loop.



1 for (int i = 0; i<5; i++) {
2     printf("i=%d \n", i);
3     
4     if (i>2) {
5         break;
6     }
7 }


The above code means that when i>2, it jumps out of the entire for loop, which is the end of the for loop, so the result is:







2> for loop nesting


Let's take a look at a for-loop nesting example, where nesting means: A For loop inside a for cycle



1 for (int x = 0; x<2; x++) {
2     for (int y = 0; y<2; y++) {
3         printf("x=%d, y=%d \n", x, y);
4     }
5 }


The output is:








This time if a break is added to the for loop, does this break jump out of the inside or outside for loop?



1 for (int x = 0; x<2; x++) {
2     for (int y = 0; y<2; y++) {
3         printf("x=%d, y=%d \n", x, y);
4         
5         break;
6     }
7 }


Notice the break in line 5th, which is the function of jumping out of the for loop, not the outside for loop. So the output is:








If you change the position of the break



1 for (int x = 0; x<2; x++) {
2     for (int y = 0; y<2; y++) {
3         printf("x=%d, y=%d \n", x, y);
4     }
5     
6     break;
7 }


Notice the break in line 6th, which is the function of jumping out of the for loop, not the for loop inside. So the output is:







The rule is already obvious: break only affects the for loop where it resides.





2.continue


Continue can only be used in a looping structure, and its role is to skip this cycle and go directly to the next loop.



Here for the For Loop example, continue can also be used in the while loop, Do-while loop.



1 for (int x = 0; x<10; x++) {
2     if (x%2==0) {
3         continue;
4     }
5     
6     printf("x=%d \n", x);
7 }


Note the 2nd line, when X%2==0, that is, when X is a multiple of 2, skips the loop, does not execute the 6th line statement, and goes directly to the next loop. Output Result:





Like break, continue only affects the For loop where it resides.



"0 Basic Learning iOS development" "02-c language" 09-Process Control


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.