Thread: question

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    question

    I need some help with this:
    Given: int x,y,z What are the values of x,y and z after the following code segments have been executed?

    x=2; y=0; z=2;
    if(x)
    {
    if(y)
    z=1;
    }
    else
    z=3;

    The answer is x=2, y=0, and z=2 I have no clue how to do this. This is my first programming class and I would greatly appreciate any help on this! Thanks so much

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well the answer is correct.
    What is happening is that if(x) is true so it goes on to the next which is if(y). Here it is false so it is skipping the z=1 command. Since the first IF is correct it is skipping the ELSE. The values have not been changed.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    I have another for ya:
    x=3;
    y=0;
    z=1;

    if(x)
    {
    if(y)
    if(z)
    y=4;
    else
    x=2;
    }
    else
    z=5;

    The answers are x=3, y=0, and z=1
    I dont really understand how to get those answers. Again thank you so much for all of your help!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Maggie,

    Your instructor is asking you very good, basic questions to get you familiar with loops and truth tests.

    Look at it this way (and keep in mind Thantos's post as you read this):

    If (x)
    printf("x is TRUE\n");

    else
    printf("x is FALSE\n");


    The above If statement will execute if x evaluates to true. In C, "true" means any non-zero number. So if X is -2, 5, 100, or even 0.1, the If statement will execute. The If statement will not execuate if x is false. In C, "false" means the value zero.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    Thanks alot for your help! I understand now how to do these things. I really appreciate it!

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Another thing you may want to do is put spacing and {} in the program. It might make things clearer.

    Code:
    int main(void)
    {
    int x,y,z;
    
    x=3; 
    y=0; 
    z=1; 
    
    if(x) 
      { 
      if(y) 
        {
        if(z) {y=4;}
        else {x=2;}
        }
      }
    else 
      z=5; 
    }
    I know for me it makes it a little bit more readable and easier to understand.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    That helped me alot! Its alot easier to read and understand whats happening. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM