Thread: Trouble with IFs

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Trouble with IFs

    Rather than having to input :

    If (number= (100*1)+10){
    printf("number times a hundred plus ten");

    If(number=(100*1)+10){
    printf("number times a hundred plus ten");

    etc...

    Isn't there a way i can condense that into one IF with an Or ||... i've tired it but what ends up happening is any number sent to this subroutine prints the message even though i only want it to print if it specifically meets ((100*1)+10) or ((100*2)+10) etc.

    What am i doing wrong ?

    Please and thank you .

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    = is assignment
    == is comparison
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Oops, my mistake i meant ==, but yeh even then the problem occurs. Cant i set multiple parameters in a single IF ? If so, how ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Please post your ACTUAL code, not paraphrased code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    If (number= =(100*1)+10){
    printf("number times a hundred plus ten");

    If(number==(100*1)+10){
    printf("number times a hundred plus ten");


    Although paraphrased this is essentially the exact same, all i want to know is if i can convert the two IFs above into just one.


    If (number= =(100*1)+10) AND HAVE THIS IN HERE TOO (number==(100*1)+10){
    printf("number times a hundred plus ten");

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They test the same condition, so you could write:
    Code:
    if (number == (100*1)+10) {
        printf("number times a hundred plus ten");
        printf("number times a hundred plus ten");
    }
    If they were different, you could join them with an || (or).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (condition1)
        if (condition2)
            some_code;
    Is the same as
    Code:
    if (condition1 && condition2)
        some_code;
    If condition1 AND condition2
    It's also possible to do:
    Code:
    if (condition1 || condition2)
        some_code;
    If condition1 OR condition 2.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Trouble with If's and Else's
    By GlitchGuy2 in forum Game Programming
    Replies: 3
    Last Post: 11-07-2008, 05:24 AM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  5. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM