[c language] circular statement

catalogue

1. Introduction

2.if else

3.switch

4.while loop

5.for loop

6.do while loop

1. Introduction

Learning contents of this section:

Branch statement

  • if
  • switch

Circular statement

  • while
  • for
  • do while

2.if else statement

Syntax structure:

First bell:

if (expression)

Statement 1;

Second:

if (expression)

Statement 1;

 else

Statement 2;

Third:

if (expression 1)

Statement 1;

else if (expression 2)

Statement 2;

else

Statement 3;

Code example -- > use the if else statement to write age segments as follows:

                          Age < 18 is juvenile

                          age is 18 to 26 for young people

                          age is middle-aged between 26 and 40

                          Age over 40 is old age

int main()
{
	int age = 0;
	scanf("%d",&age);
	if (age < 18)
		printf("juvenile");
	else if (age >= 18 && age < 26)
		printf("youth");
	else if (age >= 26 && age < 40)
		printf("middle age");
	else
		printf("old age");
	return 0;
}

The code execution is as follows: 

  Note: else and the latest if statement are a group!!!

3.switch statement

Syntax structure:

Switch (expression)
{

case: statement; (break;)

case: statement; (break;)

}

Code example -- > judge the day of the week

When there is no break:

int main()
{
	int day = 0;
	scanf("%d",&day);
	switch (day)
	{
	case 1: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");
	}
	return 0;
}

The code operation is shown in the figure below:

  When there is a break:

The code operation is shown in the figure below:

Therefore, we can see the function of break -- > jump out of the switch statement

Now let's think about how to make code 1-5 output working days and 6-7 as rest days?

We can change the code as follows:

int main()
{
	int day = 0;
	scanf("%d",&day);
	switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:printf("weekdays\n"); break;
	case 6:
	case 7:printf("Rest Day\n"); break;
	}
	return 0;
}

  Now let's talk about other statements in switch -- > default

For example, when we input 8, 9... In the code, we will not output anything, but how do we want him to output "input error"?

We can modify it as follows:

switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:printf("weekdays\n"); break;
	case 6:
	case 7:printf("Rest Day\n"); break;
	default:printf("Input error"); break;
    }

4.while loop

Syntax structure:

While (expression)

sentence;

Code practice -- > Print 1-10 on the screen, but the code should jump out when i=5

int main()
{
	int i = 0;
	while (i <=10)
	{
		printf("%d\n", i);
		i++;
		if (i == 5)
			break;
	}
	return 0;
}

Therefore, we can see the role of break -- > in the while loop, terminate the loop

Let's think about what happens if we replace break with continue and put the if statement in front of i + +?

The code execution is shown in the figure:

Then why? When i=5, we do not jump out of the loop and do not execute i + +, so i is always an dead loop  

Therefore, we can find that the function of continue -- > skips this cycle

Now let's analyze the following code

int ch = 0;
	while ((ch = getchar() )!= EOF)
	{
		putchar(ch);
	}
return 0;

The code runs as follows:

 

So how do we stop this code? CTRL + Z -- > end of read

Getchar returns the ASCII value of the character. If an error is encountered or the file ends, EOF (end of file) - > - 1 is returned, which is the end of file flag. The return type of getchar is int!!!!

Code example -- > password input and confirmation

char password = { 0 };
	printf("Please input a password:");
	scanf("%s", &password);
		printf("Please confirm the password(Y/N)");
		int ch = getchar();
		if (ch =='Y')
			printf("Confirmation successful");
		else
			printf("Confirmation failed");
	return 0;

We will find that if we haven't entered Y, he will directly output the confirmation failure. Why?

If we enter 12345\n, scanf will take 12345 away, and getchar will take the rest \ n away, so we will go directly to else.

So how to solve this problem? -- > Clear buffer \ n

Add a getchar () before getchar;

So what happens when we enter 12345 abcde?

We will find the same mistakes as before. Why?

scanf will take away 12345 before the space, while getchar can only consume one character.

So we need to clean up the buffer!! The code is modified as follows:

char password = { 0 };
	printf("Please input a password:");
	scanf("%s", &password);
	printf("Please confirm the password(Y/N)");
	int x=0;
	while ((x = getchar()) != '\n')
	{
		;
	}
	int ch = getchar();
	if (ch == 'Y')
		printf("Confirmation successful");
	else
		printf("Confirmation failed");
	return 0;

  5.for loop

Syntax structure:

For (expression 1; expression 2; expression 3)

      sentence;

for (int i = 0; i < 10; i++)
	{
		if (i == 5)
			continue;
		printf("%d ", i);
	}

The code running result is 0 1 2 3 4 6 7 8 9

From the above, we can find the function of continue - > skip this cycle

It is recommended -- > to write the for loop by closing left and opening right

eg: for(i=0;i<10;i++)

Let's judge how many times the following code loops?

int main()
{
    int i=0;
    int k=0;
    for(i=0,k=0;k=0;k++,i++)
      k++;
    return 0;
}

The answer is 0 times

Because the judgment statement here is k=0, which means that 0 is assigned to K, so it is false, so the number of cycles is 0.

6.do while loop

Statement structure:

do

      Loop statement;

While (expression);

Features: the loop body is executed at least once!!!

Today's code exercise -- > find n!

int main()
{
	int n = 0;
	int x = 1;
	scanf("%d", &n);
	for (int i = 1; i <=n; i++)
	{
		x *= i;
	}
	printf("%d", x);
	return 0;
}

Code exercise -- > 1+ 2!+.....+ n!

int main()
{
	int sum = 0;
	int n = 0;
	int x = 1;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		x *= i;
		sum = sum + x;
	}
	printf("%d", sum);
	return 0;
}

Code exercise -- > find a number in order

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int k = 7;
	int n = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = n - 1;
	int mid = 0;
	while (left <= right)
	{
	    mid = (left + right) / 2;
		if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else if (arr[mid]>k)
			right = mid - 1;
		else
		{
			printf("%d", arr[mid]);
			break;
		}
	}
	if (left > right)
		printf("Can't find!");
	getchar();
	getchar();
	return 0;
}

Code exercise -- > how to achieve this result:

 

#include<windows.h>
int main()
{
	char arr1[] = { "welcome to bit!!!!!!" };
	char arr2[] = { "####################" };
	int left = 0;
	int right = sizeof(arr1)-1;
	while (left <= right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		
		printf("%s\n", arr2);
		Sleep(1000);//Sleep for one second
		left++;
		right--;
	}
	return 0;
}

The Sleep function needs to import the header file windows.h!!

Then this section is over!!!

Here is also a Xiaobai who is learning. If there is a problem with the code or understanding, I hope to forgive you and write to me > <.

Tags: C

Posted on Tue, 16 Nov 2021 08:26:02 -0500 by _SAi_