Black Horse Programmer-Summary of C language selection structure and cyclic structure

Source: Internet
Author: User
Tags case statement

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

After learning the C language Process control, found that the choice structure and the cycle structure have their own characteristics, the following will be a comprehensive summary of the selection structure and cycle structure

The first choice structure and the cyclic structure

The selection structure mainly includes the IF statement and the switch statement, while the loop structure consists of a for loop, while loop, and a Do While loop:

A selection structure

1.if (condition) {

}else if (condition 2) {

}else{

Feature: Only one curly brace code will execute at the same time.

2.switch (value)

Case value 1:

Break

Case Value 2:

Break

Case Value 3:

Break

Default

Break

Features: By default, only one of the code following the case executes

If there is no bresk behind a case, and if the case is true, then all the following statements are executed sequentially until a break is encountered

If you want to define a new variable after the case, you must enclose it in {}.

Two-cycle structure

1.while

Features: If the initial condition is not established, the loop body will never be executed

2.do while

Features: At least one cycle body is executed regardless of the condition

3.for

Select: The For loop is generally preferred, and then the while loop is considered, but needs to be based on the situation, sometimes the while is more useful

Use of break: 1.switch statement: Exit the entire switch statement 2. Loop structure: Exits the entire loop structure

Note: Multiple nesting is only valid for the nearest loop body

Continue use: End the current cycle body, enter the next cycle body

Note: Only valid for the nearest loop structure.

Here is a comprehensive example of a comprehensive loop and select statement to experience the actual example of a loop statement and a selection statement:

1 /*prompt the user to enter a positive integer and output the corresponding *2 For example, if user input 5, the output3        *****4        ****5        ***6        **7        *8 9 */Ten#include <stdio.h> One intMain () A{//define a variable to store the values entered by the user, remember to initialize -     intn=0; -     //To determine the value of n is out of compliance the      while(N <=0) { -         //Prompt user to enter a positive integer, this while loop is a condition for judging user input -printf"Please enter a positive integer: \ n"); -          +scanf"%d", &n); -     } +     //Next is a set of nested loops for outputting graphics A      for(intI=0; i<n;i++) {//determine how many rows, and the case of each row at      -         //printf ("******\n"); -          for(ints=0; s<n-i; s++) {//Judging the case of each line of *, N-i is the * decrement of each line -printf"*"); -         } -printf"\ n"); in     } -     return 0; to}

The SELECT statement for this example is used to determine if the user's input value is not reasonable, and the For loop is a set of nested loops, which is used to determine the number of rows to be output and the number of * per row.

On the scope of variables in the second cycle

Let's take a look at an example to see some points of attention when variables are defined in a loop.

1  intMain ()2  {3     //define the variable in the For loop body, and the loop body has only one statement, it is recommended to write curly braces at any time4      for(intA=0; a<3; a++)5             intb =0;//error, cannot define variable, define only in curly braces6 7     return 0;8}

When you define B here you will get an error, because the scope of B is ambiguous, if you want to define, you need to add braces to the for, on the surface, B belongs to the global of main, because there is only one pair of main function curly braces, but for the for loop, and hopefully this B will be released at the end of the loop, so it So the compiler forces the definition of a variable in the For loop body to be defined in curly braces, and similar to a variable defined in a case statement, so when you use a for or a scenario, even if only one of the statements is accompanied by braces.

Look at this example:

1 intMain ()2 {3      for(intA=0; a<3; a++,b++)//B is certainly inaccessible here, B is defined in a pair of curly braces below, which is the inner structure4 {5printf ("%d\n", a);//output 0, 1, 2, if the b++ in the upper parenthesis is deleted6       intA =Ten;//The same name of a is possible, after the above definition of a, the loop body can continue to define a7       intb =0;8printf ("%d\n", a);//Output every time9 }Ten     return 0; One}

This is the local variable A in the For loop can define the name of the variable in the loop body, only the end of each cycle, a in the loop body will be released

However, when defining a function, the parameter cannot be defined in the function body with the same name variable:

1 int Test (intint  num2)2{3int0;   This is not allowed, according to the above for the loop is possible, but the function of the formal parameter is waiting to receive the value of the argument, and this value in the function body to participate in the operation, is a very meaningful variable, C does not allow redefining a variable name with the same name as the parameter to prevent this parameter from really meaningless, and for a is just a condition for the loop to continue, even if it is redefined in the loop body, it will be released after each loop, and a returns to its original value without affecting the loop, so it can; 4 return 0 ; 5 }      

Summing up, there are many points of attention in the application of cyclic structure and selection structure, which need to be realized in the future, but its structure and usage are fixed, as long as it is in accordance with the normal grammatical structure, it will original aim, and must pay attention to the problem of scope.

Black Horse Programmer-C language selection structure and loop structure summary

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.