Thread: If/Else statements

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    14

    If/Else statements

    I apologize if this sounds silly, as I'm completely new to programming.

    My question is is that are you not allowed to use additional if clauses after an else? Is there a way around it?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    You can use

    Code:
    if (..)
      do this
    else if (..)
      do that
    else if (..)
      ....
    else
    But maybe I got the question wrong, you can start as many if clauses after an else as you want
    Last edited by Gauloises; 06-03-2011 at 07:00 AM.

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Use an else if before the else
    Code:
    if (first)
    {
        ...
    }
    else if (second)
    {
        ...
    }
    else if (third)
    {
        ...
    }
    else // All others
    {
        ...
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Thank you for the help! I did a math problem using an 'if' and an 'else' clause. After that I wanted to calculate a separate entity that is dependent on the original math problem, so I tried using an 'if' clause after the 'else' clause of the math problem, but it ignored my 'if' statement. So are you saying that if I use an 'else if' clause for the separate entity it will work?

  5. #5
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Can you post the code that did not work like you wanted?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The one thing that we can guarantee is that it did not "ignore your if statement". If you put the if statement inside the else clause, then naturally it will only happen when the else clause happens. If the test of the if statement isn't true, then nothing will happen unless you provide an else to go with it. You should post your code to show what you mean.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Thank you everyone for your help. I realized I made a silly mistake of adding a semicolon after the if statement. After I deleted that everything worked out. Thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If else statements
    By Timbo8 in forum C++ Programming
    Replies: 15
    Last Post: 01-03-2007, 07:08 PM
  2. Help in C with 'if' statements
    By jamestgarner in forum C Programming
    Replies: 5
    Last Post: 09-07-2006, 01:41 AM
  3. If statements :P
    By Pyroteh in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2005, 01:37 PM
  4. for statements
    By Lyanette in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2003, 07:15 PM
  5. Help with IF statements
    By boontune in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2003, 08:26 AM