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 trialThomas van Dun
Courses Plus Student 237 PointsI dont understand the if statement
Can someone tell me the if statement i dont understand it!
1 Answer
Logan R
22,989 PointsAn if statement checks a condition and if the condition is true, it runs the code.
Conditions can be 'True' or 'False'. We are checking if something is true or not true.
Say we had a variable called Monday.
dayOfTheWeek = Monday
We can use an if statement to see if today is Monday. If it is Monday, we want to do some special code.
// Check if today is Monday. If it is, print out "It's Monday!"
if (dayOfTheWeek == Monday) {
print("It's Monday!")
}
We can also check for true and false other ways too.
Say we had a number that equaled 5.
int number = 5
We can check to see if the number is greater than 0, which should be 'true'. If it is true, we want to tell the user.
The first if statement will run, because 5 is greater than 0 (true) and the second if statement will not run because 5 is not less than 0 (false).
if(number > 0) {
print("The number is greater than 0!");
}
if(number < 0) {
print("The number is less than 0!");
}
I hope that is a little clearer, if it's not, feel free to reply and I can try to explain more or give more examples. It's one of those things where the more you do it, the more you will become better at it.
Gavin Ralston
28,770 PointsI was going to answer something like this, but I noticed the post came from an exercise on logical OR operators, and thought maybe that was the sticking point. Great answer!
Thomas van Dun
Courses Plus Student 237 PointsThanks, but in some video's i just don't understand some things because i'm dutch. And you know dutch people aren't that good in english!
But thanks for your reply!
Gavin Ralston
28,770 PointsGavin Ralston
28,770 PointsIt may help to post your code here, or explain what you think the answer might be.
I could answer this with a simple "An if statement means you can check to see if a condition is true, and if it is, execute a block of code" but I'm not entirely sure you're asking what an if statement is, or if you're asking for help with a particular exercise, or if you're having trouble particularly with the way logical operators like &&, ||, or some sort of XOR operator work.
Check out the Tips for asking questions on the right-hand side of the page, which will give details on how to post your code in the forums when you're asking for help. Good luck!