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.
Article: Java Conditional Statements
Quizzes: Beginner  Intermediate  Advanced 

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.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. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Article: Java Conditional Statements
Quizzes: Beginner  Intermediate  Advanced 

Suggested Articles:


Sar Maroof's New Book "Java Quizmaster For Beginners"
Author: Sar Maroof
Many years of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 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. Kneogwdle wants to be free, just like these articles!

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

Leave a Reply

megisoft © 2016