C Language Learning Summary (2) computing process and Language Learning Process

Source: Internet
Author: User
Tags bitwise operators

C Language Learning Summary (2) computing process and Language Learning Process
Chapter 3 basic operations

(Operator, arithmetic operator, relational operator, logical operator, three-object operator, ASXLL code)

 

1. What are operators?

Concept: it is the symbol used by the compiler to execute specific arithmetic or logical operations;

Classification: Arithmetic Operators, Relational operators, logical operators, and bitwise operators.

The operation operations are divided:

Calculation rules: mathematical calculation rules. First, first, the priority, and then the combination Law (from left to right [the assignment operation is from right to left x = y = z ]);

Ii. Arithmetic Operations

"+" Addition operator "-" subtraction operator "*" Multiplication operator "/" Division operator "%" remainder operator.

  • 1. Different data types have different precision. double> float> int. The remainder operator can only be an integer;
  • 2. If you assign a value to a low-precision variable with high precision, the precision will be lost. For example, if int a = 1.8, the value is 1 (not rounded)
  • 3. The operation Priority is. From left to right, but: * =/= %> + =-if you want to manually change, add ().
  • 4. During the operation, the two data types have different precision, and the data type is automatically converted from low-precision to high-precision.
  • 5. Only results with the same precision can be obtained for same precision operations. If you need to improve the accuracy, You need to force type conversion.

 

Iii. Value assignment

Concept: Assign the value on the right of "=" to the variable on the left;

Note: The variable must be on the left of the equal sign;

  • A = 10; yes;
  • 10 = a; error;

Compound assignment operator: + =,-=, * =,/=, % =;

 

4. Auto-increment and auto-increment operators:

Concept: Let the value of the connected operand + 1 or-1

Format: variable name +++ variable name -- variable name
  • 1. variables can change themselves, such as auto-increment or auto-subtraction, but constants cannot.
  • 2. ++ a is no different from a ++, but the overall value of a ++ remains unchanged when involved in other operations.
5. siziof Operator

Concept: calculates the number of bytes occupied by constants, variables, and data types in our memory;

Usage: Note: 6. relational operation

"=" ">" "<"> = "" <= ""! ="

Usage: Compare the data to see if it is true. The condition is true (1), and the condition is false (0 ).

  • 1. the relational operator returns an int value. 0 or 1.
  • 2. Priority:>, <, >=, <=, the priority is equal, but greater than = and! =. At the same time = and! = Equal priority.
  • 3. if there are two operations with the same priority, view the Union law in sequence (the Union law refers to either the left-to-right operation or the right-to-left operation). The Union law of Relational operators is from left to right, that is, first look at the priority, let's look at the combination law. as follows:

Int B = 1! = 2> 3 <4; equivalent to: 2> 3 is false = 0. 0 <4 is true = 1, 1! = 1 is false = 0. Therefore, the final result is 0;

VII. logical operations

"|", "&", And "! "Non

Usage: checks the true and false of multiple expressions.

Note:

  • 1. All data has a false value. 0 is false, and other data is true (required). Combination law: Execute from left to right,
  • 2. If | the expression on the left is true, no execution is performed on the right. & as long as the expression on the left is false, no execution is performed on the right.
  • 3. Priority Order: parentheses ()> minus sign (-)>! > Arithmetic Operators> Relational operators >&>||
8. Three-object operator-(poor readability, generally not needed) Definition:

<True/false judgment Expression 1>? <Expression 2>: <expression 3>;

For example, 1 + 2> 4? 2 + 2: 3 + 4; the calculation process is: 1 + 2 = 3, 3> 4 is false. Then the expression 3 + 4 is run, so the final result is 7.

 

9. ASCLL code

Concept: American standard information interchange code

The character operation is automatically converted to the value of the ASCLL table:

'A'-'B' = (97-98)

Frequently used:

 

Chapter 2 Process Control

(Sequential structure, selection Structure -- if/swich, loop structure -- do... while/)

1. program structure 1. Default (sequential structure): Execute from top to bottom 2. Select Structure

It is used in programming to determine whether the given condition is met. One of the two operations is determined based on the determined result (true or false ).

If branch statement:

-- Used when there are multiple choices during Program Execution

Format:

1. if (expression ){

Statement block;

}

 

2. if (expression 1 ){

Statement Block 1;

} Else if (expression 2 ){

Statement Block 2;

} Else {

Statement block 3;

}

And so on ............

Evaluate the expression: If the expression value is not 0 (true), the statement Block 0 (false) will not be executed ,;

 

Note:

  • 1. One if statement can only execute one of the given operations.
  • 2. if (); In this usage, the if statement is valid only after the next sentence. (reading the next sentence; the end of the number). Variables cannot be defined.
  • 3. Except for if ();, all if () and {} cannot have;
  • 4. if (condition judgment) {}, where the condition judgment is equal to the constant judgment, then the constant is written before (recommended );
  • 5. if (1), if (-1), if (a = 5) Permanent true if (0) Permanent false;
  • 6. if (expression) return 0; // exit the program and the code will not be executed.
Swich branch statement

-- Like if, it is also used for condition judgment. It is easier to use switch when the expression results have multiple situations.

Format:

Switch (expression) {---> expression: the return value is an integer.

Case constant 1: Statement 1; ---> the constant must be an integer.

Break;

Case constant n; statement n; ---> variables defined in the statement can cause disorder: (int a = 10) error!

Break;

Default: Statement n;

If none of the above conditions meet the execution default;

}

Note:

  • 1. The condition result can only be an integer, while the case can only be an integer constant. Note: case 'a ';
  • 2. The break is optional. If yes, the task jumps out after execution. If no, the task continues until the next break or execution is complete.
  • 3. The default statement is optional. If none of the conditions are met, the statement is executed. If none are met, the statement is executed and the execution ends.
  • 4. variables to be defined under case must be added with {}. The constant values after case cannot be repeated.
  • 5. switch statements can be replaced by if statements, but if statements cannot be replaced by switch statements.
