Thread: Okay, newbie right here

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Okay, newbie right here

    I just started C++ (moved from Java in first marking period) and we like... Just started, and my teacher tells us to use the 'nested if statement', Can anyone point me off in the right direction, or give me some PARTS of code.. please help..
    Heres what we have to do:

    Write code that will multiply some number by 2 if the number is between 1 and 100 (including 1 or 100) and if it is evenly divisible by 3; otherwise multiply by 3 if it is between 1 and 100 but not divisible by 3; finally, if it isn’t between 1 and 100, multiply the number by the number modulus 100. Hint: Use the nested if statement

    Steve

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    if(number>1 && number<100)
    { 
          //number is between 1 and 100
    
             if(!number%3)
             {
                //At this point, number is between 1 and 100
                //and number is evenly divisble by 3
             }
    }
    
    else
    { 
         //number did not qualify as being between 1 and 100  
         //nor did it qualify as being evenly divisible by 3
         
         number = number*number%100;
    }

    This is no different than how you would do it in java.
    Last edited by The Brain; 04-21-2005 at 07:17 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Well, I take it you already know what an if statement is. A nested if is just an if inside an if. I suggest beginning by writing out some pseudocode or a diagram that clearly shows how your program will branch off.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Well, I didn't expect all the code, but I really appreciate it, and now I can see what it all does. Thanks for the comments, too. Thanks The Brain

    And thank you, too, joshdick.

    Steve

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    A nested if statement is like the '&&' operand.

    Consider:

    If 'a' and 'b' =0 then printout 'yes'
    Code:
    if ((a==0)  && ( b==0))
    {
       cout<<"yes";
    }

    which is the same as saying...

    Code:
    if(a==0)
    {
      if(b==0)
      {
        cout<<"yes";
       }
    }

    So given that example how would you apply that to your given question? Come back with some code next time.


  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Okay, sorry

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I have rustled up something in java to help you get going in c++

    Code:
    //Write code that will multiply some number by 2 if 
    //the number is between 1 and 100 (including 1 or 100) 
    //and if it is evenly divisible by 3; otherwise multiply by 3 
    //if it is between 1 and 100 but not divisible by 3; 
    //finally, if it isn’t between 1 and 100, output that it isn't.
    //Hint: Use the nested if statement
    
    
    class MessWith{
        public static void main (String[] arguments)
        {   //edit number here
            int some_number=9;
            
            //do not edit below this line
            if(some_number>1)
            {
                if(some_number<100)
                {
                   if(some_number%3==0) 
                   {
                       System.out.println(some_number+" is between 1-100 and divisible by 3");
                   }
                   else
                   {
                   System.out.println("I shall times "+some_number+"  by 3 ="+some_number*3);
                   }
                }
                else 
                {
                    System.out.println("Not between 1 and 100");
                }
            }
            else 
            {
                    System.out.println("Not between 1 and 100");
            }
            
               
            
            
        }
    }

  8. #8
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    It's an if statement nested inside of another one. Get it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM