Thread: questions

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    questions

    1
    Code:
    for (d = 2; d < p; d++)
          if (!(p%d))
               isPrime = 0;
    what does the '!' in the 'if' statement means?


    2. What is printed out by the following poorly indented C code fragment?
    Code:
    int x = 5, y = 0;
    if(x < 3)
    if(y > 4)
    if (x < 5)
    y++;
    else
    y--;
    printf("%i\n", y)
    Why is the ans zero? shouldn't it be 1?

    TIA.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) It's called the NOT operation:
    !1 == 0
    !0 == 1

    2) Why do you think it should be 1? Explain in detail.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by Sebastiani View Post
    1) It's called the NOT operation:
    !1 == 0
    !0 == 1

    2) Why do you think it should be 1? Explain in detail.
    Sorry, not 1, i thought y-- will be carried out instead so it should be -1 right?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think y-- will be carried out?

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Why do you think y-- will be carried out?
    since all the if statements isn't true and the else statement will be carried out?

    sigh, my concept still ain't good.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think all the if statements will even be checked?

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Why do you think all the if statements will even be checked?
    ok so ur saying the entire if else statement will all be truncated, and the initial value of y is printed instead?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know if I like the word truncated here, but maybe. Think about how if works: you have "if", then you have a condition in parentheses, and then the next complete statement (or block of statements, if we start off with a curly brace) is done when the condition is true. So for that first if-statement, ask: what is the first complete statement following that condition?

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    This screams for indention. . . Indent it the way you think it branches (we'll tell you whether you're correct), then you'll understand.

    [edit] Or, tabstop can just spoon feed you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM