[C Language] while loop and input buffer

Directory

  • Preface
  • while loop
  • infinite loop
  • break
  • continue
  • input buffer
  • Conclusion: This article about while loop is over. If you find it useful, please don’t be stingy with your likes and attention, thank you for your support! ! !

Foreword

In life, we often need to do the same thing repeatedly, such as studying day after day. This is a cycle.
As mentioned in the previous article, C language has three structures, one of which is a loop structure, which means that the same piece of code must be executed repeatedly. For example, your girlfriend asks you to say “I love you!” ten times, so you start writing code:

#include<stdio.h>

int main()
{<!-- -->
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
printf("I love you!\
");
\t
return 0;
}

When your girlfriend asks you to say it a hundred times, it will be very cumbersome and redundant if you still follow the above method of writing, so you decide to improve your code and want it to be executed repeatedly. You discovered the while cycle:

#include<stdio.h>

int main()
{<!-- -->
int i = 1;
while (i <= 10)
{<!-- -->
printf("I love you!\
");
i + + ;
}

return 0;
}

That’s great, but I don’t have a girlfriend!

while loop

The form of the while loop is:
while (expression)
{
\tloop statement;
}

First, enter the while loop, calculate the value of the expression, and if it is true, execute the loop statement; calculate the value of the expression again, and continue execution if it is true; this process will be repeated until the value of the expression is false, then exit. Loop, execute the code behind the while loop. For example, you want to print the numbers 1-10 on the screen:

// while loop
#include<stdio.h>

int main()
{<!-- -->
int n = 1;
while (n <= 10)
{<!-- -->
printf("%d\
", n);
n + + ;
}

return 0;
}

Infinite loop

When the expression, that is, the loop condition is always true, the loop will continue. This is what we call an infinite loop. for example:

#include<stdio.h>

int main()
{<!-- -->
while (1)
{<!-- -->
printf("I love you\
");
}

return 0;
}

This code will output “I love you” an infinite number of times until the user forces it to close. This code can be summed up in one sentence: Love till death!

break

break is a keyword, which is also found in switch in the previous article.
If you add a semicolon after it, it becomes a statement.
The function of the break statement is to jump out of the loop and execute the code behind the loop.
#include<stdio.h>

int main()
{<!-- -->
int n = 1;
while (n <= 10)
{<!-- -->
if(n==5)
break;
printf("%d\
", n);
n + + ;
}

return 0;
}

This code starts looping from 1 until n==5, it will break out of the loop and execute the code behind the entire loop, so its result is 1 2 3 4.

You must have written a program for the sum of two numbers, but it is only input once and calculated once. If you want to calculate again, you must close the program and re-run the input again. At this time, we can use a while loop to input the values of a and b multiple times. Then output the result of a + b:

#include<stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 0;

while (scanf("%d %d", & amp;a, & amp;b) != EOF)
{<!-- -->
printf("%d\
", a + b);
}
\t
return 0;
}

EOF, the full name is end of file, is the end of file mark. If the read fails, EOF will be returned. In fact, EOF is a macro definition, and its value is -1. You can try to write this code into VS, right-click EOF , click to go to the definition, you will find a line of code in the stdio.h file as

#define EOF (-1)

Let’s talk about the return value of the scanf function. It will return the number of input numbers:

#include<stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 0;

int n = scanf("%d %d", & amp;a, & amp;b);

printf("%d\
", n);

return 0;
}

You can test this code. If you input any two integers, it will output 2.
You run this code again:

#include<stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 0;
int c = 0;

int n = scanf("%d %d %d", & amp;a, & amp;b, & amp;c);

printf("%d\
", n);

return 0;
}

If you input any three numbers, 3 will be output. If you do not enter enough numbers, for example, if you run this code and you only enter two numbers and press Enter, you will find that it is really just Enter and Line Feed, and it is waiting for you to enter the third number. At this time If you enter another number and press Enter, it will output 3 on the screen.

continue

We say that break terminates the loop, and continue also terminates the loop, but break terminates the entire loop and executes the code after the loop; continue only terminates this loop, and does not execute the code after continue in the loop, and will jump to the while loop. Conditional judgment part, enter the next cycle.

#include<stdio.h>

int main()
{<!-- -->
int n = 1;
while (n <= 10)
{<!-- -->
if(n==5)
continue;
printf("%d\
", n);
n + + ;
}

return 0;
}

