Quick view of C language -- 3. Branch and loop statements and functions

Posted by mvijfwinkel on Mon, 24 Jan 2022 12:54:55 +0100

1, Branch and loop statements

1. Concept of statement

In fact, the sentence concept in C language is similar to that in natural language; It is divided into five categories

Control statements in C language can be divided into the following three categories

1. Conditional judgment statements are also called branch statements: if Statement switch sentence
2. Circular execution statement: do while Statement while Statement for sentence
3. Steering statement: break Statement goto Statement continue Statement return sentence

Turn statements are generally used in conjunction with recycling

2. Branch statement

2.1 if statement

Look at the code

#include<stdio.h>

int main()
{
	int input = 0;
	printf("Forward or backward?\n");
	printf("1.Forward 2.back off:>");
	scanf("%d", &input);
	if (input == 1)
	{
		printf("forward");
	}
	else if (input == 2)
	{
		printf("back off");
	}
	else
	{
		printf("A and B Inside selection C My guy!");
	}
	return 0;
}

The basic syntax of an if statement is

if (expression)

{processing method}

else if (expression)

{processing method}

        .......

        else

{processing method}

The if statement can be followed by a variety of else cases. When the processing method is monolingual sentence, you can not increase the bracket {}, and you must increase the bracket when multiple statements. Else can be added or not.

If statements can also be nested. When nested, else and its nearest if are matched

#include<stdio.h>

int main()
{
	int i = 0;
	int n = 1;
	if (i == 1)
		if (n == 1)//else matches me
			printf("hh");
	else //else here matches the previous if
		printf("aa");
	return 0;
}

This branch code is when i is 1 and n1 is print hh, i is 1 but n is not 1 is print aa, although there is no syntax error, but the readability is poor. If you write it in curly braces, it will be much easier to understand

#include<stdio.h>

int main()
{
	int i = 0;
	int n = 1;
	if (i == 1)
	{
		if (n == 1)
		{
			printf("hh");
		}
		else
		{
			printf("aa");
		}
	}
	return 0;
}

2.2 switch statement

The function of switch statement is the same as that of if statement. It is suitable for multiple branches. When there are many choices, it is suitable to use switch statement. Its basic syntax form is

//Default means default
//When the result of a constant expression does not match any of the case s
//default will be executed
//The processing method of default can be defined by the user
//The compiler's default method is to do nothing
switch (Constant expression)
{
case constant:
    processing method;
    break;
case Another constant:
    processing method;
    break;
default:
    processing method;
    break;
}

switch must be followed by a constant expression, and the result must be a constant. The position of the default clause is arbitrary and there is no rigid provision

Break can not be added. At this time, it will be executed downward from this case until a break or exit is encountered, such as

#include<stdio.h>

int main()
{
	int input = 1;
	printf("Please enter");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		printf("111");
	case 2:
		printf("222");
	case 3:
		printf("333");
	default:
		printf("000");
	}
}

When input 1 is yes, 111 222 333 000 will be printed successively. If any number other than 1, 2 and 3 is output, 000 will be printed and then finished. However, if default is placed above case 1, 000111222333 will be printed

3. Circular statement

3.1 while statement

The syntax of the while statement is

while (Constant expression)
{
	Circulatory body
}

When the constant expression ends at 0, the loop ends. When it is not 0, the loop continues. Generally, the loop variable is adjusted in the loop body to end the loop when it meets certain conditions, such as printing numbers from 1 to 10

#include<stdio.h>

int main()
{
	int i = 10;
	while (i)
	{
		printf("%d", i--);
		i--;
	}
}

You can also add break and continue statements to the loop. The break statement will end the whole loop, and the continue statement will call over the current loop. As follows, when i is 10, the cycle is skipped, when i is 5, the cycle ends, and finally 9 8 7 6 will be printed

#include<stdio.h>

int main()
{
	int i = 10;
	while (i)
	{
		if (i == 5)
		{
			break;
		}
        if (i == 10)
		{
			continue;
		}
		printf("%d ", i--);
		i--;
	}
}

3.2 for statement

The for loop statement has been briefly introduced in the previous content, and its usage is relatively simple. The syntax is

for (initialization of loop variables; Determination of loop conditions; adjustment of loop variables)

Similarly, print the numbers 1 to 10 and use the for loop

#include<stdio.h>

int main()
{
    int i = 0;
    for(i = 1; i<=10; i++)
    {
        printf("%d ", i);
    }
}

In the for loop, the break statement will directly end the loop, while the continue statement will skip the current loop and go directly to the loop variable adjustment part.

In the for loop, initialization, condition and adjustment can be omitted arbitrarily or completely (if there is no break in the loop, the loop will be dead)

3.3 do while statement

The difference between the do while loop and the above two loops is that the loop must be executed once. Regardless of the loop conditions, the syntax is

do
{
    Circulatory body;
}
while(Constant expression);

The break statement will still directly end the loop, and the continue statement will jump to the loop condition judgment part (i.e. constant expression), which is less used

2, Functions

The basic syntax of a function is

//() is a parameter
 Return value type function name ()
{
    Function body;
}

Function can realize many functions, such as addition function, factorial function, etc. Although C language itself provides some library functions, many functions still need to be realized by users themselves. Using functions, you can reuse code and simplify code, as follows

#include <stdio.h>

int Add(int x, int y)
{
   int z = x+y;
   return z; 
}

int main()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;
    printf("Enter two numbers:>");
    scanf("%d %d", &num1, &num2);
    sum = Add(num1, num2);
    printf("sum = %d\n", sum);
    return 0; 
}

Different functions can call each other, but other functions are not allowed to be defined inside the function

Topics: C Back-end