Small bai notes: c language branch statements and loop statements

Posted by tpl41803 on Sun, 02 Jan 2022 14:43:01 +0100

Preface: I am also Xiaobai. This is my note sorting in my study. I hope it can help more people. If there are mistakes, you are welcome to correct them. If you don't understand them, you are also welcome to communicate with each other in the comment area and make common progress. In order to save space, the code in the paragraphs in the article only writes the general meaning, completely without considering the grammar and norms. We must not write code like that (manual funny).

1 statement

1.1 what is a statement

In C language programming, each statement separated by a semicolon is a statement

return 0;
1 + 2;
printf("haha,c Language!");

2 branch statement

2.1 what is a branch statement: that is, a selection statement, which needs to represent the sentence pattern used when selecting different results according to different conditions

2.2if else statement

2.2.1 determine whether you are adult or not according to your age: (Note: if else statements need not construct code blocks when there is only one statement, and code blocks must be constructed when there are multiple statements, that is, wrap all statements into one code block with {}, otherwise compile and report errors. It is recommended that you form a good habit to wrap them with {} even if there is only one statement)

int main()
{
    int a = 10;
    if(a < 18)
        {
            printf("under age");
        }
    else
        {
            printf("adult");
        }
    return 0;
}

2.2.2 when more than two types are selected, except else if is used in the middle of the beginning and end. In particular, the intermediate conditions cannot be written directly (18 < = a < 28). Due to the sequence of code logic, it will first determine whether a is less than or equal to 18, and the obtained result is true or false, which is 1 or 0 in C language. In this way, it will use 1 or 0 to determine whether it is less than 28, The result must be true, so that no matter how much the age is, only youth will be output. The correct method is to connect the conditions with "& &". The logical expression is that it is true only when it is satisfied at the same time, for example:

int main()
{
    int a = 10;
    if(a < 18)
        {
            printf("under age");
        }
    else if(a >= 18 && a < 28)
        {
            printf("youth");
        }
    else
        {
            printf("adult");
        }
    return 0;
}

//Another logic of thinking
int main()
{
    int a = 10;
    if(a < 18)
        {
            printf("under age");
        }
    else
        {
            if(a >= 18 && a < 28)
                {
                    printf("youth");
                }
            else
                {
                    printf("adult");
                }
        }
    return 0;
}

2.2.3 little knowledge point 1: the logic in C language is true or false, 0 is false, as long as it is not 0, it is true, and the default output data of C language is 1.

2.2.4 little knowledge point 2: else dangling: else matches the nearest unmatched if. In the following code, the first one is suspended due to the matching of else and the second if, and the result is no output (a is not equal to 0 and will not enter the following if else statement directly, so again, the construction of code blocks is an important part of code normalization)

//If you want to write code to realize a = 1 and B = 2, output haha, otherwise output hehe

//else is suspended, resulting in no output
int main()
{
    int a = 1;
    int b = 2;
    if(a == 0)
        if(b == 1)
            printf("haha\n");
    else
        printf("hehe\n");
    return 0;
}


//Correct writing:
int main()
{
    int a = 1;
    int b = 2;
    if(a == 0)
	{
		if (b == 1)
		{
			printf("haha\n");
		}
	}
	else
	{
		printf("hehe\n");
	}

    return 0;
}

2.2.5 little knowledge point 3 code style optimization: the indentation, the space, and the alignment. Since "=" is an assignment, and "= =" is an equal judgment, in order to prevent the if statement from writing an equal judgment condition into an assignment, it is recommended that the constant be written in front. In this way, if you accidentally write it wrong, the compiler will report an error. a == 5 is optimized to write it as 5 == a. when it is written as 5 = a, the compiler will report an error.

2.3 switch case statement

2.3.1 switch case statement is a branch statement with multiple choices. The sentence pattern can be executed by case through similar enumeration to simplify the use of else if, for example:

int main()
{
	int day = 0;
	printf("please enter a number\n");
	scanf("%d", &day);
	switch (day)
	{
	case 1:
		printf("Monday\n");
		break;
	case 2:
		printf("Tuesday\n");
		break;
	case 3:
		printf("Wednesday\n");
		break;
	case 4:
		printf("Thursday\n");
		break;
	case 5:
		printf("Friday\n");
		break;
	case 6:
		printf("Saturday\n");
		break;
	case 7:
		printf("Sunday\n");
		break;
	default:
		printf("Illegal input!\n");
		break;
	}
	return 0;
}

2.3.2 note 1: the switch statement must be an integer (int short defined) expression in parentheses. It can be an expression, such as: {int a, int b, switch(a + b)} but it cannot be an integer, such as: {float a, char b, switch(a), switch(b), switch(a +b)}

2.3.2 note that 2: case can only be followed by an integer constant expression, which can be {const b = 3; case:1; case:2 * 3; case:b;} Cannot be {int a = 1; case:a;}

2.3.3 note 3: break jumps out of the switch statement. When the break is not executed in the switch statement, all statements behind the case entry will be executed in turn. For example, when the first code enters 3, it will output "Wednesday" and "Thursday" "Sunday" and "illegal input". Therefore, it is necessary to choose whether to need a break statement according to logical thinking. For example, the second code flexibly uses break to input 1 to 5 to output working days and 6 to 7 to output rest days. (logically, the break statement of the last case is dispensable, but for later code maintenance, it is recommended to develop good habits and not omit the break statement of the last case)

int main()
{
	int day = 0;
	const int a = 1;
	printf("please enter a number\n");
	scanf("%d", &day);
	switch (day)
	{
	case a:
		printf("Monday\n");
	case 2:
		printf("Tuesday\n");
	case 3:
		printf("Wednesday\n");
	case 4:
		printf("Thursday\n");
	case 5:
		printf("Friday\n");
	case 6:
		printf("Saturday\n");
	case 7:
		printf("Sunday\n");
	default:
		printf("Illegal input!\n");
	}
	return 0;
}
int main()
{
	int day = 0;
	const int a = 1;
	printf("please enter a number\n");
	scanf("%d", &day);
	switch (day)
	{
	case a:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("weekdays\n");
		break;
	case 6:
	case 7:
		printf("Rest Day\n");
		break;
	default:
		printf("Illegal input!\n");
		break;
	}
	return 0;
}

2.3.4 note 4: default is the default. This statement is used to prompt that there is no case entry on the corresponding match. When this statement is not needed, the code specification recommends adding {default: break; (pay attention to indentation and alignment, here to save space)} at the end of the switch statement.

2.3.5 logic quiz: switch statements can be nested, with the following code (a = 3;b = 6 should be output in the end):

int main()
{
	int a = 1;
	int b = 5;
	switch (a)
	{
	case 1:
		a++;
	case 2:
		b++;
		switch (b)
		{
		case 6:
			a++;
			break;
		case 7:
			b++;
		}
	}
	printf("a = %d\nb = %d\n", a, b);
	return 0;
}

Topics: C