Week 6: Statements with Java (Part one)

10/16/2015

If you look up the definition of the word “statement" you will get something along the lines of

a definite or clear expression of something

That’s precisely what a statement is in Java. A simple statement looks like the following:

int number = 5 + 6;

Simple statements perform an action. In the above, the variable, number, is created with type int and is set to the summation of 5 and 6. Finally, the statement is terminated with a semicolon (;). In English, the semicolon is used to separate items in a list of statements. In Java and many other programming languages, it does the same thing. This is how the compiler can tell where a statement ends. The syntax for a statement is as follows:

<expression>;

What a boring day (not to mention tedious) if programs consisted only of simple statements. Programmers have the option of using a combination of simple statements together as a compound statement. A compound statement is any number of simple statements enclosed in curly braces.

{
<simple_statement>;
<simple_statement>;
<simple_statement>;
}

An example of a concept in programming that uses compound statements is a function or method.

public static void main(String[] args) {
int number = 5;
int squaredNumber = number * number;
System.out.println(squaredNumber);
}

Notice the indentation of the statements enclosed in the curly braces. This is not enforced by the compiler but you and potential collaborators will benefit greatly from putting such care into the aesthetics of your code. By looking at the indented code above, you know the simple statements are on the same level in terms of scope. Compare the above to the following:

public static void main(String[] args) {
int number = 5;
int squaredNumber = number * number;
System.out.println(squaredNumber);
}

While you can easily figure out the that the simple statements belong to the main function due to the brevity of the code, you will have a lot of trouble reading and understanding it if you were to have many lines in a compound statement or compound statements inside of a compound statement. For example:

public static void main(String[] args) {
boolean print = true;
int number = 5;
int squaredNumber = number * number;

if(print == true) {
System.out.println(“Here is a squared number:”);
System.out.println(squaredNumber);
}
}

Now we have another compound statement and we can easily understand the structure and layout of the source code thanks to proper indentation.

You might have noticed the if statement. This is one of the last kind of statements in Java, control statements. Control statements play a role in the way other statements are executed. An if statements is an example of a conditional control statement. Using the code above, we can understand that an if statement will only execute the compound statement if the condition, print == true, is…true! The symbol == in Java is used as a comparison operator. Java will check if the value of the boolean, print, is true and run the two System.out.println() methods based on that. We can also specify a fall back for the if statement or an else case. Here is an example of an if-else statement.

int number = 7;
if(number < 5) {
System.out.println(“You have less than 5 left.”);
} else {
System.out.println(“You have more than 5 left.”);
}

The condition, number < 5, is asking if number is less than 5. The else block will execute since number is greater than 5.

Here is a list of equality and conditional operators:

==
equal to
!=
not equal to
>
greater than
>=
greater than or equal to
<
less than
<=
less than or equal to

 

Sometimes you have multiple if conditions to specify. This may be the case if you only prefer some statements be evaluated on certain similar conditions. In this case you can use if-else-if statements like such:

int number = 7;
if(number < 5) {
System.out.println(“You have less than 5 left.”);
} else if (number == 5) {
System.out.println(“You have 5 left.”);
} else if (number < 10) {
System.out.println(“You have less than 10 left.”);
} else {
System.out.println(“You have more than 10 left.”);
}

In the above, the compiler will check if number is less than 5 in the first condition. If that isn’t true, it will check if number is exactly 5. It will keep going until it reaches a true condition. If none of the if-else-if conditions evaluate to true it will default to the else block. Note that you don’t have to specify and else case.

Writing if-else-if statements can be cumbersome when testing against many cases. Fear not! The programming world has not forsaken you. In place of if-else-if statements you can use switch statements.

int number = 7;
switch(number){
case 1:
System.out.println(“Number is 3”);
break;

case 5:
System.out.println(“Number is 5”);
break;

case 7:
System.out.println(“Number is 7”);
break;

default:
System.out.println(“I don’t know what number is.”);
}

With switch statements, the compiler will look for the case that is true and then break out of comparing cases. In other words, each case will be evaluated and compared with number. In this example, number is 7, so the third case will evaluate to true. Our statement, System.out.println(“Number is 7”); will execute and we will break out of the switch statement. Without the break; there, all cases will be compared against number. So we include break; after a case to tell Java to stop looking for a true case after one is found. If none of the cases turned out to be true (if number was not equal to any of the cases), the default case will run it’s block. Think of the default case as our else.

That’s it for part one. Be sure to play around with control statements and get familiar with them. Get to know switch statements and go crazy with it. For example, add more cases and remove breaks from some of the them and see what happens. See you next week for part two where we finish off control statements.

[content_band inner_container="true" padding_top="10px" padding_bottom="10px" border="none" bg_pattern="http://www.zipcodewilmington.com/wp-content/uploads/Triangles__1441830476_50.73.209.90-compressor.jpg"]

Want to learn more about Java? Check out more about our curriculum and apply today!

[/content_band]

coding tutorial JAVA java statements java tutorial learn to code round-up switch statements

Comments