0

I have a problem with the else if statement. It only prompts one time and if the number is still incorrect then it is just ends. The else statement is working fine for as long you write something else than numbers it's prompting "Retry :". I changed my variable and put it outside the main function. I wanted to create a function because it would be a lot easier but I don't know if it is appropriate and approved so I went by everything we learned in week 0 and 1. Thanks and sorry for the long message.

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>


int h;

int main(void)
{
//Declaring some variables
    int i, y, rows;

//Printing "Height:" and asking for an integer
    printf("Height: ");
    h = GetInt();


//If the input is between 0 and 23 then it's executing the loops
if ( (h >= 0) && (h <= 23) )
    { 
      //the main code..
        print("Good Job");
    }
    else if ( h < 0 || h > 23)
    {
        printf("Height: ");
        h = GetInt();
    }
    else
    {
        printf("Retry: ");
        h = GetInt();
    }
}
2
  • That is exactly how an if block works. It will only execute once. There's nothing in if that will make it repeat. Watch the lectures and Zamyla's walkthrough where loops are explained :)
    – Irene
    Sep 11 '15 at 22:50
  • I thought by re-assigning the value of h it would check all the conditions again. My mistake, thanks for your suggestion :)
    – Vyronas
    Sep 11 '15 at 23:40
1

I think there's more going on than you are aware of. First, you need to fully understand how the GetInt() function works. If you enter something that is not an integer, it will, on it's own, type Retry: on the screen. Perhaps you think that part of your code is doing that?

Putting your h variable outside changed it from being a local variable in main to being a global variable. Global variables are generally a bad idea because you never know what outside function will try to change it, and it weakens the compartmentalization of main() and any other functions that may be written. There are times when global variables are apporpriate, but should be used only when necessary and appropriate, as will be demonstrated in a later lesson.

It also appears that you have not quite grasped the difference between an if or if/else statement and a loop (for, while, or do/while loops).

IF and IF/ELSE statements execute exactly once. If the condition in the if statement is true, the code that follows (before the next semicolon or within curly braces) will be executed once. In the case of if/else, then either the code following the IF will execute, or the code following the ELSE will execute. IF statements can be chained as in IF / ELSE IF... BUT, be careful about knowing when to use ELSE IF vs. two consecutive IF statements. For IF - ELSE/IF, the second IF will only be checked if the first one is false. With two consecutive IF statements, both will always be checked.

On the other hand, a LOOP will execute as few as 0 times or can run forever.

A for loop will execute as many times as directed by the FOR statement. Generally, a for loop will be designed to run for a fixed number of passes, but it is possible to create an infinite loop.

A while statement will run for an undetermined number of times, and will terminate when the test conditiion fails. The test condition is checked at the beginning of the loop. In a while loop, it's less about how many times it runs and more about the test. In a while loop, it is possible that it may not run at all. This happens when the test condition fails. Because the test is checked at the start of the loop, it is possible that the test will fail on the first pass and the loop will never be executed. Instead, the code will drop down to the next line of code after the loop and continue.

Finally, a do/while loop works almost identically to a while loop, except that the test is checked after the loop code is executed. That means that the loop will always execute at least once, befor the loop condition is tested. When the test fails, the loop will stop executing and the next line of code following the loop will be executed.

So, as you see, your code as written executes linearly and only once. The flow is this: it will go to the first GetInt() and stay in GetInt until an integer ( + or - ) is entered. Next, if it is between 0 and 23 inclusive, it will print "Good job" and end. If it is < 0 or > 23, then it will call GetInt() again and keep "Retry: "ing until it gets any integer, after which it will exit the program again.

Because of the structure, it is impossible for the code following the final else to ever execute. There is no way for the test conditions to ever get to that point.

That's what's happening here. It should give you some food for thought. If you need convincing, try changing your printf texts so that no two are alike, and none of them say "Retry:" (maybe "try again" instead).

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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