Thread: Is it limited of using "if"?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    12

    Is it limited of using "if"?

    Hello all:

    I am coding a program, due to the situation is complicated, I used
    several "if" in a row. I found that it is not allowed to use "if" repeatedly. Is this true? What's the rule to use "if"? --Zhao

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by YuminZhao
    I am coding a program, due to the situation is complicated, I used
    several "if" in a row. I found that it is not allowed to use "if" repeatedly.
    Perhaps you simply made a syntax error. Show the smallest and simplest program that you expect to compile but which demonstrates this error. What is the error message, anyway?
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is this true? What's the rule to use "if"?
    From a language point of view - it's fine to string as many if statements together as you want.

    Your local coding standards from your boss/tutor may however vary.

    Perhaps you should post some kind of example.

    Perhaps you're meant to simplify things as well, such as
    Code:
    if ( foo && bar )  {
      // stuff
    }
    if ( foo && baz ) {
      // stuff
    }
    becomes
    Code:
    if ( foo ) {
      if ( bar ) {
        // stuff
      }
      if ( baz ) {
        // stuff
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps you used many if's when you should have been using a switch statement. We wont ever know without seeing the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM