regarding if statements

This is a discussion on regarding if statements within the C Programming forums, part of the General Programming Boards category; Code: while( foo ){ if( condition 1 ) do something; if( condition 2 ) do something else; if( condition 3 ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    regarding if statements

    Code:
    while( foo ){
    
    if( condition 1 )
        do something;
    
    if( condition 2 )
        do something else;
    
    if( condition 3 )
        do something else again;
    }
    So my question is about how I used my if statements. Is that a proper way to write code?
    It may seem like a dumb question, but I would like an opinion.

    ~/kermit

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I think what I am asking is what is the difference between what I posted here and using if / else if statements

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It depends on what you want your code to do.

    Here's what I think you may have meant:
    Code:
    while( foo )
    {
      if( condition 1 )
      {
        do something for cond 1;
      }
      else if( condition 2 )
      {
        do something for cond 2;
      }
      else if( condition 3 )
      {
        do something for cond 3;
      }
      else 
      {
        do something if all the above were false
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Yes, I meant what you have written there. The thing I was wondering about was if it is proper to leave the else part off the if, so instead of
    Code:
    if( condition 1 )
      {
        do something for cond 1;
      }
      else if( condition 2 )
      {
        do something for cond 2;
      }
      else if( condition ...
    and so on (as you wrote), you would have,
    Code:
    if( condition 1 )
      {
        do something for cond 1;
      }
      if( condition 2 )
      {
        do something for cond 2;
      }
      if( condition...
    where the else is omitted.

    The only reason I ask is that I have seen this sort of thing in various places online, yet in my C books, I only see what you wrote with 'else if' - I assume that it is proper to write the way you demonstrated, but the other way is somewhat 'legal'?

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Both ways are legal... but if condition1 and condition2 are both true, your code will execute the code for both, while the other code will only execute the code for condition1
    Away.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Ah, so it would be good to make sure that multiple conditions could not be true then when deciding which way to code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 07:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM

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