Some understanding of minute statement and loop statement in C language

Keywords: C Back-end


preface

        These two days, I have learned the knowledge of branch statements and circular statements in C language, and my understanding and understanding of C language is finally on track. Branch and loop make the program have unlimited possibilities, so that the program is no longer rigid and can only execute one command. It can accept, recognize and make correct behavior for the information entered by the user. I think it is with branches and loops that C language makes computers work like humans.


1, What are branches and loops?

        Branch, you can understand it as a multiple-choice question for the computer, and the author is our programmer. Through the pre-designed instructions, the computer judges the received information and executes the corresponding code, which is branch statement.

        Cycle can be said to be a job assigned by programmers to computers. Just like people at work, it repeats one thing day after day and keeps going back and forth. However, it is not simply repeating commands. Generally, a judgment condition will be set in the loop. When this condition is met, the loop will exit.


2, Chestnut lifting time

(1) Branch statement

        Branch statements are mainly if statements and switch statements. Here, I list a piece of code to show the functions and my views.


1.if statement

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    int a = 0;
    int b = 10;
    scanf("%d", &a);
    if (a > b)
    {
        printf("%d", a);
    }
    else if (a == b)
    {
        printf("%d %d", a, b);
    }
    else
    {
        printf("%d", b);
    }
    return 0;
}

        In the above code, the usage of if statement is simply shown. The if statement can compare the data to determine what command to execute. In fact, the if statement is not important. The key is that in the judgment condition, a == b, here should be "= =". Because "=" plays the role of assignment, we should pay more attention to it when programming.

2.switch statement

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    int a = 0;
    scanf("%d", &a);
    switch (a)
    {
    case 1:
        printf("1\n");
        break;
    case 2:
        printf("2\n");
    case 3:
        printf("3\n");
        break;
    default:    //This is an instruction for setting all values except the above
        printf("4\n");
        break;
    }
    return 0;
}

        The switch statement is more like the multiple-choice question I mentioned earlier. It can accurately judge the data entered by the user, and then enter the corresponding command. Among them, I think it should be noted that "break" is used to jump out of statements. If you do not add break, the program will continue to output the following commands after the user enters the commands. In the above code, I leave the break of case 2 blank. If you use this code to input 2, the output data will be "2 3" "Two items. In addition, case 1 is followed by a colon: not a semicolon;.

(2) Circular statement

        There are more loop statements than branch statements. There are while loops, do while loops, and for loops, but they all change. They all use a data as a condition to determine and adjust to complete the loop. Like branch statements, I will give a few examples, and then talk about my understanding.

1.while loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    int a = 0; //Initial data
    while (a < 10) //condition
    {
        printf("%d\n", a);
        a++; //Data adjustment
    }
    return 0;
}

        This is the most basic while loop, which consists of three simple elements: initial data, defining conditions, and then data adjustment.

2.do while loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    int a = 1;
    do
    {
        printf("%d\n", a);
        a++;
    } while (a < 1);
    return 0;
}

        The do while loop is actually very similar to the while loop, but it puts the conditions of the loop to the last for judgment, which can ensure a loop. As shown in the above code, the initial value of a is 1, which does not meet the conditions of the while loop, but because the conditions are judged to the last, he will still perform a loop before jumping out of the loop.

3.for loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    for(int a = 0;a < 10;a++)
    {
        printf("%d\n", a);
    }
    return 0;
}

        Compared with the first two loops, the for loop displays the three elements more intuitively on the code and can directly understand the loop. It will also be very convenient to modify the code in the future, and there is no need to cross multiple lines to find the code to be modified.


summary

         The above is my knowledge and understanding of branch and circular sentences during this period. Of course, since I am still in the beginner's stage, I only briefly introduce them here. More specific and in-depth contents. I hope I can make a better introduction to you in future research and learning. I hope you will be more tolerant and comment.

Private time

        Finally, I finished writing my second blog. I thought I was about to give up, but I still had to insist. Every time I learned something new, I always thought about how to put this code into the game. What's the use in the game? I couldn't help but always wanted to try, but it's easy to get around myself when I'm making it up, or I'm stuck, and I can't think of how to make it down. Just the so-called coincidence It's hard for a woman to make bricks without rice. What's more, I'm not a skillful woman, that is, I'm a layman. I can only make up things in the East and West, plug them into the code, and they can also run. The effect can be achieved, but I always feel strange. What's wrong. The following is the display of my personal works.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{
	int Elder task 1 = 0;
	int Elder task 2 = 0;
	int Looking for the old man = 0;
	int choose = 0;//Used to determine the player's decision
	printf("You come to a village and find an old man looking forward to it at the head of the village.\n");
	printf("You go forward, and the old man hurried over.\n");
	printf(""Brave, you're here at last,Our village has been invaded by demons. I hope you can save our village. "\n");
	printf("0.Don't worry, village head. As a brave man, I will save your village.\n");
	printf("1.You think it's a problem, turn around and leave\n");
	do
	{
		scanf("%d", &choose);
		switch (choose)
		{
		case 0:
			printf("Thank you, brave.\n");
			break;
		case 1:
			printf("You see the old man, feel disgusted, turn around and leave.\n");
			return 0;
			break;
		default:
			printf("Please enter the correct selection!\n");
			break;
		}
	} while (choose);
	while (Elder task 1 != 3 || Elder task 2 != 3)//Release tasks to players
		{
			if (Elder task 1 == 0)
			{
				printf("1.In the north of the village, there are a group of pig headed people who have been destroying our crops. Many people have no harvest. I hope you can help us eradicate them.\n");
			}
			else if (Elder task 1 == 2)
			{
				printf("I've wiped out the pig men who destroyed the crops\n");
				printf("Thank you, brave one. The villagers will have a good harvest in the future.\n");
				Elder task 1 = 3;
			}
			if (Elder task 2 == 0)
			{
				printf("2.In the south of the village, there are a group of goblin who often attack the villagers. Many people dare not go out of the village.\n");
			}
			else if (Elder task 2 == 2)
			{
				printf("I have eliminated goblin, which is dangerous to the villagers.\n");
				printf("Thank you for your efforts. In the future, the villagers will be able to leave the village at ease\n");
				Elder task 2 = 3;
			}
			if (3 == Elder task 1, 3 == Elder task 2)
			{
				break;
			}
			scanf("%d", &choose);
			switch (choose)
			{
			case 1:
				printf("It's unbearable to destroy crops. Village head, you can rest assured and give it to me!\n");
				Elder task 1 = 1;
				break;
			case 2:
				printf("What, how dare you hurt the villagers when I get rid of them!\n");
				Elder task 2 = 1;
				break;
			default:
				printf("Please enter the correct selection!\n");
				break;
			}
			while ((0 != Elder task 1 || 0 != Elder task 2) && 0 == Looking for the old man)//Give players options to choose the next action
			{
				printf("You go to the entrance of the village and look at both sides.\n");
				printf("1.Go north to the crops.\n");
				printf("2.Go south to the woods.\n");
				printf("3.Go back to the village head.\n");
				scanf("%d", &choose);
				switch (choose)
				{
				case 1:
					if (1 == Elder task 1)
					{
						printf("From a distance, you can see that the pig head man is destroying the crops,You flew into a rage and rushed up to kill the pig man.\n");
						Elder task 1 = 2;
					}
					else if (0 == Elder task 1)
					{
						printf("You see a destroyed crop field.\n");
					}
					else if (3 == Elder task 1)
					{
						printf("The pig head man was destroyed and the crops could thrive.\n");
					}
					break;
				case 2:
					if (1 == Elder task 2)
					{
						printf("You walked carefully in the jungle and saw goblin bullying the villagers. You rushed forward immediately to destroy goblin and save the villagers.\n");
						Elder task 2 = 2;
					}
					else if (0 == Elder task 2)
					{
						printf("When you see a jungle, you feel dangerous and should not approach it.\n");
					}
					else if (3 == Elder task 2)
					{
						printf("Without the threat of goblin, the villagers walked in the jungle at ease.\n");
					}
					break;
				default :
					printf("Please enter the correct selection!\n");
					break;
				}
				if (3 == choose)
					Looking for the old man = 1;
			}
			printf("You went back to the old man.\n");
			printf("The old man looks at you\n");
			Looking for the old man = 0;
		}
	printf("The old man excitedly held the brave's hand and thanked the brave for everything he did.\n");
	return 0;

}

        My code is really looping and branching. My brain is confused, but fortunately, the sewing and mending is also running for me. Originally, I wanted to add numerical values, but I had no choice but to make a text dialogue of options. When my strength is enough, I must add a combat system to this code. Keep going, programmer.

Posted by leeperryar on Thu, 04 Nov 2021 17:41:22 -0700