Java conditional statements

Conditional statements are used in Java as questions. There are three types of conditional statements which are if-statements, else-if statements and switch/case statements.

The main difference between if-statements and else-if statements are:

1- if-statements: check all the individual statements.

2- else-if-statements: if one of the statements return true, all of the other else-if statements will not be executed even if they are true.

1- if – if – if …..else statements  to make one or more choices between two or more options.
2- if – else if – else if ….. else statements to make one single choice between two or more options.
3- switch/ case statements  to make choices between many options.

conditional-statements-color

1- if statements

The if statements make choices between two or more options. The conditional expression is evaluated first. If it is true, the statement is executed and it continues to the next statements. if the value is false, then the else statement is executed.

Exercise 1

What is written to the standard output as the result of executing the following statements?

class MyClass {

	public static void main(String[] args){
		int i = 4;
		int i2 = 4;
		if(i != i2) {
			System.out.println("H");
		}
		else {
			System.out.println("K");
		}
	}
}

Select the correct answer.


Exercise 2

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		int i2 = 5;
		int i3 = 10;
		if(i2 != i3) {
			System.out.print("T");
		}
		if(i3 == 15) {
			System.out.print("Y");
		}
		else {
			System.out.print("Z");
		}
	}
}

Select the correct answer.


Exercise 3

A simple example how Java if statements practically work:

Ben has $120 on his account. His father promised him to pay him $50 if he scores 8 or higher out of 10. His uncle also promised him to pay him $20 if he scores higher than 6 out of 10. The following program tries to calculate the balance of Ben for different scores. Find out why this code sometimes calculates wrong balance and improve the issue!

What is written to the standard output as the result of executing the following code?

public class MyClass {

	public static void main(String[] args){
		double balance = 120;
		int score = 8;
		if(score >= 8) {
			balance = balance + 50; // + $50 from his father
		}
		if(score > 6) {
			balance = balance + 20; // + $20 from his uncle 
		}
		else {
			balance = balance - 10;
		}
		System.out.print(balance);
	}
}

Select all the correct answers.


2- else if – statements

The difference between if statements and else-if statements is that if the first statement is true, all of the other else-if statements will not be executed even if they are true.

Exercise 4

An example how Java if else statements work in the real world:

Find out why you need to use else if statement instead of if statement in the following program.

A product cost $100 is discounted based on the age of the customers. Customers who are 60 years or older get a discount of $10. Customers younger than 18 get a discount of $20. Everyone else get a discount of $5. The following program writes the product price to the standard output based on the customer ages.

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		int age = 50;
		double productPrice = 100.0;
		if(age >= 60) {
			productPrice = productPrice - 10;
		}
		if(age < 18) {
			productPrice = productPrice - 20;
		}
		else {
			productPrice = productPrice - 5;
		}
		System.out.print(productPrice);
	}
}

Select all the correct answers.


3- switch/case – statements

You can also use if else statement instead of switch in the following program, but using switch in this case is clearer and you can achieve the same result by writing less code.

Exercise 5

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		int grade = 4;
		switch(grade) {
			case 10:
				System.out.println("Excellent");
				break;
			case 9:
				System.out.println("Very good");
				break;
			case 8:
				System.out.println("Good");
				break;
			case 7:
				System.out.println("Fair");
				break;
			case 6:
				System.out.println("Weak");
				break;
			default:
				System.out.println("Is not valid");
		}
	}
}

Select all the correct answers.


Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

9 Comments

Add a Comment
  1. Useful information. Fortunate me I found your web site by accident.

  2. I dugg some of you post as I thought they were very beneficial invaluable

  3. Four score and seven minutes ago, I read a sweet arlicte. Lol thanks

  4. More “switch statements” exercises would be great!

    1. There’s a terrific amount of knelwodge in this article!

  5. Very nice website. Good job!

    1. Thank you!

      1. Kneogwdle wants to be free, just like these articles!

    2. An inllitegent answer – no BS – which makes a pleasant change

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017