Home > Blockchain >  break a loop then not run next code if loop breaks
break a loop then not run next code if loop breaks

Time:09-04

#include <stdio.h>

int main() {
    while (height > 0) {
        if (throttle >= 0 && throttle <= 100) {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp   velocityTemp - (throttle * K-G) / 2;
            velocity = velocityTemp   (throttle * K-G);
            fuel = fuelTemp - throttle;
            time = time   1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;   
        }
        else {
            printf("Please choose a number between 0 and 100! \n");
            break;
        }
    }
   
    if (velocity >= -2.0) {
        printf("You landed successfully: ");
    } 
    else {
        printf("Failed! You crashed");
    }

    return 0;
}

I want to only run the if velocity part if the loop does not break, if I keep the code this way it'll run that code no matter what since the break obviously only quits the loop. My full code is not written.

CodePudding user response:

There are basically two solutions to this:

  1. use a idBreak variable that you set to 1 upon breaking, like in the mikyll98's answer
  2. use a goto pastTheIfElse; instead of break;.

If the pastTheIfElse label marks a return (like it does in your code), then you can directly return instead of the goto.

#include <stdio.h>

int main() {
    while (height > 0) {
        if (throttle >= 0 && throttle <= 100) {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp   velocityTemp - (throttle * K-G) / 2;
            velocity = velocityTemp   (throttle * K-G);
            fuel = fuelTemp - throttle;
            time = time   1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;   
        }
        else {
            printf("Please choose a number between 0 and 100! \n");
            goto pastTheIfElse; //instead of break;
            //OR in this case: `return 0;`
        }
    }

    if (velocity >= -2.0) {
        printf("You landed successfully: ");
    } else {
        printf("Failed! You crashed");
    }
    pastTheIfElse:

    return 0;
}

The goto solution is what an optimizing compiler should optimize the didBreak version into, so some prefer it, while others are really really strongly against it because "goto considered harmful".

CodePudding user response:

Just assign a certain value to a variable if the loop breaks, and add an if condition that checks that value.

#include <stdio.h>

int main() {
    int didBreak = 0;

    while (height > 0)
    {
        if (throttle >= 0 && throttle <= 100)
        {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp   velocityTemp - (throttle * K - G) / 2;
            velocity = velocityTemp   (throttle * K - G);
            fuel = fuelTemp - throttle;
            time = time   1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;

        }
        else
        {
            printf("Please choose a number between 0 and 100! \n");
            didBreak = 1;
            break;
        }
    }

    if (didBreak == 0)
    {
        if (velocity >= -2.0)
        {
            printf("You landed successfully: ");
        }
        else
        {
            printf("Failed! You crashed");
        }
    }

    return 0;
}

CodePudding user response:

Replace the break; with return 0; or if you consider invalid input an error of value to the host environment return -1; (or other non-zero value).

If you want to be ultra-formal #include <stddef.h> and return EXIT_SUCCESS or return EXIT_FAILURE as you see applicable. Though use of those provided macro constants are rarely seen in either examples of real code.

Some C Standards hold functions should have only one exit point but obeying that sometimes leads to code that is just as contorted and unreadable as an early return where relevant.

  • Related