Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialolu adesina
23,007 Pointsi thought i had answered question, but it saying "Did you change the structure of the 'if / else if / else' statement
Did you change the structure of the 'if / else if / else' statement?
why do i need to change the structure?
const int revenue = 125000;
string status = null;
const int oneHundred_K = 100000;
const int one50_k = 150000;
const string red = "red"
const string yellow = "yellow"
const string green = "green"
if (revenue < oneHundred_K )
{
status = red;
}
else if (revenue <one50_k)
{
status = yellow;
}
else
{
status = green;
}
3 Answers
fodhil zidane
24,020 Pointsconst int revenue = 125000; string status = null; const int a = 100000; const int b = 150000; const string c ="red"; const string d = "yellow"; const string e = "green";
if (revenue < a) { status = c; } else if (revenue < b) { status = d; } else { status = e; }
olu adesina
23,007 Pointsthanks that worked but what did u do i dont get it explain please
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI also don't understand why this works.
fodhil zidane
24,020 PointsWe deal the constants (const) to be easy to change them once without going through our programs and change the numbers and strings, so it will be easy and time saving when we use constants.
Rajmohan Ganapathi
1,405 PointsJust remove the space in the if statement "if (revenue < oneHundred_K )" then you will not get the error.
fodhil zidane
24,020 Pointsfodhil zidane
24,020 PointsWE just need to replace the magic numbers and strings with constants (const) and declare them before the if/else statements and those magic numbers and strings with these constants. we can declare them like this:
const int a = 100000; const int b = 150000; const string c = "red"; const string d = "yellow"; const string e = "green";
Then just replace 100000, 150000, "red", "yellow', and "green" with a, b, c, d, and e.