Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90 .


if(outsdieTemperature > 90){
shelfLife-4;
}

something is wrong..can you help..?

if(outsideTemeratures > 90){
shelfLife=shelfLife-4;
}


i have to use the outsideTemperature variable

i dont understand...:sad:

Let's have a look at what is happening in the two blocks you have posted then.
Block 1:

if(outsdieTemperature > 90){
shelfLife-4;
}

Block 2:

if(outsideTemeratures > 90){
shelfLife=shelfLife-4;
}

In Block 1, you are saying if the temperature is above 90 degrees, subtract 4 (I'll assume days) days from the shelflife.Meanwhile, in Block 2, you are saying if the temperature is above 90 degrees, subtract 4 days from the shelflife and let that value be called shelflife from now on.

IE, the outside temperature is 92 degrees and the current value of shelflife is 30 days. After Block 1, shelflife is still equal to 30 days, because you haven't told the program to actually change the value stored in the variable. After Block 2 however, you have told the program to change the value stored in the variable, and shelflife is now equal to 26 (IE it's previous value minus 4 days).

i understand that its missing a variable..
i cant find which variable to use....

i understand that its missing a variable..
i cant find which variable to use....

Have you initialised the variable Shelflife before the if statement?

yes

int outsideTemperature = 80, shelfLife=10;

There are now 2 possibilities. I'll get to them after a bit of background.

If the variable shelf life isn't being changed, look at the conditional (the bit after If which tells the program the circumstances in which to run the statements in the {}).

In this case, the conditional is (outsideTemeratures > 90). If this evaluates as True, the variable shelfLife will be changed, if it evaluates as False, the variable shelfLife will not be changed.

Now, the possibilities. The first is that you made a typo when you wrote the If block. You have three different spellings of what seems to be the same variable (outsideTemperature in the declaration; outsideTemeratures in the second try at the If block and outsdieTemperature in the first try). Check that your spelling of each variable is the same throught the program.

If the program still doesn't change the value of shelfLife after you have checked your spelling, then answer me this:Is the temperature outside actually greater than 90 degrees?You have initialized the temperature variable as 80 degrees, and that is lower than 90 degrees. If the value of the outsideTemperature is not changed between the initialisation and the If block then the If block's conditional (see paragraph 3) will evaluate as False (ie the temperature outside is not greater than 90 degrees) and the statements inside the If block will be skipped.

This article has been dead for over six months. Start a new discussion instead.