Analyze this code. The initial n is 1, the loop condition is established, enter the loop, print n, n++, when the loop is executed to n is 5, continue, skip the printing part after continue and n++ and return to the loop. At the beginning, go down. At this time, n is still 5, so no printing will be done until n is greater than 10 to jump out of the entire loop and the program ends. We find that the result of this code is 1 2 3 4.
If we put n++ before if:

#include<stdio.h>

int main()
{<!-- -->
int n = 1;
while (n <= 10)
{<!-- -->
n + + ;
if(n==5)
continue;
printf("%d\
", n);
}

return 0;
}

Let’s analyze this code again. First, the initial n is 1, which meets the loop conditions. Enter the while loop, execute n + +, n is 2, n is not 5, print, return to the beginning of the loop…until n is 5, jump After this loop, 5 will not be printed, and 5 will be returned to the beginning of the loop until n is greater than 10. The loop ends. However, when n is 10, the loop conditions are still met. Enter the loop again, n + + , n becomes 11, and print 11. When you enter the loop again and start to find that the loop conditions are not met, exit the loop. We found that the result of this code starts from 2 and ends at 11, without 5.

Input buffer

We know that the keyboard is the input device in the computer, and the scanf function is the input function. However, the scanf function does not directly read the data input from the keyboard, but looks for it in the input buffer. If there is no data in the buffer, it will wait. The data input from the keyboard comes to the input buffer, and the same is true for functions such as getchar that obtain characters.

We use passwords in many scenarios in our lives:

#include<stdio.h>

int main()
{<!-- -->
char password[10] = {<!-- --> 0 };
printf("Please enter password:");
scanf("%s", password);
printf("Please confirm password: (Y/N)\
");
char input = 0;
input = getchar();
if ('Y' == input)
printf("Confirmation successful\
");
else
printf("Confirmation failed\
");

return 0;
}

Based on this code, we judge that when we confirm the password, if we enter Y, the confirmation will be printed successfully. Suppose our password is 123456, but when we run it we find:

Why did it fail to confirm so quickly? I haven’t confirmed it yet.
This is because after you enter the password, it is actually followed by a \
. At this time, the data in the input buffer is 123456\
, and the scanf or getchar function will not read the \
when reading, that is, 123456 has been taken away. But there is still one \
left, which is equivalent to you directly inputting a \
when confirming the password, not ‘Y’, so “Confirmation failed” will be output directly. The processing is also simple, we just take away \
and that’s it. We can use getchar to take away \
(please note that I only use getchar here and do not store it), so that there is no data in the input buffer:

#include<stdio.h>

int main()
{<!-- -->
char password[10] = {<!-- --> 0 };
printf("Please enter password:");
scanf("%s", password);
printf("Please confirm password: (Y/N)\
");
char input = 0;
getchar(); //Use getchar to clean up\
.
input = getchar();
if ('Y' == input)
printf("Confirmation successful\
");
else
printf("Confirmation failed\
");

return 0;
}

Huh? Doesn’t this article talk about while loop? This has nothing to do with the while loop. Don’t worry, if I am entering the password 123456 ab, using the above code at this time will not give me a chance to confirm:

This is because scanf will stop when it encounters a space, and only takes away the space before it, leaving ab\
. However, using getchar can only clear one character, and there is remaining data in the input buffer. At this time, while The role of is reflected:

#include<stdio.h>

int main()
{<!-- -->
char password[10] = {<!-- --> 0 };
printf("Please enter password:");
scanf("%s", password);
printf("Please confirm password: (Y/N)\
");
char input = 0;

//Clear the remaining data in the buffer
while (getchar() != '\
')
{<!-- -->
;
}
input = getchar();
if ('Y' == input)
printf("Confirmation successful\
");
else
printf("Confirmation failed\
");

return 0;
}

Draw a picture and analyze it:

All data is in the input buffer

After entering the while loop, when the character obtained is not \
, nothing is done. It just takes the characters out of the input buffer one by one until it encounters \
. After taking away the \
, it is entered into the buffer. Nothing is gone, now you can confirm the password. Let’s run it and see:

We see the opportunity to confirm the password!

Conclusion: This article about while loop is over. If you find it useful, please don’t be stingy with your likes and attention, thank you for your support! ! !