-
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
-
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.
-
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.
-
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
-
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.
:confused:
-
-
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");
}
}
}
-
It's an if statement nested inside of another one. Get it?