2015.8.5 cycle and function, bit operation

Source: Internet
Author: User

Note the point:

1, only the shaping can use switch.

2. Constants after case cannot be repeated

3, the default statement can be placed in any position, but can not be omitted after the break;

While

1, to handle a number of repeated operations

2, determine the conditions of the cycle of termination

(1) Before the loop begins, you need to define a loop control variable and initialize it

(2) Determine the constraints of the cycle

(3) At the end of each cycle, in the loop, to change the value of the loop control variable, so that it does not break close to the constraint conditions

Note the point:

1, in the C language of any branches are true and false, only 0 is false, the others are true

2, pay attention to not to confuse = = and =, in order to avoid this error, write the constant in front, the variable is written in the back

3,; The number is an empty statement, and the empty statement is also a statement when the while is directly followed; then it is only; and while is one, then the curly braces in the back are not related to the while.

4. Variables defined inside the while are not accessible externally

5, scope disorder: If you want to define a type of variable in the loop body, you must have braces

do{}while{}:

1. The statement of the loop body executes at least once

For (initialization expression: conditional expression: post-loop action expression) {}

1. Initialize expression: Executes only once before the loop starts

2, conditional expression, in the absence of a loop begins to fall when the loop condition expression is executed, if the condition satisfies that executes the statement in the loop body, if the condition does not satisfy on the end loop

3, after the loop operation expression: when there is no end of the loop will be executed after the loop operation expression

Note the point:

1. You cannot write semicolons directly after for

2, scope disorder, to define variables in the loop body must have curly braces

Scope in for loop

1. Variables defined in the for parentheses cannot be accessed outside the For loop

2, cyclic weight definitions of variables in parentheses can not be used

Break: Used in the loop statement to end the current loop statement;

Continue; can only be used inside the loop, the function skips the back of the current loop, and continues the next conditional judgment

if (!a) {continue;} printf ("a");

Loop nesting:

1, for not easy error

2, the code is more neat, more readable

3, when the entire cycle is also required to use the loop control variable with while

4. If the conditional expression is not written, the system defaults to True

5. For Loop variant

for (;;;) Dead loop

int i = 0;

for (;; I<2)

{

printf ("%d", I);

i++;

}

Total executions = number of external cycles * cycles executed in

Whenever a row and column problem is encountered, loop nested, outer loop control row, Inner loop control column

Summary: Conditional expressions of the inner loop change with the change of the outer-layer control variable

Cycle Summary

When using continue in while, it is important to ensure that changes to the loop control variable are placed in front of the continue, otherwise it will cause a dead loop

How to tell if else

1, from the top down to find else

2, from the bottom to find if, the first found no matching if is the else pair

Function:

Function: Complete a specific function code block, function name can not be defined repeatedly, give the function a meaningful name

Function: Managing code blocks

Function Advantages: Improve the readability of your code

Improve the reusability of code and improve the maintainability of code.

Function feature: Will not execute when called

How to define a function:

1, according to function function to give the function a meaningful name

2. Determine if you need to pass in some auxiliary values

3. Determine what type of data needs to be returned to the function caller

4. Complete function function

When the function does not return a value, declare the function's return value as void

When the return value of a function is void, the return statement can be omitted

When we need formal parameters, when we do not function, some of the data we are not sure, then this time we need to tell us.

Return value type: What type of value is returned to the function caller after the function has finished executing

Parameters of the function:

Line parameter: When taking your intentional function, the arguments in parentheses after the function name are called formal parameters.

Arguments: When calling a function, the function name is followed by the parentheses of the total arguments we call the actual arguments

1, the actual participation in the line parameter one by one corresponds to the

2. What type of value do you have to enter for the argument?

3, row parameter list can not have the same name of the row parametric

4. A variable with the same name as the row parameter cannot be directly defined inside the function

5, the basic data type as a parameter to pass the test value transfer

return value type:

Keynote function: The function that calls other functions

A function that is called in another function

1, in the function definition I declare what is the return value type, then in the keynote function must be received by a variable of type

2, what kind of return value type, you must return the value of what type, to ensure that the return value of the type is consistent with the return value in the function definition

3, if the return value of the function is not NULL, it must have a return value, cannot be empty

The function of return:

1. Return data to function call

2. End Function

3. In a function that returns a value of type void, you can use Retrun to end the function prematurely

4, in the same function can have more than one return statement, but only one return will be executed, only one return executed, then the code will not be executed, if (num=1) {printf ("a") return} return NUM1 + num2;

Note: There is no concept of function overloading in C, which distinguishes a function simply by the name of the function, as long as the name is the same as the duplicate definition, regardless of whether the row parameter is the same

Procedure for calling a function:

1, in the C language, the function is not allowed to nest, that is, the function inside can not define other functions

#include的作用:

Precompiled directives: parse the processing commands before compiling

1, at the time of pre-compilation copy # # # After the file content # include command location

# include "File name"

1. Locate the directory where the current file is located

2, if the current directory is not found on the path to the compiler include to find

3. If you have not found it, go to the system's include path to find

4, if not found on the error

# include < file name >

1, go to the compiler include path under the search

2, if not found to go to the system include the path to find

3, if not found on the error

Summary: When you include a project file (a file that the programmer writes himself) is # include ""

Use #include<> when you need to include system files

function declaration:

Copy the head of the function to the bar in front of him.

Role: Tell the compiler that the function already exists

A complete function consists of two parts, one is the function declaration, the other is the function definition

Mac file directory:

/path Front: The hard drive (with directory) that represents the flag computer

Absolute path: The path that begins with/

Relative path: A path that is not/begins with a relative path, that is, relative to the current path

Multi-File development:

The Declaration of the function and the implementation of the function are divided into a. h and a. c file for development, and others use my method of implementation only need to import my defined header file

Problems with the system in the computer

Binary: A binary number that starts with 0 B

Octal : The octet number is preceded by 0

16 binary:0x or 0x The number that begins is a hexadecimal number

Binary conversions:

format character for octal output:%o%o

16 Binary output format:%x output a-f%x a-f

There are no binary output format characters in the C language

Decimal conversion binary:

rule: In addition to 2 of the residual flashback row

such as the remainder of the quotient

/ 2 6 1

6 / 2 3 0

3 / 2 1 1

1 / 2 0 1

Binary Conversion Decimal

Multiply each bit by the number of powers on the corresponding bit and add

binary conversion octal: One octal number corresponds to 3 binary numbers, three binary numbers max represents 7, and one octal maximum of 7

Binary conversion to System : One hexadecimal corresponds to 4 binary , four binary number Max represents , a maximum of three decimal systems

Octal-binary : first to binary in turn

Decimal to the system: take more than the flashback row

three-in- one brick : Multiply each bit by the number of powers on the corresponding bit and add

Original code anti-code complement

positive integers: The original code is the same as the reverse code, positive integer 0 begins, the first bit is the sign bit

1=ge0 1

Negative Number:

-1

symbol 1 = 1 ge0 1

Original code: 0000 0000 0000 0000 0000 0000 00001

Anti-code: the original code symbol bit unchanged, the other reverse

complement: Anti-code +1

The digital complement is stored in the computer .

Bitwise operations: for BITS operations

& Bitwise and: The corresponding bits, a 0 is 0, all 1 is bit 1 a false false

| bitwise OR: The corresponding bits, a 1 is 1, all 0 is bit 0 a true

^ Bitwise XOR: Same as 0, different 1

1, multiple number of different results unchanged (Exchange law)

2, any number of different or 0 is the original number

3, the same number of each other or 0

~ Bitwise REVERSE

Move right >> m >> N m to the right to move n bits, remove the bit directly, and move in the bit complement sign bit (integer complement 0, negative complement 1)

Use scene: Quickly calculate a number divided by 2 of the N- Square, for an integer to the right of a few is divided by 2 a few times put

Shift left <<: m << n:m to the left N-bit, one out of the bit directly deleted, as bit complement 0

Moving left may change the number of digits

Usage scenario: When comparing hours, you can quickly calculate How many times this number is multiplied by 2.

Fast counting 2 of the 3- Time Square

Practice:

1.swap the value of two variables without the help of a variable

2. Determine the parity of a number

(1) The first type: arithmetic operations

A = a-B;

    B = a + B; //B = original a - original b + original b =< Span class= "S1" > original a

A = b-a; //A= original a -(original a - original b) = original a - Original a + original b = original b

(2) The second way: also or Operation

A = a ^ b;

    B = a ^ b; // original a ^ original b ^ Original b = original a;

A = a ^ b; // original a ^ original b ^ original A = original a ^ original A ^ original b = 0 ^ original b = original b

2, (1) use if

(2) not recommended for use

N% 2 = = = 0 ? printf("even \ n" ):(" odd \ n");

(3) &

101 & 001 = 1; Look at the last one, the others are multiples of 2.

if(n&1)

{

printf (" odd \ n");

} Else {

printf (" odd \ n");

}

The most important thing about the whole today and yesterday is the flexible use of loops, and the mastery of the definition declarations of functions.

2015.8.5 cycle and function, bit operation

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.