The third day of learning C language: branch statement (if - else if - else)

1.C language statement structure

        C language is a structured programming language. The structure here refers to the sequential structure, selection structure, and loop structure. C language can realize these three structures. In fact, if we analyze it carefully, everything we see every day can be Split into these three structures or a combination of these three structures.

        We can use if and switch to implement branch structures, and use for, while, and do while to implement loop structures.

2.if statement

        2.1. The syntax form of the if statement is as follows:

if (表达式)
    语句

        The if statement is true: if the expression is true (true), the statement is executed; if the expression is not true (false), the statement is not executed.

        In C language, 0 means false, and non-0 means true. That is, if the result of the expression is 0, the statement will not be executed. If the result of the expression is not 0, the statement will be executed.

        Next, write an example:

// 输⼊⼀个整数,判断是否为奇数
int main()
{
	int i = 0; // 初始化
	scanf("%d", &i); // 输入值

	if (i % 2 == 1) // 表达式
	{
		printf("是奇数!"); // 语句
	}

	return 0;
}

Results of the:

 flow chart:

        2.2.The syntax form of the else statement is as follows:

if (表达式)
    语句1
else
    语句2

        Next, write an example:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>


// 输入一个整数,判断是否为奇数,如果是奇数打印是奇数,否则打印数偶数。
int main()
{
	int i = 0; // 初始化
	scanf("%d", &i); // 输入整数

	if (i % 2 == 1) // 如果 i 模 2 等于 1的 就是奇数
	{
		printf("%d是奇数!", i); 
	}
	else // 如果不是等于 1的,就是偶数
	{
		printf("%d 是偶数!", i);
	}

	return 0;
}

        operation result:

      2.3. Branches contain multiple statements

        By default, in if and else, only one statement is controlled by default, such as:

# include <stdio.h>

int main()
{
    int age = 0;
    scanf("%d", &age);

    if(age >= 18)
        printf("成年了!\n");
        printf("可以谈恋爱了!\n");
    
    return 0;
}

  

         In the above code, you will find that whether the input value is >=18 or less than 18, "You can fall in love" will be printed on the screen.

        This is because the if statement can only control one statement, which is printf("I'm an adult\n");. If the if statement is true, it will print the adult. If the statement is false, it will not be printed. For printf("You can fall in love\n"); n"); exists independently and will be executed regardless of whether the condition of the if statement is true or false. Then if we want the if statement to control 2 statements at the same time.

What should we do? Then we need to enclose the code using else or followed by curly brackets. as follows:

int main()
{
    int age = 0;
    scanf("%d", &age);

    if (age >= 18) //if 后使⽤{} 控制多条语句-这个块也叫:程序块,或者复合语句
    {
        printf("成年了!\n");
        printf("可以谈恋爱了!\n");
    }
    else // else 后使⽤{}控制多条语句-这个块也叫:程序块,或者复合语句
    {
        printf("未成年!\n");
        printf("不可以早恋哦!\n");
    }
        

    return 0;
}

         2.4. Nested if

                In an if else statement, else can be used with another if statement to form multiple judgments.

                Example: It is required to input an integer and determine whether the input integer is 0, a positive number or a negative number.

int main()
{
	int i = 0;
	scanf("%d", &i);

	if (i == 0)
	{
		printf("%d是零\n", i);
	}
	else if (i > 0)
	{
		printf("%d是正数\n", i);
	}
	else
	{
		printf("%d是负数\n", i);
	}

	return 0;
}

 

        2.5. The dangling else problem

               If there are multiple if and else, i can remember such a rule, else always matches the closest if

               Look at the code below:

#include <stdio.h>

int main()
{
	int a = 0;
	int b = 0;

	if (a == 1)
		if (b == 2)
			printf("hehe\n");
	else
		printf("haha\n");

	return 0;
}

        What does this code output?

        In fact, nothing is output. A mistake that beginners will make is to judge that a is 0 and not equal to 1, then execute the else clause and print haha, but when you run it, you will find that there is nothing.

         why?

        Because this is the problem of dangling else, if there are multiple if and else, you can remember this rule, else always matches the closest if.

        The above code layout aligns else with the first if statement, which makes us think that else matches the first if. When the if statement is not true, the natural thing to think of is to execute the else clause and print haha, but in fact The above else is matched with the second if, so that the following if...else statements are nested in the first if statement. If the first if statement does not hold, there will be no chance of nesting if and ese. When executed, nothing is printed in the end.

        Then let’s improve it as follows:

#include <stdio.h>

int main()
{
	int a = 0;
	int b = 0;

	if (a == 1)
	{
		if (b == 2)
			printf("hehe\n");
		else
			printf("haha\n");
	}

	return 0;
}

        If you want else to match the first if, you can change it to this, as follows:

#include <stdio.h>

int main()
{
	int a = 0;
	int b = 0;

	if (a == 1)
	{
		if (b == 2)
			printf("hehe\n");
	}
	else
	{
		printf("haha\n");
	}

	return 0;
}

        As long as you bring appropriate braces, the logic of the code will be clearer, so everyone should pay attention to the use of braces when writing code in the future to make the code more readable.

        Note: When writing code, formatting is very important. It will make it easier for you to understand it later, and it will also make it easier for you to join the company. It will go smoothly when your colleagues come to read your code, and you must develop your own coding style.

Guess you like

Origin blog.csdn.net/m0_58724783/article/details/132027121