2

Whenever I put in more than one line in the do/while loop, I get this error.

mario.c:9:7: error: expected 'while' in do/while loop
  printf("Nope, again please,\n");
  ^
mario.c:7:5: note: to match this 'do'
  do 
  ^
1 error generated.

This is how I wrote the do-while loop.

do 
      n = GetInt();
      printf("Nope, only betwen 0 23 please,\n");
while (n < 0 || n > 23 || n == 0 );

2 Answers 2

4

The syntax for do-while loop goes like this

do
{
    //do something here
} while(test_condition);

It is better to use braces {} every time you use loops. Although loops work even without their use, but using braces provide readability to your code and avoid such confusions. So you should do this instead of what you mentioned.

do 
{
  n = GetInt();
  printf("Nope, only betwen 0 23 please,\n");
} while (n < 0 || n > 23 || n == 0 );
3

This error is because of the missing braces '{}'. Languages like python compile the code based on the indenting. However, C requires braces around do-while to compile correctly. Your code doesn't allow the compiler to reach the while statement. Also, C needs braces around a set of instructions you wish to execute inside a conditional statement or loop, if the statements are more than one. Eg:

if(something)
     do this line
continue code

will work correctly. However,

if(something)
     do this.
     do this too
continue code

will not work. Instead use,

if(something)
{
    do this.
    do this too.
}
continue code

will work correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .