first, sequential structure
- The default process structure is executed in the order of writing;
ii. choice of structure
- Concept: To judge a given condition and determine the execution of the code according to the result of judgment;
1. If statement
- Basic use of the IF statement
/*the first structure of the 1.if if (condition) {statement 1; Statement 2; ...... } The 2nd structure of the 2.if if (condition 1) {statement 1; Statement 2; ...... } else {statement 3; Statement 4; ....... } The 3rd structure of the 3.if (in order to determine the conditions, encountered a condition to execute its code (also stating that the condition before all the conditions are not established), the execution of the condition is no longer judged) if (condition 1) {statement 1; Statement 2; ....... } else if (condition 2) {statement 3; Statement 4; ....... } else if (condition 3) {statement 5; Statement 6; ....... } else {//The preceding conditions are not set up to come here Statement 7; Statement 8; ....... } The 4th structure of the 4.if if (conditional) statement 1; The condition is set to execute statement 1 statement 2; Statement 2 is not related to the IF statement and must be executed ....*/#include<stdio.h>intMain () {/*//Exercise 1 int count = 40; if (Count >) {printf ("Classes!!! \ n "); printf ("Prepare machine!!! \ n "); } else {printf ("No classes!!! \ n "); printf ("Not ready for machine!!! \ n "); }*/ /*//Exercise 2 int a = 5; if (a==10) {printf ("a=10\n"); } else if (a==5) {printf ("a=5\n"); } else if (a==3) {printf ("a==3\n"); } else {printf ("a= other value \ n"); }*/ /*//Exercise 3 int a = 10; if (a++ > 10)//First compare a=10 with 10 (false), plus 1 is a=11 {printf ("a\n"); } else if (a++ > 5)//At this time a=11, first compare the a=11 with 5 (true, the following conditions will no longer run, judge,) and then let a plus 1 that is a=12 {printf ("b\n"); } else if (a++ > 3)//Because condition 2 is set up, the code is no longer running, judging, a does not add 1 operation {printf ("c\n"); } else {printf ("d\n"); } printf ("A's value is%d\n", a); The result of the operation is: B A is a value of*/ //Exercise 4 intA =Ten; if(A > One)//conditions are not established, will not be executed haha, but still will be executed Hey Heyprintf"haha, haha \ n"); printf ("Hey, hi, black \ n");//This statement has no relation to the IF statement and will inevitably execute//The result is: Hey Hei Hei return 0;}
Trap one: When comparing the size, the constants are placed on the left and the variables on the right
int 10431 ; if 0 // if one less write = , that is: a = 0, the equivalent of assignment operation, and no error, no reminder!!! {statement 1;} Else {statement 2;} /* Summary: When comparing the size, the constant is placed on the left side, the variable is placed on the right side: if (0 = = a) if less write a =, that is: 0 = A; At this time because the constant can not be assigned, will error alert!!! */
Trap Two: note the assignment operator = Do not write = =
int Ten If you write more than one =, that is: a = = 15; At this time there is a warning, there is no error, but did not assign a value to a 15,a or equal to ten;printf ( " %d " , a); /* Summary: Note the assignment operator = Do not write = =, the consequences are serious! */
Trap three: Do not write the semicolon after the IF
if (56// write a semicolon; causes the code inside the curly braces to be a block of code that will inevitably execute {printf ("a " );} // Summary: Do not write a semicolon after the IF
Trap Four: Scope issues
if (6); { int5// variable A is scoped to "if" inside curly braces } //variable A is then destroyed in memory by printf ("%d", a); // Explanation: A scope problem: A is valid only within curly braces, and when executed to the printf function, the variable A has been reclaimed, and an error has been made: (Error:use of undeclared identifier ' a '), using an undefined identifier;
Trap Five: Scope ambiguous
#include <stdio.h>
Int main ()
{
If (10 > 6 ) int a = 5 ;
// Span style= "color: #008000;" >// summary: If you want to define a new variable in the code following the IF statement, you must extend the curly braces
Dark Horse Programmer----Process Control (sequential structure, selection structure, loop structure)