Conditional statements in Java

Conditional statements check conditions and take decision what to to.

If Statements:

If we want that a code execute if a condition satisfy otherwise not than we use if statements. For examples, we are taking an integer from console and want to print 'number is between 0 and 100' if number is between 0 and 100.

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("type an integer value: ");
  9. int a = input.nextInt();
  10. // check for condition
  11. // condition returns true or false
  12. if (a > 0 && a <= 100)
  13. {
  14. System.out.print("a is between 0 and 100 (inclusive)");
  15. }
  16. }
  17. }

Run this code and enter different values.

If else statements:

If else statement provides an alternative. If condition fails then alternative code will run. In above example if we want to print 'number is outside the range' for numbers which are not between 0 and 100 then we will use else statement.

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("type an integer value: ");
  9. int a = input.nextInt();
  10. // check for condition
  11. // condition returns true or false
  12. if (a > 0 && a <= 100)
  13. {
  14. System.out.print("a is between 0 and 100 (inclusive)");
  15. }
  16. else
  17. {
  18. System.out.print("number is outside the range");
  19. }
  20. }
  21. }

else if Statements:

It provides more alternatives. For example, in above example if we want to print for multiple ranges then code will be:

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("type an integer value: ");
  9. int a = input.nextInt();
  10. // check for condition
  11. // condition returns true or false
  12. if (a > 0 && a <= 100)
  13. {
  14. System.out.print("a is between 0 and 100 (inclusive)");
  15. }
  16. else if (a > 100 && a <= 200)
  17. {
  18. System.out.print("a is between 100 and 200 (inclusive)");
  19. }
  20. else if (a > 200 && a <= 300)
  21. {
  22. System.out.print("a is between 200 and 300 (inclusive)");
  23. }
  24. else if (a > 300 && a <= 400)
  25. {
  26. System.out.print("a is between 300 and 400 (inclusive)");
  27. }
  28. else
  29. {
  30. System.out.print("number is outside the range");
  31. }
  32. }
  33. }

Nested statements:

We can write if or if else or else if statements inside if or else or else if statements.

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("type an integer value: ");
  9. int a = input.nextInt();
  10. // check for condition
  11. // condition returns true or false
  12. if (a > 0 && a <= 100)
  13. {
  14. if (a > 0 && a <= 50)
  15. {
  16. System.out.print("a is between 0 and 50 (inclusive)");
  17. }
  18. else
  19. {
  20. System.out.print("a is between 50 and 100 (inclusive)");
  21. }
  22. }
  23. else if (a > 100 && a <= 200)
  24. {
  25. if (a > 100 && a <= 150)
  26. {
  27. System.out.print("a is between 100 and 150 (inclusive)");
  28. }
  29. else if (a > 150 && a <= 200)
  30. {
  31. System.out.print("a is between 150 and 200 (inclusive)");
  32. }
  33. }
  34. else if (a > 200 && a <= 300)
  35. {
  36. System.out.print("a is between 200 and 300 (inclusive)");
  37. }
  38. else if (a > 300 && a <= 400)
  39. {
  40. System.out.print("a is between 300 and 400 (inclusive)");
  41. }
  42. else
  43. {
  44. System.out.print("number is outside the range");
  45. }
  46. }
  47. }

Switch Statements:

Switch statements are useful when we have to compare a single variable with a list of variables and decide which code has to be execute.

Syntax of switch statements is:

  1. switch(variable)
  2. {
  3. case 1:
  4. //code
  5. break;
  6. case 2:
  7. //code
  8. break;
  9. ...
  10. ...
  11. ...
  12. case n:
  13. //code
  14. break;
  15. default:
  16. break;
  17. }
  • Every case is compared with variable. If any case matches with variable then code inside that case (until break statement) will run.
  • Once break statement is executed, switch will stop executing code and program will come out from the switch statement.
  • Cases must be of same data types that of variable like int.
  • Variable can only be integer, short, byte or char.
  • Every switch statement must has a default case. Break statement after default may be neglected because there is no case after that case.
  • NOTE: if you forget break statement then code of next case will also be executed.

Lets take a very simple example: we have to input an integer number and program will print if number is between 1-3 or between 4-7 or 8 or 9 or 10 or outside the range.

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("type an integer value: ");
  9. int number = input.nextInt();
  10. // write a switch statement
  11. switch(number)
  12. {
  13. // check for 1 to 3
  14. // no break statement require because we want to execute same code
  15. case 1:
  16. case 2:
  17. case 3:
  18. System.out.println("number is between 1 to 3");
  19. // break switch statement
  20. break;
  21. // check for 4 to 7
  22. case 4:
  23. case 5:
  24. case 6:
  25. case 7:
  26. System.out.println("number is between 4 to 7");
  27. // break switch statement
  28. break;
  29. // check for 8
  30. case 8:
  31. System.out.println("number is 8");
  32. // break switch statement
  33. break;
  34. case 9:
  35. System.out.println("number is 9");
  36. // break switch statement
  37. break;
  38. case 10:
  39. System.out.println("number is 10");
  40. // break switch statement
  41. break;
  42. default:
  43. System.out.println("number is outside the range");
  44. }
  45. }
  46. }

It is your exercise to implement the example used in nested statement using switch statement after reading this section.

HINT: Divide number by 50.

Exercise:

Take a variable as mark and print the grade according to this table using both if else and switch statements.

Min marksMax marksGrade
030F
3140E
4150D
5160C
6170C+
7180B+
8190A
91100A+



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a technical core team member at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel