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");
}