Use the break and continue keywords:

Concept:

Both the break and continue statements can be used in a loop to jump out of the loop (end loop );

The break statement can also be used in the switch statement to jump out of the switch statement.

Note:

// If (expression) else {... break ;......}

1) The break statement does not work for the if-else Condition Statement;

2) in a multi-tier loop, a break statement only jumps one layer outward.

3) continue indicates skipping this loop instead of jumping out of the loop body;

 

3. Loop statements

Concept:

It refers to a programming loop statement. when the conditions are met, the loop is judged cyclically until the loop does not meet the conditions and runs a piece of code repeatedly according to the conditions.

1. while statement:

Format:

While (condition) {--- first judge and then execute: the condition is true and the Execution code block is false;

Statement block;

}

Note:

Int num = 3;

While (3 = num ){

Printf ("num = % d \ n", num );

Num ++;

}

1. usually write constants in front

2. If there is only one statement after while, it can omit braces.

3. You cannot directly write a semicolon (meaningless) after the while symbol)

4. Scope: variables defined in the internal code block cannot be accessed in the external code block.

5. Scope disorder: while (1 <3) int num = 5;

6. Simplest endless loop (the condition is always true, and the loop cannot end): while (1 );

 

 

2. do... whlie statement:

Format:

Do {

Statement block;

} While (condition );

Features: Compared with while, statement blocks are executed at least once after execution;

 

3. for statement:

Format:

For (expression 1; expression 2; expression 3) {--- three statements separated by two semicolons.

Statement block 4;

}

Execution Process:

1-> 2-> 4-> 3-> 2-> 4-> 3 ......

 

Expression 1: the initial value of the loop variable. It is executed only once before it starts;

Expression 2: If the loop condition is true, the code of statement block 4 is executed. False. The loop ends;

Expression 3: value-added cyclic variables: Execute after each execution is complete;

Note:

  • 1. if you encounter a continue, jump out of this loop and continue the next judgment. If the condition is true, continue to execute the loop.
  • 2. If the break is met, the cycle ends immediately regardless of whether the condition is met.
  • 3. Both continue and break jump out of the current loop scope. If the outer layer has a loop, it will not be affected.
  • The for (;) Statement is equivalent to the while (1) statement and is an endless loop.
Nested for loop:

1. for (int a = 0; a <5; a ++ ){

Statement Block 1;

For (int B = 0; B <10; B ++ ){

Statement Block 2;

}

}

Core:

 

Case 1: (while)

1. Train of Thought: 2. 1) receive the string entered by the user. 3) judge whether the user has typed the Enter key. 4. // define the character variable to receive the character. 5. char ch; 6. // define a counter variable, the number of characters to be saved is 7 int count = 0; 8 // receives the first character of the user input string 9 scanf ("% c", & ch ); 10 // other characters in the input buffer are obtained cyclically 11 // ch = '\ n' to determine whether the obtained character is a linefeed 12 // if it is a linefeed, end 13 while (ch! = '\ N') {14 // counter + 115 count ++; 16 // obtain the next character scanf ("% c", & ch) of the input buffer again ); 17} 18} 19 // print the counter value printf ("count = % d \ n", count );Count the number of characters entered in one line from the keyboard

Case 2: (while, switch)

1. Train of Thought: 2, 3, 1) keep repeating, waiting for users to enter 4, 5, 2) enter four characters, print the corresponding direction 6 7 switch 8 9 // define the variable to store the user input content 10 11 char ch; 12 13 // define the flag, 0-normal, 1-Exit 14 int flag = 0; 15 printf ("Please control the movement of the villain: w. s. a. left d. right q. exit \ n "); 16 17 while (! Flag) {18 19 scanf ("% c", & ch); switch (ch) {20 21 // determine the case of w input in upper and lower case 22 23 case 'W': 24 case 'W': 25 26 printf ("upper \ n "); 27 28 break; 29 30 // determine the case where the user inputs uppercase and lowercase s 31 32 case's: 33 34 35 case's ': 36 37 printf ("bottom \ n"); 38 39 break; 40 41 // determine the case where a is input in upper and lower case 42 43 case 'A ': 44 case 'A': 45 46 printf ("Left \ n"); 47 48 break; 49 50 // determine the case where the user inputs d in upper and lower case 51 52 case 'D': 53 case 'D': 54 55 printf ("right \ n "); 56 57 break; 58 59 case 'q': 60 61 case 'q': 62 63 printf ("exited \ n"); 64 65 flag = 1; 66 break; 67 68 }}Enter wsad to print the corresponding direction

Case 3: (if... else, while)

1 idea 2 // 1. Define variables, save the number of players' guesses and the number randomly generated by the computer. 3 // 2. Let the computer randomly generate a number ranging from 1 to. 4/* 5. Difficulty: how to generate a number ranging from 1? 6 arc4random_uniform (1000) generates an integer between 0 and 1001. 7 arc4random_uniform (1000) generates an integer between 0 and. 8 is required to be 1, so what does not conform to 9 arc4random_uniform (1000) + 1 produces is: 0 (+ 1)-999 (+ 1), which is the number 10 between 1 and, the random number formula between n is 11 arc4random_uniform (n-m + 1) + m 12. If a random number between 10 and 50 is generated, 13 arc4random_uniform (50-10 + 1) + 10; the random number between 14-1000 is 15 arc4random_uniform (116-1 + 1) + */17 // 3. It is controlled cyclically, allows the user to guess 10 times at most 18 // determine the number if the guess is more than the number randomly generated

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.