[c language] branch and loop (number guessing game)

branch

if branch statement

if (expression)
sentence;
else
Statement 2;

if (expression)
{
Statement 1;
}
else if (expression)
{
Statement 2;
}
else
{
Statement 3;
}
If the expression is true, execute the statement
In c language, 0 is false and non-0 is true
else matches the nearest if

switch branch statement

Often used for multiple branches

Switch (integer expression)
{
case (integer constant expression):
sentence;
break; / / jump out
Default: / / by default, all case s that cannot be matched jump here
}

Break is very important. If there is no break statement, it will be executed down all the time

#include<stdio.h>
	int main()
	{
		int month = 0;
		scanf("%d", &month);
		switch (month)
		{
		case 1:
			printf("January");
			break;
		case 2:
			printf("February");
			break;
		case 3:
			printf("March");
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
			printf("other");
			break;
		default:
			printf("Input error!");
			break;
		}
		
		return 0;
	}



loop

while

while (expression)
{
Loop statement;
}
If the expression is true, execute the loop statement
Otherwise, do not execute
Break: in a while loop, break is used to permanently terminate the loop
Continue: in the while loop, continue is used to jump out of this loop and directly go to the judgment part to see whether to enter the next loop

#include<stdio.h>
int main()
{
	char password[20] = {0};
printf("Please input a password<");
    scanf("%s", password);
	//getchar() clears' \ n 'in buffer
	//getchar();
	
	//Clean up multiple characters in the buffer
	int tem = 0;
	while (tem=getchar() != '\n')
	{
		;
	}
	printf("Please confirm the password: Y/N\n");
	if (getchar() == 'Y')
		printf("The password is correct");
	else
		printf("Incorrect password");

	return 0;

}

Input from the keyboard to the buffer. Getchar reads characters from the buffer. Enter the password 12345 and press enter. In the buffer: 12345\n, so you need to clear the buffer and read \ n with getchar. At this time, the buffer is empty and does not affect the reading of the next character. Otherwise, you cannot read the Y/N entered by the keyboard
If the password entered is 12345 abcd, the buffer is: 12345 abcd\n. multiple characters cannot be read with getchar, but multiple characters are read with a while statement, and the buffer is cleared

for

(initialization;   Judgment; adjustment statement)

For (expression 1; expression 2; expression 3)
{
Circulatory body;
}
First execute expression 1 for initialization, then execute expression 2 for judgment. If it is true, execute the loop body, and finally execute expression 3; continue the loop until expression 2 is false.


In a for loop, break permanently terminates the loop
continue aborts this cycle and jumps to the adjustment section

do...while

do
{
Loop statement;
}while (expression);
Execute first and then judge. If it is true, continue to execute the loop until the expression is not tenable.
Features: cycle at least once

Figure guessing game

1. Automatically generate a random number of 1-100
2. Guess the number: Yes, Congratulations, the game is over;
If you guess wrong, you will be told whether you guess big or small, and then continue to guess until you get it right
3. The game can be played all the time unless you quit the game

#include<stdio.h>
 void menu() {
	printf("*******************\n");
	printf("****  1.play  *****\n");
	printf("****  0.exit  *****\n");
	printf("*******************\n");
}
 void game() {
	 //1. Generate random number
	 int red = rand()%100+1;
	 //2. Guess the number
	 printf("Please guess the number\n");
	 while (1)
	 {
		 int guess = 0;
		 scanf("%d", &guess);
		 if (guess > red)
			 printf("Guess big");
		 else if (guess < red)
			 printf("Guess it's small");
		 else
		 {
			 printf("Congratulations, you guessed right");
			 break;
		 }
	 }
 }
int main()
{
	int input = 0;
	srand((unsigned int) time (NULL));//Timestamp conversion random number
	do
	{
		menu();
		printf("Please select>>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game");
			break;
		default:
			printf("Input error, please re-enter");
			break;
		}
	} while (input);
	return 0;
}

Operation results

Tags: C

Posted on Sun, 03 Oct 2021 20:37:57 -0400 by nmarisco