C language program structure

Performing a general structure of programming language, there are three general, sequence, selection cycle.

Select structure

if else

Only if there is

if(表达式)
{
    语句;
}

if else structure

if(表达式)
{
    语句;
}
else
{
    语句;
}

Nested

if(表达式)
{
    语句;
}
else if(表达式)
{
    语句;
}

..............

else
{
    语句;
}

For else, it is always combined with the front of the first pair of not if, likely to cause confusion. Therefore most of the time will be selected in the nested structure else, else if written in the form of. To prevent confusion, you can use curly braces aligned structure.

switch

if else selection interval can be realized, but if you want to select a particular point, it may also be used switch.

switch(表达式)
{
    case 常量 1:
        代码块 1;
        break;              // 此处的 break 不可以省略
    case 常量 2:
        代码块 2;   
        break;              // 此处的 break 不可以省略
    case 常量 3:
        代码块 3;
        break;              // 此处的 break 不可以省略
    case 常量 4:
        代码块 4;
        break;              // 此处的 break 不可以省略
    default:
        代码块;
        break;              // 此处的 break 可以省略
}
  • Expression (expression) of the switch, must be an integer and character

  • case can only be a constant expression

  • case branch must be used in conjunction with a break, so jump out switch, otherwise it will continuously execute the rear of the statement until the next break.

  • Between the two case blocks, braces can not.

Loop structure

Repeat loop structure for a section of code. Three elements present in the cyclic structure: loop initial condition loop termination condition, terminates the loop variable tends conditions.

while

while (表达式)
{
    语句;
}

do while

do
{
    语句;
}while (表达式);
  • While loop structure is false, may not perform in a judgment expression

  • do while construction is performed once at least, after the judge expression of true and false, is true only continue

  • do while input checking is still relatively easy

for

for (表达式 1; 表达式 2; 表达式 3)
{
    语句;
}
  • Expression 1 is the cycle initial condition, if the initialization has been completed to the structure, can be omitted

  • Expression of loop termination condition 2, if the termination condition is present in the loop, may be omitted

  • 3 is approaching the expression loop condition, if the condition exists in the loop approach may be omitted

For all of the loop structure is:

  • Like the essence of all of the cyclic structure, just different ways to achieve the degree of convenience when solving specific problems is not the same
  • while the number of cycles may be used when uncertain, for the general case for cycles determined, do while generally need to perform at least one case.
  • and while the first are determined for further execution, do while determination is performed first and then

Of course, all the nested loop can be achieved, it is nested:

  • Writing from outside to inside the nested loop
  • Loop control variable may be a "half-closed interval" wording, such as for (i = 0; i <= 10; i ++)
  • When nested, the longest loop should be placed innermost, an outermost layer on the shortest cycle, to reduce the number of cpu cycles cross-cutting layer

Program jump

break

Used to advance the end of the cycle or select (switch), only out of the current layer circulation.

continue

To end the current round of cycle, cycle abandon the current round of follow-up statement, go to the next time through the loop. It can only be used in a loop structure.

return

End the current function call returns the result. If the current function is the main function, the process ends.

goto

When performing the goto statement, the program will jump to immediately after the identification of the goto statement, as follows:

#include <stdio.h>
#include <Windows.h>

int main()
{
	int i, j;
	for (i = 0; i <= 10; i++)
	{
		for (j = 0; j <= 10; j++)
		{
			printf("%d,%d\n", i, j);
			if (i >= 5)
				goto target;
		}
	}

target:printf("everying is over;");

	system("pause");
	return 0;	
}

Implementation of the results:

0,0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
0,10
1,0
1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
2,0
2,1
2,2
2,3
2,4
2,5
2,6
2,7
2,8
2,9
2,10
3,0
3,1
3,2
3,3
3,4
3,5
3,6
3,7
3,8
3,9
3,10
4,0
4,1
4,2
4,3
4,4
4,5
4,6
4,7
4,8
4,9
4,10
5,0
everying is over;

But not too much use of the goto statement in actual use, as it will damage the structure of the program design process. But goto efficiency out of multiple loops can still be pricey, there is the ability to focus error.

Published 77 original articles · won praise 5 · Views 4890

Guess you like

Origin blog.csdn.net/SAKURASANN/article/details/104420677