Immediate Exit of 'While' Loop in C++

Immediate exit of 'while' loop in C++

Use break?

while(choice!=99)
{
cin>>choice;
if (choice==99)
break;
cin>>gNum;
}

Exiting while(1) loop in C programming

To better explain to you lets add few lines to your code:

#include<stdio.h>
int main()
{

while(1)
{
printf("Enter number: ");
scanf("%d", &num);

if (num==2)
{
return 0;
}
else
{
printf("Num = %d", num);
}
}

printf ("BYE\n");
return 0;
}

and lets put break instead of return 0;:

#include<stdio.h>
int main()
{

while(1)
{
printf("Enter number: ");
scanf("%d", &num);

if (num==2)
{
break;
}
else
{
printf("Num = %d", num);
}
}

printf ("BYE\n");
return 0;
}

Output of first code would not include the message "BYE", where second code would.

So the to sum up return terminates function and does not execute anything below it, where break terminates loop and program does not execute remaining lines with in the loop.

Exit from while loop without break in C

The only way I see is to put in conditional the more code below part

 int stop = 0;

while (stop != 1) {
if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
stop = 1;
} else {
// more code below
}
}

The second only way using a function :

while (doStuff() == 0);

int doStuff(void) {
if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
return 1;
}

// more code below

return 0;
}

PS: Maybe I'm a nazi but this should definitely be a do ... while

 int stop = 0;

do {
if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
stop = 1;
} else {
// more code below
}
} while (stop != 1);

Breaking out of a loop from within a function called in that loop

break, like goto, can only jump locally within the same function, but if you absolutely have to, you can use setjmp and longjmp:

#include <stdio.h>
#include <setjmp.h>

jmp_buf jump_target;

void foo(void)
{
printf("Inside foo!\n");
longjmp(jump_target, 1);
printf("Still inside foo!\n");
}

int main(void) {
if (setjmp(jump_target) == 0)
foo();
else
printf("Jumped out!\n");
return 0;
}

The call to longjmp will cause a jump back to the setjmp call. The return value from setjmp shows if it is returning after setting the jump target, or if it is returning from a jump.

Output:

Inside foo!
Jumped out!

Nonlocal jumps are safe when used correctly, but there are a number of things to think carefully about:

  • Since longjmp jumps "through" all the function activations between the setjmp call and the longjmp call, if any of those functions expect to be able to do additional work after the current place in execution, that work will simply not be done.
  • If the function activation that called setjmp has terminated, the behaviour is undefined. Anything can happen.
  • If setjmp hasn't yet been called, then jump_target is not set, and the behaviour is undefined.
  • Local variables in the function that called setjmp can under certain conditions have undefined values.
  • One word: threads.
  • Other things, such as that floating-point status flags might not be retained, and that there are restrictions on where you can put the setjmp call.

Most of these follow naturally if you have a good understanding of what a nonlocal jump does on the level of machine instructions and CPU registers, but unless you have that, and have read what the C standard does and does not guarantee, I would advise some caution.

How can I exit a while(1) based on a user input?

while(1) {
printf("Enter string to process: ");
scanf("%s", client_string);

string_size=strlen(client_string);
write(csd, client_string, string_size);
if (client_string[string_size -1 ] == '&') {
break;
}
}

break keyword can be used for immediate stopping and escaping the loop. It is used in most of programming languages.
There is also one useful keyword to slightly influence loop processing: continue. It immediately jumps to the next iteration.

Examples:

int i = 0;
while (1) {
if (i == 4) {
break;
}
printf("%d\n", i++);
}

will print:

0
1
2
3

Continue:

int i = 0;
while (1) {
if (i == 4) {
continue;
}
if (i == 6) {
break;
}
printf("%d\n", i++);
}

will print:

0
1
2
3
5

Immediate exit of a loop when input is not an unsigned in C++

You can make the input a condition of the loop.

while (cin >> numbers)
{
//...
}

Will run until the user enters something that cannot be inputted into numbers. If you want to have the check the numbers < 9999 as well then we would have

while (std::cin >> numbers && numbers < 9999)
{
//...
}


Related Topics



Leave a reply



Submit