[C language] Detailed explanation of if statement

C Language Elementary Series

Branching Statements and Looping Statements (1)


Table of contents

C Language Elementary Series

foreword

One, what is a statement?

1.1 How to understand the sentence?

Second, the branch statement (selection structure)

2.1, if statement

2.2, Wrong conditional writing of if statement

2.2, the correct conditional writing of the if statement

2.3, single branch structure

2.4, double branch structure

2.5, multi-branch structure        

2.5.1 can be realized, using a lot of nesting but very bloated writing

2.5.2 Concise writing

2.5.2 A more concise way of writing

2.6, the if statement can only control one statement behind it by default

 2.7, develop good coding habits

2.7.1 A more understandable code style 

if statement tips

2.8, example if statement

2.8.1. Determine whether a number is odd

2.8.2. Output odd numbers between 1 - 100

Summarize


foreword

We learn C language, the adventure of growing from a small boy to a master, we will initially explore the knowledge level of the if statement in this chapter


One, what is a statement?

C language statements can be divided into the following five categories:
1. Expression statement
2. Function call statement
3. Control statement
4. Compound statement
5. Empty statement

1.1 How to understand the sentence?

We'll use code and comments, and and run schematics:

int main()
{
	3 + 5;//这就是一个表达式语句
	printf("hehe\n");//我们想用printf函数打印hehe这个信息,调用这个函数,
	//完成之后给个分号,这就是函数调用语句
	//语句就是大部分情况下一个分号隔开的就是一个语句
	;//简单的放一个分号,这就是一个语句,但是这个语句什么都不干,这叫空语句
	//空语句的应用场景,有时候我们需要一条语句,但是这条语句什么都不需要做,就可以使用空语句
	return 0;
}

Control statements are used to control the execution flow of the program to realize various structural modes of the program. They are composed of specific statement definers.
There are nine kinds of control statements in C language.
Can be divided into the following three categories:
1. Conditional judgment statement is also called branch statement: if statement, switch statement;
2. Loop execution statement: do while statement, while statement, for statement;
3. Turning statement: break statement, goto statement, continue statement, return statement

Second, the branch statement (selection structure)

The branch statement is a selection structure for our C language. In order to realize this selection structure,
the C language provides the grammatical feature of the branch language

2.1, if statement


It is commonly used in C language monads, and how to write such a statement form of if statement that can realize branching ,

To explain:
If the expression evaluates to true, the statement executes.
How to express true and false in C language?
0 means false, non-zero means true.

2.2, Wrong conditional writing of if statement

We'll use code and comments, and and run schematics:

//错误写法
int main()
{
	int age = 0;
	scanf("%d", &age);
	if (18<= age <=30)
	//表达式先判断age>=18,
	//如果age是50的话,18小于age,这个条件为真,为真就等于1
	//1又小于等于30,所以这个地方就打印青年了
	//不是两端去判断age在这个范围内,它是先执行18<= age,在执行age <=30
	printf("青年\n");
	return 0;
}

2.2, the correct conditional writing of the if statement

We'll use code and comments, and and run schematics:

正确写法
int main()
{
	int age = 0;
	scanf("%d", &age);
	if ( age >=18 && age <= 30)
	printf("青年\n");
	return 0;
}

2.3, single branch structure

Single-branch syntax structure:
if (expression)  
statement;

Explanation:
Put an expression in the small parentheses behind the if statement.
If the result of the expression is true, the statement will be executed. If the result of the expression is false , it will not be executed.
In C language: 0 means false, and non-zero means true.

We'll use code and comments, and and run schematics:

//单分支的场景
//只处理符合条件的
int main()
{
	//如果年龄大于等于18 -- 成年
	//小于18,不管他
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
	{
		printf("成年人");
	}
	return 0;
}

2.4, double branch structure

Double-branch syntax structure:
if (expression)
statement 1;
else
statement 2;

Explanation:
If if the expression is true, execute statement 1.
When the expression is false, I also want to deal with it. I can use else to execute statement 2.
Either you enter from the if statement and execute statement 1. If not, execute the else statement and execute statement 2. Choose one of the two

We'll use code and comments, and and run schematics:

int main()
{
	// 有if有else你不行就我来
	//如果年龄大于等于18 -- 成年人,否则就打印 -- 未成年人
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
	{
		printf("成年人");
	}
	else
	{
		printf("未成年人\n");
	}
	return 0;
}

2.5, multi-branch structure

Multi-branch syntax structure:    
if (expression 1)
statement 1;
else if (expression 2)
statement 2;
else
statement 3;

Explanation:
if the expression statement, if the expression statement is true, execute statement 1,
if not, else if judges the following expression 2,
if the statement of expression 2 is true, then execute statement 2
, if the statement of expression 2 is not true, then only execute statement
3 of else.

Example of multi-branch structure:
 we use multi-branch conditions
age <18 - underage
18-30 - youth
31-50 - middle-aged
51-70 - middle-aged and elderly
 71-99 - old
 > 99 - old birthday


2.5.1 can be realized, using a lot of nesting but very bloated writing

We'll use code and comments, and and run schematics:

