int main() {
int input = 0;
do {
if(input > 1) {
printf("You entered");
break;
}
printf("Enter an integer (> 1): ");
scanf("%d",&input);
} while(input < 2);
return 0;
}
Hello, I'm new to C. Could anyone explain what break does? In this program, the statement before break "You entered" doesn't show up on the console. I know java but this works totally different.
There is no integer exist between 1
and 2
. Change
if(input > 1)
to
if(input > 0)
After that if you enter 1
then program enters the if
body then print You entered
and on encountering the break
statement, immediately terminates the do-while
loop.
The "break" statement causes your code to exit the loop immediately.
You're not seeing any output because you only loop while the input is strictly less than 2, but your if statement is looking for input that's strictly greater than 1.
That's not going to work; if you enter 1, the if statement won't execute (because 1 > 1 is false), and if you enter 2, the loop exits immediately (because 2 < 2 is false).
You either need to loop while input <= 2, or you need to test for input >= 1.
Having said all that...
Standard output is usually line-buffered , meaning you won't see anything show up on your console until the buffer is full or you send a newline character. You either need to manually flush the stream or send a newline as part of the format string; either
printf("You entered");
fflush(stdout);
or
printf("You entered\n");
should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.