C language selection structure and loop structure

Keywords: C Back-end

1, Select structure

1. if select statement

(1) Single branch if statement

Syntax structure:  

if(Conditional judgment expression){
        sentence;
}

If the value of the conditional judgment expression is true, execute the statement; If false, the statement is not executed.

Example: enter two integers and output the largest of the two numbers.

#include <stdio.h>
int main(){
    int a,b;
    printf("Please enter two integers:\n");
    scanf("%d%d",&a,&b);
    if(a>b){                     //Judge whether a > b
        printf("The maximum number is%d",a);
    }
    if(a<=b){                    //Judge whether a < = b
        printf("The maximum number is%d",b);    
    }
    return 0;
}

Operation results

(2) Double branch if...else statement

Syntax structure:

if(Conditional judgment expression){
    Statement 1;
}
else{
    Statement 2;
}

If the value of the conditional judgment expression is true, execute statement 1; otherwise (the value of the conditional judgment expression is false), execute statement 2.

else can only be used with if, not alone.

  Example: enter an integer to judge whether the number is positive or negative.

#include <stdio.h>
int main(){
    int x;                       //Define integer variable x
    printf("Please enter an integer:");
    scanf("%d",&x);
    if(x>=0){                    //Judge whether x > = 0
        printf("%d Is a positive number",x);    //Execute the statement when x > = 0, not else
    }else{                                        
        printf("%d Is a negative number",x);
    }
    return 0;
}

Operation results 

(3) Multi branch if statement (nested)

In an if...else statement, the if branch or else branch can be an IF statement or if...else statement, which is called nesting of if statements or multi branch if statements. The if...else statement is applicable to the operation of judging two conditions.

If there are multiple conditional branch judgments in the program structure, you can use the multi branch if statement.

2. switch statement

A switch statement is a multi branch selection statement.

Syntax structure:

switch(expression){
    case Constant expression 1: Statement 1; break;
    case Constant expression 2: Statement 2; break;
    ·    ......
    ·    ......
    ·    ......
    case Constant expression n: sentence n; break;
    default:Default statement;
}

The expression after switch is the condition to execute the judgment, and then use the case keyword to represent various constant expressions that meet the inspection conditions, and the subsequent statements are corresponding operations. There is also a default keyword, which is used to execute the default statement after default if no conditions are met.

The value of the expression after switch is compared with the constant expression after case. Both execute the statement after case at the same time.

If there is a break statement after the statement corresponding to case, the program jumps out of the switch statement; If there is no break statement, the statement corresponding to the next case will continue to be executed.

Example: use the numbers 1 ~ 7 to represent Monday to Sunday. According to the input numbers 1 ~ 7, the corresponding week value is output.

#include <stdio.h>
int main(){
	int week;
	printf("Please enter the number 1~7: \n");
	scanf("%d",&week);
	switch(week){
		case 1:printf("Monday"); break;
		case 2:printf("Tuesday"); break;
		case 3:printf("Wednesday"); break;
		case 4:printf("Thursday"); break;
		case 5:printf("Friday"); break;
		case 6:printf("Saturday"); break;
		case 7:printf("Sunday"); break;
	default:printf("Please enter 1-7 Number between"); 
    }
    return 0;
}

Operation results 

2, Cyclic structure

  1. while statement

The while loop statement belongs to the current loop, that is, judge the condition first, and then execute the loop body statement.

Using the while statement can solve the problem of repeatedly executing a loop body when a condition is met.

Syntax structure:

while(expression){
    Statement block(Circulatory body)
}

While statement first checks the condition of the expression. If the condition is true, execute the statement block (loop body). Each time the loop is executed, the program will return to the while statement to re verify whether the conditions are met. If the conditions are not met, skip the while statement and directly execute the following program code.

Example: use the while statement to output 50“  * ”  .

#include <stdio.h>
int main(){
    int i=50;            //Define i equal to 50
    while(i>0){          //Judge that i is greater than 0
        printf("*");
        i--;             //Recycle after self reduction until i is not greater than 0
    }
    return 0;
}

Operation results 

2. Do while statement

The do while loop statement belongs to the until loop. This type of loop first executes the loop body once, and then makes a conditional judgment on whether to execute the next loop body.

Syntax structure:

do{
    Statement block(Circulatory body)    
}while(expression);

The do while statement is executed in this way. First, execute the contents of the loop body sentence once, and then judge the expression. When the expression is true, return to re execute the loop body statement, and then judge the expression again until the value of the expression is false. At this time, the loop ends.

For example, 10 students in a group in a class have an exam. Now we want to count the total score and average score of the group.

#include <stdio.h>
int main(){
	float a=1,sum=0,score;
	printf("Please enter the scores of 10 students:\n");
	do{
		scanf("%f",&score);
		a++;
		sum+=score;    //Accumulate
	}while(a<=10);     //Cycle condition
	printf("Total score:%.2f\t average:%.2f",sum,sum/10);
	return 0; 
} 

Operation results 

3. for statement  

for statement is a convenient, flexible and powerful loop statement. for statement can be used when the number of loops is determined, or when the number of loops is uncertain but the loop end condition is given.  

Syntax structure:  

for(Initial value of cyclic variable;Cycle condition;Cyclic variable change){
        Statement block
}

(1) Execute loop variable initialization.

(2) Execute the loop condition. If the condition value is true, execute the statement block; if false, end the loop.

(3) Execute the loop variable change, and then return to step (2) to continue.

Example: output a number from 1 to 100.

#include <stdio.h>
int main(){
	int a;
	for(a=1;a<=100;a++){    //Cycle from 1 to 100 and output a for each cycle
		printf("%d\n",a);
	}
}

Operation results: 1 2 3 4 5 6 7 8 9....... 95 96 97 98 99 100

 

Dead cycle:

Loops that cannot be terminated are often called dead loops or infinite loops.

For example, the following code:

#include <stdio.h>
int main(){
	int a;
	for(a=1;a>0;a++){
	printf("6");	
    }
} 

If a is always greater than 0, it will always cycle output 6 (be careful not to cycle too long to avoid crash and other problems)

Posted by Kane250 on Sun, 28 Nov 2021 02:04:29 -0800