int main()
{
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
	{
		printf("未成年\n");
	}
	else//else如果是整体的一个逻辑的话,大于等于18,
		//else里面有很多种类的情况,里面也可以细分
	{
		if (age >= 18 && age <= 30)//如果大括号里面只有一条语句。就可以将大括号去掉
		{
			printf("青年\n");//if  else这个整体合起来是一条语句
		}
		else
		{
			if (age >= 31 && age <= 50)
			{
				printf("中年\n");
			}
			else
			{
				if (age >= 51 && age <= 70)
				{
					printf("中老年\n");
				}
				else
				{
					if (age >= 71 && age <=99 )
					{
						printf("老年\n");
					}
					else
					{
						printf("老寿星\n");
					}
				}
			}
		}
	}
	return 0;
}

 2.5.2 Concise writing

 We'll use code and comments, and and run schematics:

int main()
{
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else if (age >= 18 && age <= 30)
		printf("青年\n");
	else if (age >= 31 && age <= 50)
		printf("中年\n");
	else if (age >= 51 && age <= 70)
		printf("中老年\n");
	else if (age >= 71 && age <=99 )
		printf("老年\n");
	else
		printf("老寿星\n");		
	return 0;
}

  2.5.2 A more concise way of writing

 We'll use code and comments, and and run schematics:

//因为有前置条件可以在else if中写出一种条件就行 
//但是条件顺序不能变动,变动就会逻辑错误
int main()
{
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else if ( age <= 30)
		printf("青年\n");
	else if ( age <= 50)
		printf("中年\n");
	else if ( age <= 70)
		printf("中老年\n");
	else if ( age <= 99)
		printf("老年\n");
	else
		printf("老寿星\n");
	return 0;
}

2.6, the if statement can only control one statement behind it by default

 We'll use code and comments, and and run schematics:

int main()
{
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
	    printf("成年了\n");
	    printf("谈恋爱");//独立的,不被if控制
	return;
}

If you want the if statement to be controllable, you need to add a restriction
and add a curly brace after the if, so that the if statement is controlled

A curly brace corresponds to a code block

int main()
{
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
	{//代码块
		printf("成年了\n");
		printf("谈恋爱");
	}
	return;
}

 2.7, develop good coding habits

It is necessary to develop a good code style so that others can understand it.
A code style that is easy for everyone to read wrong 

We'll use code and comments, and and run schematics:

int main()
{
	int a = 0;
	int b = 2;
	if (a == 1)
		if (b == 2)
			printf("hehe\n");
	else
		printf("haha\n");
	return 0;
}
//看代码感觉结果是haha
//但是结果是不打印
//else是与最近的if相匹配的,这样第一if进入了才能执行if里面的if和else
//要写出让人容易理解的代码风格

//更容易看懂的代码风格
int main()
{
	int a = 0;
	int b = 2;
	if (a == 1)
	{
		if (b == 2)
			printf("hehe\n");
		else
			printf("haha\n");
	}
	return 0;
}

2.7.1 A more understandable code style 

We'll use code and comments, and and run schematics:

if语句的书写风格
 容易误导的写法
if的条件满足就是return x,不满足就返回return y。
if (condition) {
    return x;
}
return y;
让人一目了然的写法
if (condition)
{
    return x;
}
else
{
    return y;
}
在不影响效率的情况下我们要写那种让人一目了然的代码

if statement tips

We'll use code and comments, and and run schematics:

int main()
{
	int a = 0;
	//if(a == 5)//这样写少写一个=号,不易发现,没有报错
	if (5 == a)//当我们写少写一个=号,就会报错,易发现错误
		//未来我们想判断一个变量和一个常量值是否相等的话,
		//可以把常量放在等号的左边,这样即使少写了一个等号,就会发现错误,改正
		printf("hehe\n");
	return 0;
}

2.8, example if statement

2.8.1. Determine whether a number is odd

We'll use code and comments, and and run schematics:

int main()
{
	int n = 0;
	scanf("%d", &n);//只有变量才能赋值,表达式是不能被赋值的,
	//少写一个等号就会直接报错的
	if (n % 2 == 1)//通过模2得到的余数为1的,都是奇数
	{
		printf("YES\n");
	}
	else
		printf("NO\n");
		return 0;
}

 2.8.2. Output odd numbers between 1 - 100

We'll use code and comments, and and run schematics:

Use branching statements to solve problems:

int main()
{
	int i = 1;//循环变量
	while (i <= 100)
	{
		if (i % 2 == 1)//通过模2得到的余数为1的,都是奇数
			printf("%d ", i);//%d后面加一个空格是为了,让我们看的跟清晰
		i++;
	}
	return 0;
}

 Solve the problem without branching statements:

int main()
{
	int i = 1;//循环变量
	while (i <= 100)
	{
		printf("%d ", i);//%d后面加一个空格是为了,让我们看的跟清晰
		i+=2;
	}
	return 0;
}


Summarize

The above is what I want to talk about today. This article only comprehensively introduces the if statement in C language, which can make your adventure in C language more interesting and fulfilling.

Guess you like

Origin blog.csdn.net/weixin_73466540/article/details/131775990