Java Control Flow Statements
Control Flow Statements in Java
It is sometimes necessary to perform repeated actions or skip some statements in a program. For these actions certain control statements are available. These statements control the flow of execution of the programs. The statements inside Java code are executed from top to bottom, in the order that they appear. Java control flow statements, change or break the flow of execution by implementing decision making statements, looping statements, and branching statements in your program to execute particular blocks of code based on the conditions. In this tutorial you can learn about the decision making statements (if, if - else, switch) supported by the Java programming language.
Decision Making Statements in Java
- if – else
branching statement in Java
Java programming language provides following types of decision making or branching statements in Java . Java programming language provided decision making statements or branching statements as follows .
An if statement consists of a boolean expression followed by one or more statements .
An if statement can be followed by an optional else statement , which executes when the boolean expression is false .
nested if statement
You can use one if or else if statement inside another if or else if statements .
A switch statement allows a variable to be tested for equality against a list of values.
Java if Statement
The if statement is the most basic of all the control flow statements in Java. It tells your program to execute a certain section of code only if a particular test condition evaluates to true. In Java, the opening and closing braces are optional. But this is applicable only if the block of code to be execute is just a single statement. It is recommended to put the brackets around the statements, even if there is only one statement to execute. Why because, in the beginning you may start with one statement and later during the development phase you may add more statements. In the following Java if statement example you can learn how to use if statement in Java.
Java if Statement Syntax :
Java if Statement Example
Java if Statement Example 2
Java if else Statement
The if else statement provides a secondary path of execution when an "if" clause evaluates to false. In other words if a particular test condition evaluates to false then else block will be executed. In the following Java if else statement example you can learn how to use if else statement in Java.
Java if else Statement Syntax :
Java if else Statement Example
Java switch Statement
Unlike if and if - else statements, the switch statement can have a number of possible execution paths. The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression within the brackets, then executes all statements that follow the matching case label. break statement is necessary in switch statement. Without break statement, statements in switch blocks fall through. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
Java switch Statement Syntax:
Java int switch Statement Example
Java char switch Statement Example