From

xiaoxiao2021-03-06  162

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

Branch structure program

Relational operator and expression

There is often a comparison of two amounts of size in the program to determine the program's next step. Compare two amount operators called relational operators. There is a relationship operator in the C language: greater than> = greater than or equal to == equal to! = Not equal to the relational operator is a binocular operator, its bonding is left binding. The priority of the relationship operator is lower than the arithmetic operator, which is higher than the assignment operator. In the six relationship operators, <, <=,>,> = is the same, higher than == and! =, == and! = Priority the same. The general form of relational expression relationship expression is: expression relational operator expression, for example: A B> CD, X> 3/2, 'a' 1 (b> c), A! = (C == d), etc. The value of the relationship expression is "true" and "false", expressed with "1" and "0". Such as: 5> 0 is "true", that is, 1. (A = 3)> (b = 5) Since 3> 5 is not established, its value is false, that is, 0. Void main () {char c = 'k'; int i = 1, j = 2, k = 3; float x = 3e 5, y = 0.85; Printf ("% D,% d / n", 'a ' 5 = k 1); Printf ("% D,% D / N", 1 = k 1); Printf (" % D,% D / N ", 1

Logical operator and expression

The logical operator C language provides three logical operators & calculation || or operations! Non-computational and operators & or operators || are bibliographic operators. Has left bonding. Non-operator! For the single operator, with right binding. The relationship between the logical operator and other operator priority can be represented as follows: According to the priority order of the operator: A> B && C> D is equivalent to (a> b) && (c> d)! B == C || D C && x y C) && (X Y) 0 && 4> 2, due to 5> 0 true, 4> 2 is true, the results are true. 2. Or Operations || Two quantities participating in the calculation As long as one is true, the result is true. The two quantities are false, the result is false. For example: 5> 0 || 5> 8, due to 5> 0 is true, the phase or the result is true 3. Non-operation! When the carrier is true, the result is a fake; the participation of the operation is fake, the result It is true. For example:! (5> 0) results are fake. Although C is compiled when a logical operation is given, "1" represents "true", "0" represents "false". However, when it is determined that one amount is "true" or "false", "0" represents "false", the value "true" is "true". For example, since 5 and 3 are not "0" therefore the value of 5 && 3 is "true", that is, 1. Another example: 5 || 0 value is "true", that is, 1. A general form of logical expression logical expressions: The expression logical operator expression is a logical expression, thereby constituting a nested situation. For example: (A && B) && C, according to the left binding of the logical operator, the above equation can be written as: A && B && C logical expression value is the final value of various logical operations in the style, representing "1" and "0" respectively " True "and" false ".

