Search code examples
cif-statementreturngoto

Return to the beginning of an if statement


I was wondering if there was any way to return to the beginning of an if statement when the user enters something we do not want him to enter. For example:

#include <stdio.h>

int main()
{
    int number1;

    printf("Please enter a number:"); scanf("%d", &number1);

    if(blablabla)
        printf("blabla");
    else if(blablabla)
        printf("blabla");
    else if(blablabla)
        printf("blabla");
    else
        /*I want to return to the beginning of this if statement so the user can try again*/
}

I saw some things on goto, but I am not sure it is the way to go... Thank you very much! :]


Solution

  • Try a while statement:

    int valueIWantItToBe = 1;//Whatever value you want
    do {
        printf("Please enter a number:"); scanf("%d", &number1);
    } while (number1 != valueIWantItToBe);