While Loop in C

I have a few questions regarding this code. I have tried all these scenarios, and not sure how or why the code is behaving in certain ways.

Q1. Lines 5-6. Given both the ‘guess’ and ‘tries’ variables are of the same type (if type has an impact to begin with), why are we assigning a ‘0’ value to tries variable but nothing to guess variable?

Q2a. What happens if we assign a ‘0’ value to guess variable? (no effect in result, but why)?

Q2b. What happens if we assign any other number to guess variable, except for 8?

Q2c. What happens if we assign an ‘8’ value to guess variable?

Q3. When we assign a ‘0’ to tries variable, does that mean it’s already occupying the 0th term? And can now take only 1, 2, 3 and 4th attempts before the code stops working? If so, then why do we get 6 attempts in total? Even if we are counting in the 0th attempt, should it still not ignore the 6th attempt (tries =5) given the code states ‘tries < 5’?

Q4. When we assign tries = 4 in line 6, we get two attempts, tries = 5 leads to one attempt… However, I can still have an attempt even when tries = 6 or any higher number. Why?

Q5. If we want to add a message such as “You Failed” after all the attempts are exhausted, how do we do that?

#include <stdio.h>

int main() {

int guess;
int tries = 0;

printf(“I’m thinking of a number in the range 1-10.\n”);
printf(“Try to guess it: “);
scanf(”%d”, &guess);

while (guess != 8 && tries < 5) {
printf(“Wrong guess, try again: “);
scanf(”%d”, &guess);
tries++;
}

if (guess == 8) {
printf(“You got it!\n”);
}

}

Q1. The value for guess is expected to be provided by the user. The first time we try to access the value assigned to the guess variable is when evaluating the condition for the while loop. By that time, the scanf statement has already read and assigned a value to guess. Whereas, if we don’t initialize the tries variable, then by the time we reach the condition for the while loop, tries will still be uninitialized. This can cause undefined behavior. Depending on the programming language, uninitialized variables may be assigned default values OR they may contain some garbage value OR your program may crash OR your output may keep changing randomly.

Q2A. Initializing guess doesn’t cause any problem. The scanf statement will replace the initial value with a value provided by the user. Between the declaration of guess and the scanf statements, there are no other statements involving guess, so initialization doesn’t cause any discernible effects.

Q2B. & Q2C. Doesn’t matter what initial value you choose. Even if you initialize it as int guess = 8; or any other integer, the scanf will replace this initial value with the user provided value. If the scanf statement was absent, then the initialization does matter. If you comment out or delete the scanf statement and initialize guess as 8, then the while loop will be skipped. But, in the presence of the scanf statement, the initial value will just be replaced with the user provided value.

Q3. The way the code is written in the screenshot, it allows 6 attempts. To correct it, another tries++; statement can be added immediately after the first scanf statement. That will limit the maximum allowed attempts to 5. The code in the screenshot in its current form allows 6 attempts.

  • tries is initialized as 0.

  • Then the first guess is scanned, but tries isn’t incremented and remains 0. This is causing the 6 attempts. (To correct it, you can add another tries++; statement after the first scanf but before the while loop).

  • The condition of the while loop is evaluated. If the condition is true, the body of the while loop is executed.

Q4. See answer to Q3 above for why an additional attempt is happening and how it can be corrected with an additional tries++; statement. As for the second part, if tries is initialized as 6 or any higher number, one attempt will nevertheless be allowed. The reason is that the first two printf and the scanf statements are positioned before the while loop and hence, the condition hasn’t been evaluated yet. You could use an if statement to handle this situation,

int guess;
int tries = 7;

if (tries < 5) {
   printf("I'm thinking ...");
   printf("Try to... ");
   scanf(...);
   tries++;
}

while (guess != 8 && tries < 5) {...

However, if you decide to do this, then you should also make sure to initialize guess with a dummy value. For Example, int guess = 0;. This is necessary because if tries has been initialized as 5 or greater, than both the if condition and while loop will be skipped and you will encounter the if (guess == 8) {... statement. If you haven’t initialized guess and all the scanf statements have been skipped, then you are liable to run into undefined behavior when evaluating the expression guess == 8.

Q5. If you reach the last if statement (which is outside the while loop), then one of two things has happened. EITHER you guessed successfully OR you failed to guess correctly and have exhausted all attempts. If you guessed successfully, then the last value assigned to the guess variable will be 8 and the condition if (guess == 8) {... will be true. If you failed and have exhausted all attempts, then the last value assigned to guess would be some value other than 8. So, you could just do:

if (guess == 8) {
    printf("You got it");
} else {
    printf("You failed to guess the number");
}
2 Likes