home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19431 < prev    next >
Encoding:
Text File  |  1993-01-08  |  1.7 KB  |  74 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!think.com!ames!ncar!noao!stsci!kimball
  3. From: kimball@stsci.edu (Timothy Kimball)
  4. Subject: Re: C if else statements HELP
  5. Message-ID: <1993Jan8.174706.4337@stsci.edu>
  6. Sender: news@stsci.edu
  7. Organization: Space Telescope Science Institute
  8. X-Newsreader: TIN [version 1.1 PL6]
  9. References: <1993Jan8.160740.22055@magnus.acs.ohio-state.edu>
  10. Date: Fri, 8 Jan 1993 17:47:06 GMT
  11. Lines: 61
  12.  
  13. Christopher M Toth (ctoth@magnus.acs.ohio-state.edu) wrote:
  14. :     Hi! I just started learning C and have run into problems with
  15. : 'if' and'else' statements. I have a program that I am *trying* to write. The
  16. : basic format is:
  17. :     printf ("which do you choose?");
  18. :     scanf("%d", &n);
  19. :     choice#1
  20. :     choice#2
  21. :     choice#3
  22. :     choice#4
  23. :     if choice #1
  24. :         printf.....
  25. :     if choice #2
  26. :         printf.....
  27. :     if choice #3
  28. :         printf.....
  29. :     if choice #4
  30. :         printf.....
  31. :     I want the program to goto the approiate section when the user
  32. : inputs either 1,2,3,or 4.
  33.  
  34. Do you want to _goto_ the appropriate section of the program,
  35. or do you want to execute the appropriate lines?
  36. Gotos are possible in C, but discouraged for anything but
  37. serious error handling, and even then not without some work.
  38.  
  39. If this is your main(), then try something like this:
  40.  
  41.     printf ("which do you choose?");
  42.     scanf("%d", &n);
  43.  
  44.     if (n == 1)
  45.         printf(...);
  46.     else if (n == 2)
  47.         printf(...);
  48.     else if (n == 3)
  49.         printf(...);
  50.     else if (n == 4)
  51.         printf(...);
  52.  
  53. If you want to do more than one thing for each option,
  54. then use blocks:
  55.  
  56.     if (n == 1) {
  57.         printf(...);
  58.         printf(...);
  59.     } else if (n == 2) {
  60.         printf(...);
  61.         printf(...);
  62.     } else if (n == 3) {
  63.         printf(...);
  64.         printf(...);
  65.     } else if (n == 4) {
  66.         printf(...);
  67.         printf(...);
  68.     }
  69.  
  70. Hope this helps some. -- tdk
  71.