Branch statement, loop

Posted by jameslloyd on Sat, 05 Mar 2022 05:09:52 +0100

Branch statements and loops

1.bool type

bool has only true and false; In C language, there are tens of millions of true (non-0) and only one false (0)

1.1 several bool types

int a = 0;//false
char ch = '0';//true
char ch2 = '\0';//false null character
char ch3 = 0;//false
int *ptr = NULL;//false null macro is defined as 0 in C

1.2 relational expression: the result is bool

  • The associativity of relational operators is left combination

  • ==Is equal to comparison. And = indicates assignment,

1.3 logical expression: the result is bool value

  • &&(concise and): the result is true only when both expressions involved in the operation are true, otherwise it is false
  • ||(concise or): as long as one of the two expressions involved in the operation is true, the result is true. When both expressions are false, the result is false
  • ! (non operation): when the expression participating in the operation is true, the result is false; When the expression participating in the operation is false, the result is true.

Priority: the priority of logical operators and other operators from low to high is:

  • Assignment operator (=) < & & and | < relational operator < arithmetic operator < non (!)

2. Branch statement

For the problem of judgment before selection, we should use the branch structure.
A pair of curly braces {} is a block of code.

2.1 single branch, double branch and multi branch

  • Single branch
int a = 5,b = 0,max = 0;
	max = a;
	if (max < b)
		max = b;
  • Double branch / triple operator

    if statements can be expressed with "?:.

    int a = 0, b = 5, max = 0;
	max = a > b ? a : b;
  • Multi branch if else
    Identify the category of keyboard input characters, whether they are numeric characters, lowercase characters, uppercase characters and other characters, and count the number.

    Code example:

#include <stdio.h>
int main()

	char ch;
	int connum = 0, dignum = 0, capnum = 0, amlnum = 0, othnum = 0;
	printf("input a character:");
	int a = 10;
	//Comma operator (lowest priority), otherwise there will be priority problems
	while (ch = getchar(), ch != '\n')
		if (ch < 32){
			//printf("This is a control character\n");
			connum += 1;
		else if (isdigit(ch)){
			//printf("This is a digit\n");
			dignum += 1;
		else if (isupper(ch)){
			//printf("This is a capital letter\n");
			capnum += 1;
		else if (islower(ch)){
			//printf("This is a small letter\n");
			amlnum += 1;
			//printf("This is an other character\n");
			othnum += 1;
	printf("con:%d\n", connum);
	printf("dig:%d\n", dignum);
	printf("cap:%d\n", capnum);
	printf("sam:%d\n", amlnum);
	printf("oth:%d\n", othnum);
	return 0;

Result display:

  • If nested if
  1. C language stipulates that else is always paired with the nearest if in front of it

2.2 empty statement

The statement can be empty and has no symbols except the semicolon at the end.

  • Placing a semicolon after the parentheses of an if, while, or for statement creates an empty statement, causing the statement to end prematurely.

2.3 switch multi branch statements

Another choice structure statement is used to replace the simple if else statement with multiple branches

Code example:

#include <stdio.h>
int main(){
	char grade = '#';
	printf("input grade(ABCDE)\n");
	scanf_s("%c", &grade);
	switch (grade)
	case 65:printf("90-100\n"); break;
	//case'A':printf("90-100\n"); break;  // The same meaning cannot exist at the same time
	case'B':printf("80-89\n");  //If there is no break, continue to execute the next case until you meet the break
	case'C':printf("70-79\n"); break;
	case'D':printf("60-69\n"); break;
	case'E':printf("<60\n"); break;
	default:printf("input error\n"); break;

Result display:

  • Follow the rules:

    1. switch (integer variable expression): byte short. int.char . Cannot be a floating point number, string, must be an integer.
    2. case label must be a constant expression
    3. case tag cannot be a unique constant. Two tags with the same constant value are not allowed
    4. Default is not necessary. Without default, if all case s fail to match, nothing will be executed in the switch, and then the subsequent code will be executed.

3. Loop statement while; do{}while;for(;;) 😉

  • while {} statement

    Judgment before execution

  • Do {} while (expression)

Execute before Judge

  • for(;😉{}

    Code list:
#include <stdio.h>
int main()
	int i = 0;
	int sum = 0;
	for (sum = 0, i = 1; i <= 3; ++i, sum = sum + i)//comma expression 

	printf("sum:%d\n", sum);
	return 0;

Result display:

  • Jump statements: break,continue,goto,return
  1. break: it can only be used in a switch statement. Jump out of the switch or terminate the loop in advance, and turn to the statement after the switch statement or loop statement.

  2. continue: terminate this cycle and start the next cycle.

    The difference between the two: the continue statement ends only this cycle, while the break statement ends the whole cycle.

  3. Goto: the control program is transferred from the place where the goto statement is located to the labeled statement. It is best to go down and jump only in this function.

  4. Return: used to end the execution of the function and return the caller. If it is the main function, it returns the operating system

You should study hard today~

Topics: C