Selection structure in C language - if statement/if...else statement

 

overview

C language supports the most basic three kinds of program operation structures: sequence structure, selection structure, and loop structure.

  • Sequential structure: Programs are executed sequentially without jumps.
  • Selection structure: According to whether the conditions are met, the corresponding functions are selectively executed.
  • Loop structure: Depending on whether the condition is met, a certain piece of code is executed multiple times in a loop.

Today, let's discuss selection structures in detail.

select structure 

 if statement

The if statement is one of the most commonly used statements in the C language, and the meaning it expresses to us is also very simple-if.

The logic flow chart is as follows:

 After reading it, we can clearly know that the if statement is a statement with a selection and filtering function, and it also has a selection and filtering function.

The format of the if statement is as follows:

if (expression) statement 1

The expression in the above brackets will be calculated first to determine whether to execute statement 1 next. If its value is true (that is, not equal to 0), then statement 1 will be executed. If false, statement 1 will be skipped directly.

 The if statement syntax is as follows:

if(boole_a_expression)
{
   /* 如果布尔表达式为真将执行的语句 */
}

Code example:

#include <stdio.h>

int main()
{
	int a = 10;
	int b = 20;

	if (a > b)    //给出判断a是否大于b
	{
		printf("%d\n", a);    //如果a大于b 则执行该语句,反之跳过即可。
	}

	return 0;
} 

if...else statement 

We just read the if statement earlier, and know that the if statement is a statement with a selection function, then it is not difficult for us to understand the if...else statement.

The if...else statement means if...then...otherwise...then..., that is, if there is only an if statement, it will be skipped directly if it is not true, but it will be followed by an else If it is not established, the statement after else will be executed.

The logic flow chart is as follows:

 

 The format of the if ...else statement is as follows:

if (expression) statement 1  

         else statement 2 

The expressions in the parentheses above will be evaluated first to determine whether statement 1 or statement 2 will be executed next. The expression must have scalar type. If its value is true (that is, not equal to 0), then statement 1 will be executed. If false, statement 2 will be executed. 

Note : The C language assumes any non-zero and non-null value to be true, and zero or null to be false.

  The if ... else statement syntax is as follows:

if(boole_a_expression)
{
   /* 如果布尔表达式为真将执行的语句 */
}
else
{
   /* 如果布尔表达式为假将执行的语句 */
}

code example

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;

	if (a > b)    //判断
	{
		printf("%d\n", a);    //如果a>b 则执行此语句
	}
	else
	{
		printf("%d\n", b);    //如果a<b 则执行此语句
	}
	return 0;
}

 

 if...else if...else statement

As for the if...else if...else statement is a process of continuous nesting, that is, the result can be divided into multiple branches, and it is only necessary to ensure that one if corresponds to one else.

Here we can understand that if a statement is executed, statement 1 is executed after it is judged to be true, and if it is judged to be false, continue to judge whether it meets another condition, if another condition is met, statement 2 is executed, and if it is not satisfied, it can continue to extend go down.

An  if  statement can be followed by an optional  else if...else  statement, which can be used to test multiple conditions.

When using the if...else if...else statement, the following points should be noted:

  • An if can be followed by zero or one else, else must be after all else if.
  • One if can be followed by zero or more else if, else if must be before else.
  • Once an else if matches successfully, other else if or else will not be tested.

The logic flow chart is as follows:

 if...else if...else syntax is as follows:

if(bool_a_expression 1)
{
   /* 当布尔表达式 1 为真时执行 */
}
else if( bool_a_expression 2)
{
   /* 当布尔表达式 2 为真时执行 */
}
else if( bool_a_expression 3)
{
   /* 当布尔表达式 3 为真时执行 */
}
else 
{
   /* 当上面条件都不为真时执行 */
}

code example

#include <stdio.h>

int main()
{
	unsigned int a;
	scanf("%u", &a);

	if (a < 10)
	{
		printf("个位\n");  
	}
	else if (a < 100)
	{
		printf("十位\n");
	}
	else if (a < 1000)
	{
		printf("百位\n");
	}
	else
	{
		printf("很大\n");
	}

	return 0;
}

These are the relevant knowledge of the selection structure in C language - if statement/if...else statement. Of course, the selection structure is not only the if statement. Next, I will introduce the switch statement and the ternary operator. If you are interested Friends can subscribe to my C language column to get the update progress.

Guess you like

Origin blog.csdn.net/qq_62464995/article/details/129587833