Java_7_Control statements (1)
Java_7_Control statements (1)
Java keywords are also known as reserved words. Keywords are particular words that
act as a key to a code. These are predefined words by Java so they cannot be used as
a variable or object name or class name.
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to execute
and when. Decision-making statements evaluate the Boolean expression and control
the program flow depending upon the result of the condition provided. There are two
types of decision-making statements in Java, i.e., If statement and switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program
is diverted depending upon the specific condition. The condition of the If statement
gives a Boolean value, either true or false. In Java, there are four types of if-statements
given below.
34M
651
Java Try Catch
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a
Boolean expression and enables the program to enter a block of code if the expression
evaluates to true.
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
Consider the following example in which we have used the if statement in the java
code.
Student.java
Student.java
Output:
x + y is greater than 20
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of
code, i.e., else block. The else block is executed if the condition of the if-block is
evaluated as false.
Syntax:
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }
Student.java
Output:
x + y is greater than 20
3) if-else-if ladder:
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 2; //executes when all the conditions are false
9. }
Student.java
Output:
Delhi
4. Nested if-statement
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. if(condition 2) {
4. statement 2; //executes when condition 2 is true
5. }
6. else{
7. statement 2; //executes when condition 2 is false
8. }
9. }
Student.java
Output:
Delhi
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch statement
contains multiple blocks of code called cases and a single case is executed based on
the variable which is being switched. The switch statement is easier to use instead of
if-else-if statements. It also enhances the readability of the program.
o The case variables can be int, short, byte, char, or enumeration. String type is
also supported since version 7 of Java
o Cases cannot be duplicate
o Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
o Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
o While using switch statements, we must notice that the case expression will be
of the same type as the variable. However, it will also be a constant value.
The syntax to use the switch statement is given below.
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
...
default:
// default statements
}
Consider the following example to understand the flow of the switch statement.
Output:
Size: Large
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// ask users to enter operator
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
// ask users to enter numbers
System.out.println("Enter first number");
number1 = input.nextDouble();
switch (operator) {
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly while
some condition evaluates to true. However, loop statements are used to execute the
set of instructions in a repeated order. The execution of the set of instructions depends
upon a particular condition.
In Java, we have three types of loops that execute similarly. However, there are
differences in their syntax and condition checking time.
1. for loop
2. while loop
3. do-while loop
Calculation.java
Output:
Consider the following example to understand the functioning of the for-each loop in
Java.
Calculation.java
Output:
Java
C
C++
Python
JavaScript
It is also known as the entry-controlled loop since the condition is checked at the start
of the loop. If the condition is true, then the loop body will be executed; otherwise, the
statements after the loop will be executed.
1. while(condition){
2. //looping statements
3. }
The flow chart for the while loop is given in the following image.
Calculation .java
Output:
0
2
4
6
8
10
It is also known as the exit-controlled loop since the condition is not checked in
advance. The syntax of the do-while loop is given below.
1. do
2. {
3. //statements
4. } while (condition);
The flow chart of the do-while loop is given in the following image.
Consider the following example to understand the functioning of the do-while loop in
Java.
Calculation.java
Jump Statements
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to the
other part of the program. There are two types of jump statements in Java, i.e., break
and continue.
The break statement cannot be used independently in the Java program, i.e., it can
only be written inside the loop or switch statement.
Consider the following example in which we have used the break statement with the
for loop.
BreakExample.java
Output:
0
1
2
3
4
5
6
Calculation.java
Output:
0
1
2
3
4
5
Output:
0
1
2
3
5
1
2
3
5
2
3
5
Java Switch Statement
The Java switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement. The switch statement works with byte, short, int, long, enum
types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you
can use strings in the switch statement.
In other words, the switch statement tests the equality of a variable against multiple
values.
Points to Remember
Syntax:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
Flowchart of Switch Statement
41.6M
784
Difference between JDK, JRE, and JVM
Example:
SwitchExample.java
Output:
20
SwitchMonthExample.javaHTML
Output:
7 - July
SwitchVowelExample.java
Vowel
Example:
SwitchExample2.java
Output:
20
30
Not in 10, 20 or 30
Example:
SwitchStringExample.java
1. //Java Program to demonstrate the use of Java Switch
2. //statement with String
3. public class SwitchStringExample {
4. public static void main(String[] args) {
5. //Declaring String variable
6. String levelString="Expert";
7. int level=0;
8. //Using String in Switch expression
9. switch(levelString){
10. //Using String Literal in Switch case
11. case "Beginner": level=1;
12. break;
13. case "Intermediate": level=2;
14. break;
15. case "Expert": level=3;
16. break;
17. default: level=0;
18. break;
19. }
20. System.out.println("Your Level is: "+level);
21. }
22. }
Test it Now
Output:
Example:
NestedSwitchExample.java
21. break;
22. case 'M':
23. System.out.println("Drawing, Manufacturing Machines");
24. break;
25. }
26. break;
27. case 3:
28. switch( branch )
29. {
30. case 'C':
31. System.out.println("Computer Organization, MultiMedia");
32. break;
33. case 'E':
34. System.out.println("Fundamentals of Logic Design, Microelectro
nics");
35. break;
36. case 'M':
37. System.out.println("Internal Combustion Engines, Mechanical Vi
bration");
38. break;
39. }
40. break;
41. case 4:
42. switch( branch )
43. {
44. case 'C':
45. System.out.println("Data Communication and Networks, Multi
Media");
46. break;
47. case 'E':
48. System.out.println("Embedded System, Image Processing");
49. break;
50. case 'M':
51. System.out.println("Production Technology, Thermal Engineerin
g");
52. break;
53. }
54. break;
55. }
56. }
57. }
Test it Now
Output:
Example:
JavaSwitchEnumExample.java
Output:
Sunday
Monday
Twesday
Wednesday
Thursday
Friday
Saturday
Example:
WrapperInSwitchCaseExample.java
Output:
You are eligible for vote.
ForEachExample.java
Output:
12
23
44
56
78
Note: The break and continue keywords breaks or continues the innermost for loop
respectively.
Syntax:
1. labelname:
2. for(initialization; condition; increment/decrement){
3. //code to be executed
4. }