Problem with an "if/else" statement

 

Hello everyone!

I am working on an aggression level "if/else statement". Here's what I have so far;

if (AggLevel = 5)
{
Aggression = 200;//Leverage is 50:1
}else
{
if (AggLevel = 4)
{
Aggression = 250;//Leverage is 40:1
}
}else
{
if (AggLevel = 3)
{
Aggression = 400;//Leverage is 25:1
}
}else
{
if (AggLevel = 2)
{
Aggression = 1000;//Leverage is 10:1
}
}else
{
if (AggLevel = 1)
{
Aggression = 2000;//Leverage is 5:1
}
}else
{
if (AggLevel = 0)
{
Aggression = 10000;//Leverage is 1:1
}
}

I have put this within the int start() section (I imagine there is a much simpler way to code this, however I am very much a beginner and do not know an easier way).

It is giving me "illegal assignment" errors for the entire thing, which leads me to assume that I am either putting the function in the wrong place, or am not putting something in to make it work correctly.

Can anyone give me a direction that I need to go? I would greatly appreciate it!

Thanks!

 
Darko:

Hello everyone!

I am working on an aggression level "if/else statement". Here's what I have so far;

if (AggLevel = 5)
{
Aggression = 200;//Leverage is 50:1
}else
{
if (AggLevel = 4)
{
Aggression = 250;//Leverage is 40:1
}
}else
{
if (AggLevel = 3)
{
Aggression = 400;//Leverage is 25:1
}
}else
{
if (AggLevel = 2)
{
Aggression = 1000;//Leverage is 10:1
}
}else
{
if (AggLevel = 1)
{
Aggression = 2000;//Leverage is 5:1
}
}else
{
if (AggLevel = 0)
{
Aggression = 10000;//Leverage is 1:1
}
}

I have put this within the int start() section (I imagine there is a much simpler way to code this, however I am very much a beginner and do not know an easier way).

It is giving me "illegal assignment" errors for the entire thing, which leads me to assume that I am either putting the function in the wrong place, or am not putting something in to make it work correctly.

Can anyone give me a direction that I need to go? I would greatly appreciate it!

Thanks!

Option 1: fix your code

if (AggLevel == 5)
Aggression = 200;//Leverage is 50:1

else
if (AggLevel == 4)
Aggression = 250;//Leverage is 40:1
etc.


Option 2: implement in a function

if (AggLevel == 5)
{
Aggression = 200;//Leverage is 50:1

return();
}
if (AggLevel == 4)
{
Aggression = 250;//Leverage is 40:1

return();
}
etc.

}


Option 3: this is the how I'd write this sort of logic

switch AggLevel

{

case 5: Aggression = 200; break;

case 4: Aggression = 250; break;

etc...

}

 
Darko wrote >>

Hello everyone!

I am working on an aggression level "if/else statement". Here's what I have so far;

if (AggLevel = 5)
{
Aggression = 200;//Leverage is 50:1
}else
{
if (AggLevel = 4)
{
Aggression = 250;//Leverage is 40:1
}
}else
{
if (AggLevel = 3)
{
Aggression = 400;//Leverage is 25:1
}
}else
{
if (AggLevel = 2)
{
Aggression = 1000;//Leverage is 10:1
}
}else
{
if (AggLevel = 1)
{
Aggression = 2000;//Leverage is 5:1
}
}else
{
if (AggLevel = 0)
{
Aggression = 10000;//Leverage is 1:1
}
}

I have put this within the int start() section (I imagine there is a much simpler way to code this, however I am very much a beginner and do not know an easier way).

It is giving me "illegal assignment" errors for the entire thing, which leads me to assume that I am either putting the function in the wrong place, or am not putting something in to make it work correctly.

Can anyone give me a direction that I need to go? I would greatly appreciate it!

Thanks!

Hello Darko,

Your problem is with in each if statement, you have to put 2 equal signs instead of 1 and it will work

eg:

if (AggLevel == 5)
{
Aggression = 200;//Leverage is 50:1
}else
{
if (AggLevel == 4)
{
etc...

Moreover, it is advised in this case, to use a Switch case stament like cloud braker specified i option 3

Regards

 

I'm not a fan of switch-case as it is quite restrictive - like you can't compare strings or do complex expressions, so I normally just use if-else formatted like so:


if (AggLevel == 5) Aggression = 200; else //Leverage is 50:1
if (AggLevel == 4) Aggression = 250; else //Leverage is 40:1
if (AggLevel == 3) ...


Curly brackets not required if you only have a single statement within each if-else. Makes it easier to read. So, the choice is yours.

 

Thank you very much everyone! I will be implementing these and trying them out. As far as putting the one option into a function, will I be able to put it after int start() at any point to have it work fine, or do I need to put it beforehand?


Thank you again! It is much appreciated!!

 
Darko:

Thank you very much everyone! I will be implementing these and trying them out. As far as putting the one option into a function, will I be able to put it after int start() at any point to have it work fine, or do I need to put it beforehand?


Thank you again! It is much appreciated!!

The order that functions (including init, start, deinit and your own functions that you may call from these) appear in your code is unimportant.

Reason: