954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

If

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..?

THK
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

I strongly suggest that you read through the tutorial .

DavidRyan
Posting Whiz in Training
229 posts since Jul 2006
Reputation Points: 22
Solved Threads: 11
 
I strongly suggest that you read through the tutorial .





i'll try ..!

THK
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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


i have to use the outsideTemperature variable

i dont understand...:sad:

THK
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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 sayingif 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).

DavidRyan
Posting Whiz in Training
229 posts since Jul 2006
Reputation Points: 22
Solved Threads: 11
 

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

THK
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
i understand that its missing a variable.. i cant find which variable to use....

Have you initialised the variable Shelflife before the if statement?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

yes

int outsideTemperature = 80, shelfLife=10;

THK
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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:[INDENT]Is the temperature outside actually greater than 90 degrees?[/INDENT]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.

DavidRyan
Posting Whiz in Training
229 posts since Jul 2006
Reputation Points: 22
Solved Threads: 11
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You