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