Process control _ Select notes

select

1. definitions

Some code is executed, the code may not perform , selectively execute some code

 

2. Classification

if

switch

 

Select _ IF

1.if simplest usage

   if ( expression )

         Statements ;

Function :    If the expression is true, then the statement is executed ; otherwise it does

 

2.if range ( focus )

IF default can only control the execution of a sentence, if you want to execute multiple statements, required braces

② control multiple statements format

    if ( expression )

   {

             Statement A;

             Statement B;

   }

 

IF 3. ... the else ... Usage

① format

   if ( expression )

           Statement A;

   else

           Statement B;

② Note that, the else default only a statement control ( method of performing a plurality of statements and if the same ) , and the sentence A and the sentence B can not be performed at the same time

 

IF 4. ... the else ... IF ... the else usage

① format

  if ( expression )

          Statement A;

  else if ( expression )

          Statement B;

  else

          Statement C;

Note :

① How to control multiple statements execute the same if the same

the else ... IF the number may be increased

③ statements A, B, C can only be performed where a

 

5.C language for true and false judgments

Non-zero is true

That is a false zero

True with a representation

False with 0 represented

 

6. Programs

① Note : the expression can not be written "a <x <b" form, to be written as "x> a && x <b " form

 

 if common issues

1. Empty the problem statement

    if  (3>2);

Equivalent to

    if  (3>2)

     ; // This is an empty statement

Therefore, under normal circumstances if the back expression can not add ";"

 

 

Select _switch

 

1. Format

 

switch ( expression )

 

{

 

   case   constant expression A :

 

        Statement 1;

 

   break;

 

   case   constant expressions B;

 

         Statement 2;

 

   break;

 

   default:

 

         Statement N;

 

}

 

2. Usage

 

①break quit. For example if the sentence 1 no later BREAK , you have to execute the statement 2 . That is, from start to execution break ends.

 

②default can appear anywhere, or may not, but it is recommended to write at the end, it represents remove the case other circumstances match.

 

Some books also the switch as a selection structure

 

Note: Case labels must be constant expressions (constant Expression) can only be used for basic data types Switch , these types include int , char and so on. For other types, you must use the if statement.

 

( Supplementary ) constant expression ( const experssion ): means (1) will not change, and (2) can be obtained calculation result of expression during compilation. Literal belongs constant expression, initialized with constant expressions const object is also constant expressions.

 

An object (or expression) is not constant expression determined by its data type and an initial value.

 

example:

 

const int a = 1; // constant expression

 

cosnt int b = a + 1; // constant expression

 

int c = 2; // initial value literal constant, when c data type is normal int .

 

const int d = fun (); () // fun value to be obtained at runtime, D is not literals

 

3. Examples

 

switch(1+1)

 

{

 

    case 1:

 

    printf("1\n");

 

    break;

 

    case 2:

 

    printf("2\n");

 

    printf("...");

 

    break;

 

    default:

 

    printf("。。。");

 

}

 

Output:

 

2

 

...

 

Guess you like

Origin www.cnblogs.com/qinenxi/p/10994939.html