Thread: break & switch statement

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    3

    Post break & switch statement

    I have just started learning C language. I'm using a book by Greg Perry & Dean Miller. Explaining the "break & switch" statements they have as an example the code that follows. At the end of the program they use the following statement: - while ((choice < 1) || (choice> 5)); - Compiled, the program works. In my limited knowledge of C language, it should not work for the variable "choice" is actually larger than 1 and smaller than 5. The expresion ought to read - while ((choice > 1) || (choice < 5)); - instead. Your input would be greatly appretiated. Thanks
    
    
    Code:
    /* The code listing from chapter 17 of The Absolute Beginner's Guide to C
    Teaching new programmers to create kick-butt code since 1941! */
    /* A Dean Miller joint */
    /* Filename Chapter17ex1.c */
    /* This program presents a menu of choices, gets the user's choice,
       and then uses the switch statement to execute a line or two of
       code based on that choice. What the user wants to do is never
       truly implemented, it is a series of stubs to teach the value
       of the switch statement */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    
        {
        int choice;
        printf("What do you want to do?\n");
        printf("1.- Add a new contact\n");
        printf("2.- Edit existing contact\n");
        printf("3.- Call existing contact\n");
        printf("4.- Text existing contact\n");
        printf("5.- Exit\n");
        do
    
        {
            printf("Enter your choice: ");
            scanf(" %d", &choice);
            switch (choice)
    
                {
                case (1): printf("\nTo add you will need ");
                     printf("the contact's first name, ");
                     printf("last name and number.\n");
                     break;
                case (2): printf("\nGet ready to enter the ");
                     printf("name of the contact ");
                     printf("you wish to edit.\n");
                     break;
                case (3): printf("\nWhich contact do ");
                     printf("you wish to call\n");
                     break;
                case (4): printf("\nWhich contact do ");
                     printf("you wish to text\n");
                     break;
                case (5): exit(1); // exits the program early
                     break;
                default: printf("\n%d is not a valid choice.\n", choice);
                     printf("Try again\n");
                     break;
                }
            }
    
            while ((choice < 1) || (choice > 5));
    
        return 0;
    
        }
    
    




    • Ignore
    • Add to Dictionary







    • Ignore
    • Add to Dictionary
    Last edited by LGCarmona; 10-22-2018 at 03:33 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on what you think the purpose of the loop is, I suppose. Based on what's printed, I would say that we want to keep looping if the user has entered an invalid number (too big or too small), so that is how the loop is written. If the value of choice is 1, 2, 3, or 4, we execute the single case and then stop.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    3
    Quote Originally Posted by tabstop View Post
    That depends on what you think the purpose of the loop is, I suppose. Based on what's printed, I would say that we want to keep looping if the user has entered an invalid number (too big or too small), so that is how the loop is written. If the value of choice is 1, 2, 3, or 4, we execute the single case and then stop.
    Im not clear, but thank you tabstop. It was my understanding that the loops purpose was for the program to cycle 1 to 5 in terms of choices for the user. Default within switch was to take care of invalid entry. Again thanks

    default: printf("\n%d is not a valid choice.\n", choice);
    printf("Try again\n");
    break;






    • Ignore
    • Add to Dictionary






    • Ignore
    • Add to Dictionary
    Last edited by LGCarmona; 10-23-2018 at 03:27 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by LGCarmona View Post
    Im not clear, but thank you tabstop. It was my understanding that the loops purpose was for the program to cycle 1 to 5 in terms of choices for the user. Default within switch was to take care of invalid entry. Again thanks
    That's an interesting idea, but it doesn't match what the code states it's purpose is, up there at the comment at the top:
    Code:
    This program presents a menu of choices, gets the user's choice,
       and then uses the switch statement to execute a line or two of
       code based on that choice.
    Once the user has chosen a thing, then the program's job is done; the only time it's going to ask for more input is if the original was an invalid choice. We're certainly not going to do 1 through 5 in order.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    3
    Quote Originally Posted by tabstop View Post
    That's an interesting idea, but it doesn't match what the code states it's purpose is, up there at the comment at the top:

    Once the user has chosen a thing, then the program's job is done; the only time it's going to ask for more input is if the original was an invalid choice. We're certainly not going to do 1 through 5 in order.
    Thank you, I follow the code as written, I undestand it, no problem. My question is regarding the "while" statement at the end of the program: - while ((choice < 1) || (choice> 5)); - In my limited knowledge of C language, the expresion ought to read - while ((choice > 1) || (choice < 5)); - instead.





    • Ignore
    • Add to Dictionary

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by LGCarmona View Post
    Thank you, I follow the code as written, I undestand it, no problem. My question is regarding the "while" statement at the end of the program: - while ((choice < 1) || (choice> 5)); - In my limited knowledge of C language, the expresion ought to read - while ((choice > 1) || (choice < 5)); - instead.
    Well, but then it would do exactly the wrong thing. We want to continue if the data is invalid, that is, the data is either less than 1 or greater than five.

    The other downside of your alternative is this: can you think of a number where (choice > 1) || (choice < 5) is false? There's not a lot of point in using a conditional that is always true.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    It is saying - "If the user has entered below 1 or above 5, do the loop again"
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 01:59 PM
  2. switch won't break
    By GiantFurby in forum C Programming
    Replies: 20
    Last Post: 10-15-2008, 12:51 PM
  3. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08:42 PM
  4. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM
  5. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM

Tags for this Thread