Getting started with iOS development☞C language (branch structure + loop structure), ios Branch

Source: Internet
Author: User

Getting started with iOS development☞C language (branch structure + loop structure), ios Branch
Branch Structure: if statement:

If (conditional expression ){

Statement;

...

}

At least one code is executed.

If (conditional expression ){

Statement;

...

} Else {

Statement;

...

}

If (conditional expression 1 ){

Statement;

....

} Else if (condition expression 2 ){

Statement;

....

} Else if (condition expression 3 ){

Statement;

....

}

....

Else {

Statement;

....

}

Features:

  • At most one of these code blocks will be executed.
  • When the program executes the conditional expression 3, it means that all the previous expressions are not valid.
  • If the if statement contains else, a code block will be executed

If statement features:

  • All begin with if
  • You can have any else if
  • Up to one else statement can be entered.
  • If the if statement is followed by only one statement, the braces after the if statement can be omitted (usually not in development)

 

If nesting: Other if statements can be nested inside the if statement.

Conclusion: if statements are also statements. if statements can be written wherever they can.

If (conditional expression ){

Statement;

...

If (conditional expression ){

Statement;

...

}

Statement;

....

}

 

Omit the if Statement of braces: when there is only one statement after the if statement, the braces after the if statement can be omitted.

 

Switch statement:

1. Expression

The result of expression calculation must be an integer (short, int, long, char is a special integer). It cannot be a float or string.

2. case:

1) must be a constant, not a variable

2) A case can only have one constant value of an object.

3) The constant value must be an integer.

4) The value after case cannot be repeated (each case value cannot be repeated)

5) each case can be followed by 0 ~ N statements

3. default

  • Yes, no, or whatever.
  • Default does not have to be placed in the last branch. It can also be placed in the middle or front. Generally, all are placed at the end.
  • No matter where the default is written, it will be executed at the end (only when all the previous cases are not satisfied) as long as a certain case meets, all subsequent cases and default values will be invalid.

 

Note:

  • Constants after case cannot be repeated
  • Break: ends the switch statement.
  • The switch statement ends only when the break or switch is followed by braces.
  • When the switch matches a case, all the subsequent cases become invalid.
  • Only one constant or constant expression can be added after case.
  • The trumpet after the switch can only be an integer expression

/*

Case 1:

Int a = 2;

Break;

*/

// If a new variable is defined in case, it must be wrapped in braces {}

Case 0:

{

Int a = 2;

Break;

}

 

Switch (month ){

Case 12:

Case 1:

Case 2:

Printf ("Winter \ n ");

Break;

Case 3:

Case 4:

Case 5:

Printf ("Spring \ n ");

Break;

Case 6:

Case 7:

Case 8:

Printf ("Summer \ n ");

Break;

Case 9:

Case 10:

Case 11:

Printf ("Autumn \ n ");

Break;

Default:

Printf ("Do not discard treatment \ n ");

Break;

}

 

/*

1. When a variable has only a few fixed values, it is more concise to use switch.

2. if the range is [-infinity, 1] [10, + infinity], use if (x <= 1 | x> = 10)

*/

 

Case:

/** Addition, subtraction, multiplication, division, and Division calculator (Prompt: Input 1 + 2 and output 1 + 2 = 3 )*/

Int test (){

Int num1, num2; // defines two variables, indicating the operands.

Char operator; // defines a char type variable, indicating that the operation is required.

Printf ("Please input in the following format (1 + 1) \ n"); // prompt to use the input

Scanf ("% d % c % d", & num1, & operator, & num2 );

Int result;

Switch (operator ){

Case '+ ':

Result = num1 + num2;

Break;

Case '-':

Result = num1-num2;

Break;

Case '*':

Result = num1 * num2;

Break;

Case '/':

If (num2 = 0 ){

Printf ("the divisor cannot be zero \ n ");

Return 0;

}

Result = num1/num2;

Break;

Default:

Printf ("your input format is incorrect \ n ");

Break;

}

Printf ("% d % c % d = % d \ n", num1, operator, num2, result );

Return 0;

}

 

Loop Structure:

Loop is the execution of the same code block again and again

Loop: used to process repeated operations

 

Steps for writing a loop:

1. Determine the operation to be repeated

2. determine the conditions for cyclic termination:

1) do something before the loop starts (define a loop control variable and initialize it)

2) Determine cyclic Constraints

3) at the end of each loop or in a loop, change the value of the loop control variable to keep it close to the constraints.

 

 

The simplest endless loop:

 

  • While (1 );
  • For (;); // that is, for (; 1;); Note: The condition expression in the for loop is true by default.

  

  

Do while and while are different:

While if the conditional expression is not met at the beginning, the loop body will never be executed.

Do while if the conditional expression is not met at the beginning, it will also execute a loop body

That is to say, do while always executes a loop body regardless of whether the conditional expression is satisfied.

 

 

Related Article

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.