Thread: nest if statements

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    10

    nest if statements

    How can i write the same code using a nested if statement instead of a series of if statements?

    /* Reads a positive single digit integer and prints the word equivalent for it.
    for example. 5 = five. */
    #include<stdio.h>
    void main()
    {
    int a, keep=1;
    while(keep>=0)
    {
    printf("\nEnter a single digit: ");
    scanf("%d",&a);
    if(a==0)
    printf("You have entered zero");
    if(a==1)
    printf("You have entered one");
    if(a==2)
    printf("You have entered two");
    if(a==3)
    printf("You have entered three");
    if(a==4)
    printf("You have entered four");
    if(a==5)
    printf("You have entered five");
    if(a==6)
    printf("You have entered six");
    if(a==7)
    printf("You have entered seven");
    if(a==8)
    printf("You have entered eight");
    if(a==9)
    printf("You have entered nine");
    printf("\nEnter a positive number to continue or a negative number to terminate ");
    scanf("%d",&keep);
    }
    printf("\nTerminating");
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Use the switch statement:
    Code:
    switch(a)
    {
       case 0: printf("You have entered zero"); break;
       case 1: printf("You have entered one"); break;
       ...
    }
    or use else if:
    Code:
    if(a==0)
       printf("You have entered zero");
    else if(a==1)
       printf("You have entered one");
    else if(a==2)
       printf("You have entered two");
    ...
    Or use an array:
    Code:
    char *digits[] = { "zero", "one", "two", "three", "four",
                      "five", "six", "seven", "eight", "nine" };
    
    ...
    
    if(a >= 0 && a < (sizeof(digits)/sizeof(*digits)))
       printf("You have entered %s\n", digits[a]);
    Last edited by Monster; 05-21-2004 at 04:24 AM.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If you are doing what Monster is doing, you could use enum.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    u cannot use a nested if statement for whatever u want to do.
    a nested if stucture is something like
    Code:
    if(cond1)
    {
    if(cond2)
    {
    if(cond3)
    {
    ...and so on....
    }
    }
    }
    so what u r doing i snot possible using nested if. i think all the other ways possible (other than what u have done) have been explained by Monster.
    even a fish would not have been in danger had it kept its mouth shut.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gooddevil
    u cannot use a nested if statement for whatever u want to do.
    Yes you can.
    Code:
    printf("You entered ");
    if( a > -1 )
    {
        if( a > 0 )
        {
            if( a > 1 )
            {
                if( a > 2 )
                {
                    ...and so on...
                }
                else
                {
                    printf("two");
                }
            }
            else
            {
                printf("one");
            }
        }
        else
        {
            printf("zero");
        }
    }
    else
    {
        printf("an invalid number");
    }
    printf(".\n");


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    yes i am sorry, my fault. i din think it that way.thanx for corrections
    even a fish would not have been in danger had it kept its mouth shut.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    a nested if style uses just as much code as clover's.
    i would use monster's array method.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sand_man
    a nested if style uses just as much code as clover's.
    i would use monster's array method.
    Actually, it uses more due to the else statements. It wasn't a question of if it was a better way; it was an an answer showing that it could be done. The poster just prior to that stated it couldn't be done. I was illustrating that it can indeed be done.

    Additionally, if this were a homework requirement, which it may well have been, due to the way the first post was worded, the exercise may require they use nested if statements.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM