Thread: Question With Nested If's

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27

    Question With Nested If's

    Code:
    scanf ("%c", i);
    
    if (i=='a')
    {
         printf ("the user enterered a thus it entered if");
              scanf ("%c", i);
              if (i=='a')
              {
                    printf ("the user still entred a");
              }
    }
    
    else printf ("the user did not enter a");
    this is a simplified form of my question, if the user did not enter 'a' on the second if statement, will it go straight to else?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    problem 1:
    -scanf needs the address of 'a' as in &a

    but to answer your question assuming the first line works... no
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why not compile it and find out? No. The else binds to the first if.


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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Consider the following code:

    PHP Code:
    char c1c2;

    scanf("%c", &c1); /* Get a character */
    scanf("%c", &c2); /* Get another character */

    if (c1 == 'a')
    {
        
    printf("User entered an 'a' first ");
        if (
    c2 == 'a')
        {
            
    printf("and also the second was an 'a'\n");
        }
        else
        {
            
    printf("buf the second was not an 'a'\n");
        }
    }
    else
    {
        
    printf("First character was not an 'a' ");
        if (
    c2 == 'a')
        {
            
    prinf("but the second one was!\n");
        }
        else
        {
            
    printf("and neither was the second one!\n");
        }

    And see what the effect is .... enter every combination of a and something else and try to predict what the program will output.
    I think you can put a signature here.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i'm sorry for the first one, forgot the & in there, yes i tried to compile it but the program just crash after i entered the character..

    i already tried the code above, that was my original idea for my program but then it is a very long code..i'm trying to shorten my program now..

    is my question above possible? like if the second if statement is false, it will do directly to the first else?

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    possible?
    Code:
    scanf ("%c", &i);
    
    if (i=='a')
    {
         printf ("the user enterered a thus it entered if");
         scanf ("%c", &i);
         if (i=='a')
               printf ("the user still entred a");
    }
    if(i!='a')
         printf ("the user did not enter a");
    that's one way. Although its pretty silly at this point
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    by the way, thanks for all the peepz helping me out here..

    here's the segment of the program i'm trying to do..

    Code:
    scanf ("%c", &cAction);
    if (cAction=='H' || cAction=='h') //if hte player hits
    {
         //codes to deal the third card from the player..
    
              scanf ("%c", &cAction);
              if (cAction=='H' || cAction=='h')
              {  //codes to deal the fourth card..
    
               }
    
    }
    
    else //meaning if the user choose to stand..
    {// codes to when the user stands..
    }
    the else part, where the user stands, is quite long and putting the same code all over again when it's needed will make it long, so i thought if i'll put em in a nested if, i thought that anytime the if statements inside would return false, it will immediately go to this else, i was trying to make an else statement for multiple if's!

    this is a blackjack game by the way..

    i cant use fuctions, it's hard to explain..

    what do i do?

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    edit:
    scratch that. I mentioned switch, but I forgot about the original requirement that it asks twice.

    my thought would be to loop.
    for(i=0; i < 2 && (a == 'H' || a == 'h'); i++).....

    and then instead of the else, check for the condition (a == 'H' || a == 'h') in an 'if'
    Last edited by FillYourBrain; 07-26-2006 at 08:10 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. C problem
    By shibz101 in forum C Programming
    Replies: 6
    Last Post: 09-17-2001, 07:57 AM