Trouble with IFs

This is a discussion on Trouble with IFs within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    = is assignment
    == is comparison
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,890
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    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, 04:24 AM
  3. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  4. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  5. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21