YouTip LogoYouTip

Java If Else Switch

Java Conditional Statements – if…else | Tutorial

\n\n

Tutorial -- Learning not just technology, but dreams!

\n\n\n\n\n\n\n\n\n\n\n\n

Java Tutorial

\n

Java Tutorial Java Introduction Java Development Environment Setup Java Basic Syntax Java Comments Java Objects and Classes Java Basic Data Types Java Variable Types Java Variable Naming Rules Java Modifier Types Java Operators Java Loop Structures – for, while and do…while Java Conditional Statements – if…else Java switch case Statement Java Number & Math Classes Java Character Class Java String Class Java StringBuffer and StringBuilder Classes Java Arrays Java Date and Time Java Regular Expressions Java Methods Java Constructors Java Stream, File and IO Java Scanner Class Java Exception Handling

\n\n

Java Object-Oriented

\n

Java Inheritance Java Override/Overload Java Polymorphism Java Abstract Classes Java Encapsulation Java Interfaces Java Enums Java Packages Java Reflection

\n\n

Java Advanced Tutorial

\n

Java Data Structures Java Collections Framework Java ArrayList Java LinkedList Java HashSet Java HashMap Java Iterator Java Object Java NIO Files Java Generics Java Serialization Java Networking Java Sending Emails Java Multithreading Java Applet Basics Java Documentation Comments Java Examples Java 8 New Features Java MySQL Connection Java 9 New Features Java Quiz Java Common Libraries

\n\n

Java Loop Structures – for, while and do…while

\n

Java switch case Statement

\n\n

Java Conditional Statements - if...else

\n

Conditional statements in Java allow a program to execute different blocks of code based on different conditions.

\n

An if statement consists of a boolean expression and one or more statements.

\n\n

Syntax

\n

The syntax of an if statement is:

\n
if(boolean_expression) {\n   /* Executes if the boolean expression is true */\n}
\n

If the boolean expression evaluates to true, then the block of code inside the if statement is executed. Otherwise, the code following the else statement block is executed.

\n\n

Test.java File Code:

\n
public class Test {\n   public static void main(String args[]) {\n      int x = 10;\n      if( x < 20 ) {\n         System.out.print("This is if statement");\n      }\n   }\n}
\n

The compiled and running result of the above code is as follows:

\n
This is if statement
\n\n
\n\n

if...else Statement

\n

An if statement can be followed by an optional else statement, which executes when the boolean expression of the if statement is false.

\n\n

Syntax

\n

The usage of if...else is as follows:

\n
if(boolean_expression) {\n   /* Executes if the boolean expression is true */\n} else {\n   /* Executes if the boolean expression is false */\n}
\n\n

Example

\n

Test.java File Code:

\n
public class Test {\n   public static void main(String args[]) {\n      int x = 30;\n      if( x < 20 ) {\n         System.out.print("This is if statement");\n      } else {\n         System.out.print("This is else statement");\n      }\n   }\n}
\n

The compiled and running result of the above code is as follows:

\n
This is else statement
\n\n
\n\n

if...else if...else Statement

\n

An if statement can be followed by an optional else if...else statement, which is useful to test multiple conditions.

\n

When using if, else if, else statements, note the following:

\n
    \n
  • An if statement can have zero or one else statement and it must come after any else if statements.
  • \n
  • An if statement can have zero to many else if statements and they must come before the else statement.
  • \n
  • Once an else if statement evaluates to true, the remaining else if and else statements are skipped.
  • \n
\n\n

Syntax

\n

The syntax of an if...else if...else statement is:

\n
if(boolean_expression 1) {\n   /* Executes if the boolean expression 1 is true */\n} else if( boolean_expression 2) {\n   /* Executes if the boolean expression 2 is true */\n} else if( boolean_expression 3) {\n   /* Executes if the boolean expression 3 is true */\n} else {\n   /* Executes if none of the above boolean expressions is true */\n}
\n\n

Example

\n

Test.java File Code:

\n
public class Test {\n   public static void main(String args[]) {\n      int x = 30;\n      if( x == 10 ) {\n         System.out.print("Value of X is 10");\n      } else if( x == 20 ) {\n         System.out.print("Value of X is 20");\n      } else if( x == 30 ) {\n         System.out.print("Value of X is 30");\n      } else {\n         System.out.print("This is else statement");\n      }\n   }\n}
\n

The compiled and running result of the above code is as follows:

\n
Value of X is 30
\n\n
\n\n

Nested if...else Statement

\n

It is legal to nest if-else statements. Which means you can use one if or else if statement inside another if or else if statement.

\n\n

Syntax

\n

The syntax for a nested if...else is as follows:

\n
if(boolean_expression 1) {\n   /* Executes if the boolean expression 1 is true */\n   if(boolean_expression 2) {\n      /* Executes if the boolean expression 2 is true */\n   }\n}
\n

You can nest else if...else in the same way as you have nested the if statement.

\n\n

Example

\n

Test.java File Code:

\n
public class Test {\n   public static void main(String args[]) {\n      int x = 30;\n      int y = 10;\n      if( x == 30 ) {\n         if( y == 10 ) {\n            System.out.print("X = 30 and Y = 10");\n         }\n      }\n   }\n}
\n

The compiled and running result of the above code is as follows:

\n
X = 30 and Y = 10
\n\n

Java Loop Structures – for, while and do…while

\n

Java switch case Statement

\n\n

5 Notes Write a Note

\n
    \n
  1. \n

    #0 alan.huang

    \n

    407***3@qq.com 74 Using the previously learned variables, for loops, and if knowledge, create a small program. The program's function is to input a diamond shape composed of asterisks (*) in the console. Please see the following code:

    \n
    public class Demo2 {\n    // Ascending order code below\n    public void prit1(float c) {\n        float p = c / 2; // Ascending order\n        float d; // Declare line number variable\n        float e; // Declare asterisk printing variable\n        float f; // Declare space printing variable\n        float r; // Declare ascending order\n        float s = c % 2; // Modulo\n        if (s == 0) {\n            System.out.println("The data you entered cannot form a diamond structure");\n        } else {\n            for (d = 1; d <= p; d++) {\n                for (f = p; f >= d; f--) {\n                    System.out.print(" ");\n                }\n                for (e = 1; e <= d * 2 - 1; e++) {\n                    if (e == 1 || e == d * 2 - 1) {\n                        System.out.print("*"); // If it's the first or last number, print *\n                    } else {\n                        System.out.print(" "); // Otherwise print a space\n                    }\n                }\n                System.out.println();\n            }\n        }\n    }\n\n    // Descending order code below\n    public void prit2(float m) {\n        float i; // Declare line number variable\n        float j; // Declare asterisk printing variable\n        float k; // Declare space printing variable\n        float n = m / 2 + 1; // Descending order\n        float o = m % 2; // m modulo\n        if (o == 0) {\n            System.out.print("");\n        } else {\n            for (i = 1; i <= n; i++) // Line loop;\n            {\n                // Print spaces before printing asterisks;\n                for (k = 0; k < i - 1; k++) {\n                    System.out.print(" ");\n                }\n                // Loop for printing asterisk count below;\n                for (j = (n - k) * 2 - 2; j >= 1; j--) // Loop for printing asterisk count;\n                {\n                    if (j == (n - k) * 2 - 2 || j == 1) {\n                        System.out.print("*");\n                    } else {\n                        System.out.print(" ");\n                    }\n                }\n                // After printing asterisks, print a newline;\n                System.out.println();\n            }\n        }\n    }\n\n    public static void main(String[] args) {\n        Demo2 a = new Demo2();\n        float b = 11; // Based on the number of lines, determine if a diamond can be formed. If it's an odd number of lines, the corresponding diamond can be printed; if it's an even number of lines, output "The data you entered cannot form a diamond structure";\n        a.prit1(b);\n        a.prit2(b);\n    }\n}
    \n

    alan.huang 9 years ago (2017-08-07)

    \n
  2. \n
  3. \n

    #0 AntAbu

    \n

    163***4484@qq.com 27 Place the diamond to be printed inside a square. The console prints an a*a area. Find the functions where the diamond's edges are located. Print "*" at points on the edges, and print " " at other points. No need to distinguish between ascending and descending order. Please see the code:

    \n
    public class Draw {\n    int a, b; // a is the number of rows for the diamond to be generated\n    int h; // h is the parameter in the method, also the number of rows\n    int i, j; // i j are loop structure parameters\n\n    public void draw(int h) {\n        for (i = 1; i <= h; i++) { // Print row by row\n            for (j = 1; j <= h; j++) { // The number of prints per row is consistent with the row number\n                // The statement below is the function for the four edges of the diamond. At coordinate points on the edge, print *, otherwise print a space\n                if (j == (h + 3) / 2 - i || j == (h - 1) / 2 + i || j == i - (h - 1) / 2 || j == (3 * h + 1) / 2 - i) {\n                    System.out.print("*");\n                } else {\n                    System.out.print(" ");\n                }\n            }\n            System.out.println(); // After printing row i, print a newline\n        }\n    }\n\n    public static void main(String[] args) { // Static method\n        Draw b = new Draw(); // Initialize method\n        int a = 35; // Assign value and execute draw method\n        b.draw(a);\n    }\n}
    \n

    AntAbu 9 years ago (2017-09-02)

    \n
  4. \n
  5. \n

    #0 tf

    \n \n
  6. \n
← Java NumberJava Loop β†’