home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!think.com!ames!ncar!noao!stsci!kimball
- From: kimball@stsci.edu (Timothy Kimball)
- Subject: Re: C if else statements HELP
- Message-ID: <1993Jan8.174706.4337@stsci.edu>
- Sender: news@stsci.edu
- Organization: Space Telescope Science Institute
- X-Newsreader: TIN [version 1.1 PL6]
- References: <1993Jan8.160740.22055@magnus.acs.ohio-state.edu>
- Date: Fri, 8 Jan 1993 17:47:06 GMT
- Lines: 61
-
- Christopher M Toth (ctoth@magnus.acs.ohio-state.edu) wrote:
- : Hi! I just started learning C and have run into problems with
- : 'if' and'else' statements. I have a program that I am *trying* to write. The
- : basic format is:
- :
- : printf ("which do you choose?");
- : scanf("%d", &n);
- : choice#1
- : choice#2
- : choice#3
- : choice#4
- :
- : if choice #1
- : printf.....
- : if choice #2
- : printf.....
- : if choice #3
- : printf.....
- : if choice #4
- : printf.....
- :
- : I want the program to goto the approiate section when the user
- : inputs either 1,2,3,or 4.
-
- Do you want to _goto_ the appropriate section of the program,
- or do you want to execute the appropriate lines?
- Gotos are possible in C, but discouraged for anything but
- serious error handling, and even then not without some work.
-
- If this is your main(), then try something like this:
-
- printf ("which do you choose?");
- scanf("%d", &n);
-
- if (n == 1)
- printf(...);
- else if (n == 2)
- printf(...);
- else if (n == 3)
- printf(...);
- else if (n == 4)
- printf(...);
-
- If you want to do more than one thing for each option,
- then use blocks:
-
- if (n == 1) {
- printf(...);
- printf(...);
- } else if (n == 2) {
- printf(...);
- printf(...);
- } else if (n == 3) {
- printf(...);
- printf(...);
- } else if (n == 4) {
- printf(...);
- printf(...);
- }
-
- Hope this helps some. -- tdk
-