Void main () {char c = 'k'; int i = 1, j = 2, k = 3; float x = 3e 5, y = 0.85; Printf ("% d,% d / n",! x *! y, !!! x); Printf ("% D,% D / N", X || I && J-3, I

The use of an IF statement can constitute a branch structure. It judges based on a given condition to determine the execution of a branch block. There are three basic forms of the IF statement of the C language.

1. The first form is a basic form IF (expression) statement; its semantics is that if the value of the expression is true, then the following statement is executed, otherwise the statement is not executed. The process can be represented as the following picture void main () {Int A, b, max; printf ("/ n INPUT TWO NUMBERS:"); Scanf ("% D% D", & A, & B); max = a; if (Max

In this example, two numbers A, B are entered. Turn A to the variable max, then discriminate the size of Max and B with the IF statement, such as MAX is less than B, then give B. Therefore, MAX is always large, and finally outputs the value of the max. 2. The second form is an IF-ELSE form IF (expression) statement 1; ELSE statement 2; its semantics is that if the value of the expression is true, the statement 1 is executed, otherwise the statement 2 is executed. Void main () {Int A, B; Printf ("Input Two number," "% D% D", & A, & B); if (A> b) Printf ("max =% d / n" , a); ElsePrintf ("max =% d / n", b);} Enter two integers to output large numbers. Change the size of the IF-ELSE statement to determine A, B, if A is large, then output A, otherwise output B. 3. The third form of the IF statement in the first two forms of IF-ELSE-IF is generally used for two branches. When there are multiple branch selection, IF-ELSE-IF statement can be used, and its general form is: IF (Expression 1) statement 1; ELSE IF (Expression 2) statement 2; ELSE IF (Expression 3) Statement 3 ... ELSE IF (Expression M) Statement M; ELSE Statement N; It is semantically to determine the value of the expression, and the corresponding statement is executed when a certain value is true. Then run the executor outside the entire IF statement. If all expressions are fake, the statement n is executed. Then continue the follow-up program. The execution process of the IF-ELSE-IF statement is shown in Figure 3-3.

#include "stdio.h" void main () {char C; Printf ("INPUT a character:"); c = getchar (); if (c <32) Printf ("this is a control character / n"); Else IF (C> = '0' && c <= '9') Printf ("this is a digit / n"); Else IF (c> = 'a' && c <= 'z') Printf ("this is a Capital Letter / N "); Else IF (C> = 'a' && c <= 'z') Printf (" this is a sales sall letter / n "); ElsePrintf (" this is an an other character / n ");} IF (C <32) Printf ("this is a control character / n"); ELSE IF (c> = '0' && c <= '9') Printf ("this is a digit / n"); ELSE IF C> = 'a' && c <= 'z') Printf ("this is a capital letter / n"); Else IF (c> = 'a' && c <= 'z') Printf ("this is a sales / N "); ElsePrintf (" this is an an other character / n "); this example requires the category of the keyboard input character. The type can be discriminated based on the ASCII code of the input character. The ASCII code is known to be less than 32 of the control character. The number between "0" and "9", between "A" and "Z" is uppercase letters, between "A" and "Z" lowercase letters, and the rest is other characters. This is a multi-branch choice problem, with the IF-ELSE-IF statement programming, determine the range of the input character ASCII code, gives different outputs. For example, it is input to "G", and the output is displayed to lowercase characters. 4. Pay attention to the following questions in using the IF statement

(1) In three forms of IF statements, both the IF key is expressed. This expression is typically a logical expression or a relationship expression, but it can also be other expressions, such as assignment expressions, etc., or even a variable. For example: if (a = 5) statement; if (b) statement; it is allowed. As long as the value of the expression is not 0, it is "true". Such as IF (a = 5) ...; the value of the expression is always non-0, so the subsequent statement is always executed, of course, this situation does not necessarily appear in the program, but in the grammar is legal . Another example, there is a block: if (a = b) Printf ("% d", a); ElsePrintf ("a = 0"); this statement is semantically assigned to A, such as non-0 output This value, otherwise output "A = 0" string. This usage often occurs in the program.

(2) In the IF statement, the condition judges that the expression must be enclosed in parentheses, and you must add a bit number after the statement.

(3) In three forms of the IF statement, all statements should be a single statement. If you want to execute a set of (multiple) statements when you meet the conditions, you must enclose this group of statements to make a set of {}. Composite statement. However, it should be noted that it cannot be added again after}. For example: if (a> b) {A ; b ;} else {a = 0; b = 10;}} nested in the IF statement

When the execution statement in the IF statement is another IF statement, the IF statement is nesting. Its general form can be represented by the following: if (expression) IF statement; or if (expression) IF statement; ELSE IF statement; if the IF statement in nested may be IF-ELSE type, this will appear The case of an IF and multiple ELSE overlap, then pay special attention to the matching problem of IF and ELSE. For example: IF (Expression 2) IF (Expression 2) statement 1; ELSE statement 2; where is the ELSE is paired? It should be understood as: or it should be understood as: IF (Expression 1) IF Expression 1) IF (Expression 2) IF (Expression 2) statement 1; statement 1; ELSE ELSE Statement 2; Statement 2; In order to avoid this erriness, C language regulations, Else always nearestly The IF pair, so the above example should be understood in the previous case. Void main () {Int A, B; Printf ("Please Input A, B:"); Scanf ("% D% D", & A, & B); if (a! = b) IF (A> b) Printf ("A> b / n"); Else Printf ("a b) Printf ("a> b / n"); Else Printf ("a B, A b) Printf (" a> b / n "); Else Printf (" a

Conditional operator and conditional expression

If you only perform a single assignment statement in a conditional statement, you can often use the conditional expression. Not only makes the program simple, but also improves operational efficiency. Conditional operator is? And:, it is a three-mean operator, that is, three participating calculations. The general form of the conditional operator constitutes a conditional expression: Expression 1? Expression 2: Expression 3 The value rule is: if the value of the expression 1 is true, the value of expression 2 is used as a conditional expression. The value, otherwise be used as the value of the entire conditional expression with the value of the expression 2. Conditional expressions are commonly used in assigning statements. For example, conditions statement: if (a> b) max = a; else max = b; available conditional expression is MAX = (a> b)? A: B; Semantics to execute this statement are:, such as A> B is true Then give a to Max, otherwise the b is given to Max. When using conditional expressions, you should also pay attention to the following: 1. The operational priority of the conditional operator is lower than the relational operator and the arithmetic operator, but is higher than the assignment. Therefore, MAX = (a> b)? A: B can be written to max = a> B? A: B2. Conditional operator? And: Is a pair of operators, cannot be used separately. 3. The combination of conditional operators is right to left. For example: A> B? A: C> D? C: D should be understood as a> b? A: (c> d? C: d) This is the case of conditional expression nested, ie in the expression 3 It is also a conditional expression. Void main () {Int A, B, Max; Printf ("/ N INPUT TWO NUMBERS:"); Scanf ("% D% D", & A, & B); Printf ("max =% d", A> B ? a: b);} Use the conditional expression to reprogram the previous example, output large numbers in two numbers. SWITCH statement

The C language also provides another Switch statement for multi-branch selection, which is: Switch (expression) {case constant expression 1: statement 1; Case constant expression 2: statement 2; ... case constant expression N: statement n; default: statement n 1;} It is semantically: calculating the value of the expression. Compared to its constant expression value, when the value of the expression is equal to the value of a constant expression, the following statement is not performed, and then the statement after all CASE is continued. If the value of the expression is different from the constant expression after all CASE, the statement after DEFAULT is executed. Void main () {Int A; Printf ("Input Integer Number:"); Scanf ("% D", & A); Switch (a) {Case 1: Printf ("Monday / N"); Case 2: Printf "Tuesday / N"); Case 3: Printf ("Wednesday / N"); Case 4: Printf ("Thursday / N"); Case 5: Printf ("Friday / N"); Case 6: Printf ("Saturs / N "); Case 7: Printf (" sunday / n "); default: printf (" error / n ");}}

This program is required to enter a number and output an English word. However, when input 3, Case3 and all the following statements are executed, and all words and future words are output. This is of course undesirable. Why do this happen? This just reflects a feature of the Switch statement. In the Switch statement, "Case constant expression" is only equal to one statement label, the expression of the expression and a certain number of times are turned to the label execution, but cannot automatically jump out of the entire Switch statement after executing the statement of the label, so appears Continue to perform all back CASE statements. This is completely different from the IF statements described earlier, pay special attention. In order to avoid the above, the C language also provides a BREAK statement, dedicated to jump out of the switch statement, and the Break statement has only a keyword BREAK, no parameters. Also described later. The program of modifying the scrip, adds the BREAK statement after each CASE statement so that each execution can be jumped out of the Switch statement, thereby avoiding the result that should not be output. Void main () {Int A; Printf ("INPUT INTEGER NUMBER:"); Scanf ("% D", & A); Switch (a) {Case 1: Printf ("Monday / N"); Break; Case 2: Printf ("Tuesday / N"); Break; Case 3: Printf ("Wednesday / N"); Break; Case 4: Printf ("Thursday / N"); Break; Case 5: Printf ("Friday / N") Break; Case 6: Printf ("Saturday / N"); Break; Case 7: Printf ("sunday / n"); Break; default: printf ("error / n");}} When using the Switch statement Note the following points: 1. The value of each constant expression after CASE cannot be the same, otherwise an error will occur. 2. After CASE, multiple statements are allowed, can be enclosed without {}. 3. The order of each case and default clause can change without affecting the program execution. 4. DEFAULT clause can be omitted. Program example input three integers, output maximum and minimum. Void main () {Int A, B, C, Max, Min; Printf ("INPUT Three Numbers:"); Scanf ("% D% D% D", & A, & B, & C); if (A> B) {MAX = a; min = B;} else {max = B; min = a;} if (max c) min = C; printf ("max =% d / nmin =% D ", MAX, min);

In this program, first compare the size of the inputs A, B, load large numbers into the MAX, load the size into the min, then compare with C, if the max is smaller than c, then the c is given to Max; if C is less than min, Then give C to MIN. Therefore, MAX is always the maximum number, while MIN is always the minimum. The last output of MAX and MIN can be output. Calculator program. The user inputs the calculation and the four operators, and the output calculation results are output. Void main () {Float A, B, S; Char C; Printf ("INPUT Expression: A (-, *, /) b / n"); scanf ("% f% c% f", & a, & c, & b); Switch (C) {CASE ' ': Printf ("% f / n", A b); Break; Case '-': Printf ("% f / n", ab); Break; casser * ': Printf ("% f / n", a * b); Break; Case' / ': Printf ("% f / n", A / b); Break; Default: Printf ("Input Error / N" );}}} float a, b, s; char C; Printf ("INPUT Expression: A (-, *, /) b / n"); scanf ("% f% c% f", & A, & C, & B Switch (c) {case ' ': Printf ("% f / n", A b); Break; Case '-': Printf ("% f / n", ab); Break; Case '* ': Printf ("% f / n", A * b); Break; Case' / ': Printf ("% f / n", A / b); Break; default: Printf ("Input Error / N") This example can be used for four arithmetic values. The Switch statement is used to determine the operator and then output an operation value. An error message is given when the input operator is not , -, *, / /.

Cyclic structure program

The loop structure is a very important structure in the program. It is characterized in that a block is executed repeatedly when a given condition is established until the conditions are not established. A given condition is referred to as a circulating condition, and the program segment repeatedly executed is referred to as a cyclic body. The C language provides a variety of cyclic statements that can form a variety of different forms of loop structures.

WHILE statement

The general form of the While statement is: while (expression) statement; where the expression is a cycle condition, the statement is a cyclic body. The semantics of the While statement are: calculate the value of the expression, when the value is true (non-0), performs a cyclic body statement. Its execution can be represented by Figure 3-4. Statistics Enter the number of characters from the keyboard. #include void main () {int N = 0; Printf ("INPUT A String: / N"); while (getchar ()! = '/ n') N ; Printf ("% D", n);} int N = 0; Printf ("INPUT A String: / N"); while (getchar ()! = '/ n') N ; Printf ("% D", N); this program The cycle condition is getchar ()! = '/ N', its meaning is that as long as the characters entered from the keyboard are not the cycle. The cyclic body N completes the number count of the input character. Therefore, the program implements a number of characters that input a line character. Use the While statement to pay attention to the following: 1. The expression in the WHILE statement is generally a relationship expression or logical expression, as long as the value of the expression is true (non-0), it can continue loop. Void main () {Int a = 0, n; Printf ("/ n INPUT N:"); scanf ("% D", & n); while (n -) printf ("% d", A * 2) } int A = 0, N; Printf ("/ n INPUT N:"); scanf ("% d", & n); while (n -) printf ("% d", A * 2); this example The program will perform N cycles, each execution, N value minus 1. The cyclic body outputs the value of the expression A * 2. This expression is equivalent to (A * 2; A ) 2. The cyclic body, such as including more than one statement, must be enclosed in {}, constitutes a composite statement. 3. Pay attention to the choice of cycle conditions to avoid death cycles. Void main () {Int a, n = 0; while (a = 5) Printf ("% D", n );} int A, n = 0; while (a = 5) Printf ("% d", n ); In this example, the cycle condition of the WHILE statement is assigning expression a = 5, so the value of the expression is always true, and there is no means for suspending cycles in the cyclic body, so the loop will endlessly do endlessly. Form a dead cycle. 4. Allow the cyclic body of the While statement to the While statement, thereby forming a dual cycle. Do-While statement

The general form of the Do-While statement is: DO statement; while (expression); where the statement is a cyclic body, the expression is a loop condition. The semantics of the Do-While statement are: First execute the cyclic body statement once, and then discriminate the value of the expression, if it is true (non-0), continue the cycle, otherwise the loop is terminated. The difference between the Do-While statement and the While statement is that the do-while is executed before executing, so the do-while is at least one cycle. While is first judged, if the condition is not satisfied, the cyclic body statement is not executed. While statements and do-while statements generally rewrite each other. Void main () {Int a = 0, n; Printf ("/ n INPUT N:"); scanf ("% d", & n); do printf ("% d", A * 2); while (- n);} int A = 0, N; Printf ("/ n INPUT N:"); scanf ("% D", & n); do printf ("% d", A * 2); while (--n In this example, the cycle condition is changed to - N, otherwise the cycle will be executed. This is caused by the first execution. For the Do-While statement, you should also pay attention to the following points: 1. In the if statement, WHILE statement, both the expression cannot be added behind, and after the expression of the DO-WHILE statement must be added. 2. DO-While statement can also form multiple cycles, and can also be nested with the While statement. 3. When the cyclic body between DO and WHILE is composed of multiple statements, it must also be enclosed in {} to form a composite statement. 4. DO-while and while statements are replaced with each other, pay attention to modify the loop control. For statement

The FOR statement is more features provided by the C language and uses a wider cycle statement. Its general form is: for (Expression 1; Expression 2; Expression 3) statement; expression 1 is typically used to assign the cyclic variable, generally assigning expression. Allowed in the FOR statement to be transmitted to the initial value, and the expression can be omitted at this time. Expression 2 is typically a circulating condition, typically a relational expression or logical expression. Expression 3 is usually available to modify the value of the loop variable, typically an assignment statement. These three expressions can be a comma expression, that is, each expression can be composed of multiple expressions. Three expressions are optional, all can be omitted. The "statement" in general form is a cyclic body statement. The semantics of the for statement are: 1. First calculate the value of the expression 1. 2. Calculate the value of the expression 2, if the value is true (non-0), execute the cycler once, otherwise the loop is jumped out. 3. Then calculate the value of the expression 3, and turn it back to step 2. During the entire FOR cycle, the expression 1 calculates only once, expressions 2 and expressions, 3 may calculate multiple times. The cyclic body may be executed multiple times or may not be executed once. The execution process of the for statement is shown in the figure. Void main () {INT N, S = 0; for (n = 1; n <= 100; n ) s = s n; printf ("s =% D / N", s);} Calculated with for statement s = 1 2 3 ... 99 100

INT N, S = 0; for (n = 1; n <= 100; n ) s = s n; Printf ("s =% D / N", S); Expression 3 in this effer statement is N , is actually an assignment statement, equivalent to n = n 1 to change the value of the cyclic variable. Void main () {Int a = 0, n; Printf ("/ n INPUT N:"); scanf ("% D", & n); for (; n> 0; A , n -) printf ("% D ", A * 2);} Use the for statement to modify the example. Starting from 0, N will be output from N. INT A = 0, N; Printf ("/ n INPUT N:"); scanf ("% d", & n); for (; n> 0; a , n -) Printf ("% d", a * 2); In this effer statement, the expression 1 has been omitted, and the initial value of the loop variable is obtained by the scanf statement before the FOR statement, and the expression 3 is a comma expression, from a , n - two expressions. composition. Each cycle is once again 1. N self-reduction 1. A change causes the output even increment, and the change in the change of N. In the use of the for statement, you should pay attention to the following points 1. The expression infor statement can be omitted, but the semicolon interval cannot be less. Such as: for (; expression; expression) eliminates the expression 1. FOR (expression; expression) eliminates the expression 2. FOR (expression; expression;) eliminates the expression 3. FOR (;;) saves all expressions. 2. When the cyclic variable has been assigned, the expression 1 can be omitted, as in Example 3.27, it belongs to this situation. Such as omnipulates 2 or Expression 3 will cause an infinite loop, then the cycle should be ended in the cyclic body. The example belongs to this situation. Void main () {INT A = 0, N; Printf ("/ n INPUT N:"); scanf ("% D", & n); for (; n> 0;) {a ; n -; printf "% d", a * 2);}} int A = 0, n; printf ("/ n INPUT N:"); scanf ("% d", & n); for (; n> 0;) {a , N -; Printf ("% D", A * 2);} In this example, the expression 1 and the expression 3 are omitted, and the N-state of the cycle is reduced by the N-statement in the cycle, to control the number of cycles . Void main () {INT A = 0, N; Printf ("/ n INPUT N:"); scanf ("% D", & n); for (;;) {a ; n -; printf ("% D) ", A * 2); if (n == 0) Break;}} int a = 0, n; printf (" / n INPUT N: "); scanf ("% d ", & n); for (;; ) {A ; N -; Printf ("% D", a * 2); if (n == 0) Break;} This example is all omitted. The decrease of cyclic variables and the judgment of the cycle variable are achieved by the statement in the cyclic body. When the n value is 0, the loop is aborted by the BREAK statement, and the procedure after the FOR is executed. In this case, the FOR statement has been equivalent to the While (1) statement. If there is no corresponding control method in the cyclic body, a dead cycle is caused. 3. The cyclic body can be a novel statement.

#include "stdio.h" void main () {int N = 0; Printf ("Input A String: / N"); for (; getchar ()! = '/ n'; n ); printf ("% D ", N);} In this example, the expression 1 of the FOR statement is eliminating, and the expression 3 is not used to modify the loop variable, but used as a count of input characters. In this way, the count that should be completed in the cyclic body is completed in the expression. Therefore, the cyclic body is an empty statement. It should be noted that the semicolon after empty statement is not less, such as lacking this semicolon, then executes the rear printf statement as a cycler. Conversely, if the cyclic body is not a null statement, it will never add a semicolon after the brackets of the expression, so that the cyclic body is empty and cannot be executed repeatedly. These are all common mistakes in programming, and they should pay very attention. 4.FOR statement can also be nest each other with while, do-while statement, constitute multiple cycles. The following is a legally nested nested. (1) for () {... while () {...} ...} (2) do {... for () {...} ...} while (); (3) while () {... for () {...} ...} (4) for () {... for () {...}} void main () {INT I, J, K; For (i = 1; i <= 3; i ) {for (j = 1; j <= 3-i 5; J ) Printf (""); for (k = 1; k <= 2 * i-1 5; k ) {IF (k <= 5) Printf (""); Else Printf "*");} printf ("/ n");}} transfer statement

The statement in the program is usually always performed in the direction or in the direction defined by the statement. If you need to change the normal flow of the program, you can use the transfer statement introduced in this section. Four transfer statements are available in C language: goto, break, continue, and return. The return statement can only appear in the modulated function, used to return to the primary, and we will introduce in the function chapter. This section describes the first three transfusion statements.

1.goto statement

The GOTO statement is also called unconditional transfer statement, and its general format is as follows: GOTO statement label; where the statement label is written according to the identifier, it is placed in front of a statement, and the reference number (:) is column after the reference number (:). The function of the statement is identified by the identifier, and uses the goto statement. Such as: Label: I ; loop: while (x <7); C language does not limit the number of times in the program, but the respective labels must not be renamed. The semantics of the GOTO statement are the statements that change the program flow direction and rotate the statement identified by the statement label. The GOTO statement is usually used with the conditional statement. Can be used to achieve conditional transfer, constitute a loop, and jump out of the cyclic body. However, in the structured programming, it is generally not advocated using a goto statement to avoid chaos of the program process, making it difficult to understand and debugging procedures. Statistics Enter the number of characters from the keyboard. #include "stdio.h" void main () {int N = 0; Printf ("INPUT A String / N"); loop: if (getchar ()! = '/ n') {n ; goto loop;} printf ("% D", N);} int N = 0; Printf ("INPUT A String / N"); loop: IF (getchar ()! = '/ n') {n ; goto loop;} printf (" % D ", N); this example uses the IF statement and the GOTO statement to form a loop structure. When the input character is not '/ n', the N is executed, and then transfer to the IF statement cycle execution. The loop is stopped until the input character is '/ n'. BREAK statement

The BREAK statement can only be used in a switch statement or a loop statement, and its role is to jump out of the switch statement or jump out of this layer loop, and rotate the execution later. Since the direction of transfer of the BREAK statement is clear, the statement label is not required to cooperate with it. The general form of the Break statement is: BREAK; the BREAK statement is used in the Switch statement and the FOR statement in the above example. Using the BREAK statement can make the loop speech with multiple exits, making programming more flexible and convenient under some occasions.

Continue statement

The Continue statement can only be used in the cyclic body, and its general format is: Continue; its semantics is: End this cycle, that is, the statement after the CONTINUE statement in the cycle is not executed, and the judgment and execution of the next cycle condition are transferred. It should be noted that this statement only ends the cycle of this layer, does not jump out of the loop. Void main () {Int n; for (n = 7; n <= 100; n ) {if (n% 7! = 0) Continue; Printf ("% D", n);}} output 100 can be used 7 Take the number. INT N; for (n = 7; n <= 100; n ) {IF (n% 7! = 0) Continue; Printf ("% D", N);} In this example, each of 7 to 100 The number is tested. If the number cannot be divided by 7, that is, the mode is not 0, and the next cycle is turned by the CONTINUS statement. Only when the model is 0, the following printf statement can be performed, and the output can be removed by 7. #include "stdio.h" void main () {Char A, B; Printf ("Input A String: / N"); b = getchar (); while ((a = getchar ())! = '/ n' ) {if (a == b) {Printf ("Same Character / N"); Break;} B = a;}} Checking the same line in the input line. CHAR A, B; Printf ("Input A String: / N"); b = getchar (); while ((a = getchar ())! = '/ n') {if (a == b) {printf "Same Character / N"); Break;} b = a;} In this case, the first read character is sent to B. Then enter the loop, read the next character into A, compare the A, B is equal, and if the phase is equal, the prompt string is output and the cycle is output. If it is not equal, the character in A is given to B, and the next cycle is input. The number of prime numbers within 100 is output. The number of prime is only 1 and itself. Use the exhaustive method to determine if a number is the number of prime. Void main () {INT N, I; for (n = 2; n <= 100; n ) {for (i = 2; i = n) Printf ("/ T% D", N);}} INT N, I; For (n = 2; n <= 100; n ) {for (i = 2; i = n) Printf ("/ t% d", n);} In this case, the first layer of cycle represents the 100 numbers of 1 to 100. Whether it is the number of prime, a total of 100 times, in the second layer cycle, the logs are removed by 2 to n-1, and if the except is except, the layer loop is jumped, and it is not the number of prime numbers. If all the numbers are ended, the cycle is ended, then the number of digits, there is I> = n, so the number of output probes can be determined by this. Then transfer to the next large cycle. In fact, all even numbers of 2 or more are not prime numbers, so that the step size of the cyclic variable can be changed to 2, i.e., each time it increases 2, and only the number of Number N is removed by 2 to n can determine whether the number is considered. This will greatly reduce the number of cycles and reduce program runtime.


New Post(0)