If statements are found in all programming languages as a way to make choices. Here’s a comparison of ifs in App Inventor blocks, CSP block and pseudocode and Java ifs.
The statements in a Java main method normally run or execute one at a time in the order they are found from top to bottom. If statements (also called conditionals or selection) change the flow of control through the program so that some code is only run when something is true. In an if statement, if the condition is true then the next statement or a block of statements will execute. If the condition is false then the next statement or block of statements is skipped.
A conditional uses the keyword if followed by Boolean expression inside of an open parenthesis ( and a close parenthesis ) and then followed by a single statement or block of statements. The single statement or block of statements are only executed if the condition is true. The open curly brace { and a close curly brace } are used to group a block of statements together. It is recommended to always put in the curly braces even if you have just one statement under the if statement.
// A single if statement
if (boolean expression)
Do statement;
// Or a single if with {}
if (boolean expression)
{
Do statement;
}
// A block if statement: { } required
if (boolean expression)
{
Do Statement1;
Do Statement2;
...
Do StatementN;
}
Note2.8.3.
Note that there is no semicolon (;) at the end of the boolean expression in an if statement even if it is the end of that line. The semicolon goes at the end of the whole if statement, often on the next line. Or { } are used to mark the beginning and end of the block of code under the if condition.
Imagine that your cell phone wanted to remind you to take an umbrella if it was currently raining in your area when it detected that you were leaving the house. This type of thing is going to become more common in the future and it is an area of research called Human Computer Interaction (HCI) or Ubiquitous Computing (computers are everywhere).
Run the following active code a couple times until you see all the possible outputs. It prints out whether a random number is positive or equal to 0. Add another if statement that tests if it is a negative number.
A common mistake in if statements is using = instead of == in the condition by mistake. You should always use ==, not =, in the condition of an if statement to test a variable. One equal sign (=) assigns a value to a variable, and two equal signs (==) test if a variable has a certain value.
Always use curly brackets { and } to enclose the block of statements under the if condition. Java doesn’t care if you indent the code – it goes by the { }.
Don’t put in a semicolon ; after the first line of the if statement, if (test);. The if statement is a multiline block of code that starts with the if condition and then { the body of the if statement }.
if statements test a boolean expression and if it is true, go on to execute the following statement or block of statements surrounded by curly brackets { } like below.
// A single if statement
if (boolean expression)
Do statement;
// A block if statement
if (boolean expression)
{
Do Statement1;
Do Statement2;
...
Do StatementN;
}
Relational operators (==, !=, <, >, <=, >=) are used in boolean expressions to compare values and arithmetic expressions.
The variable isRaining is a boolean variable that is either true or false. If it is true then the message Take an umbrella! will be printed and then execution will continue with the next statement which will print Drive carefully. Run the code below to see this.