Thread: Really newbie question about if

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Really newbie question about if

    can you have more than 1 if else statement in an if statement
    ie.

    if(x>=64)
    {
    cout << "you are dead.";

    else if(x<=11)
    cout << "you are young.";

    else if(x<19 && x>=12)
    cout << "you are a teen.";

    else if(x<=63 && x>=20)
    cout << "you are an adult.";
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can you have more than 1 if else statement in an if statement
    Yes.
    Code:
    if(x>=64)
    {
    cout << "you are dead.";
    
    else if(x<=11)
    cout << "you are young.";
    
    else if(x<19 && x>=12)
    cout << "you are a teen.";
    
    else if(x<=63 && x>=20)
    cout << "you are an adult.";
    }
    No. This is an illegal construct because every else must have a matching if. Because you placed the else statements inside the first if, it will not work. This will though:
    Code:
    if(x>=64)
      cout << "you are dead.";
    else if(x<=11)
      cout << "you are young.";
    else if(x<19 && x>=12)
      cout << "you are a teen.";
    else if(x<=63 && x>=20)
      cout << "you are an adult.";
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    4
    thanks that really helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM