Computer - Java Notes (If-Else, Switch) (Class-Ix, File-4) - 2023

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CLASS – IX (ICSE) (Computer Notes )

CONTROL STATEMENTS IN JAVA (FILE-4)

CONTROL STATEMENTS IN JAVA


A programming language uses control statement to cause the flow of execution to advance or branch based on changes
to the state of the program.
Java’s program control statements can be put into following categories:
 Select/conditional/decision making statements
 Looping/iteration

 Jump/unconditional
SELECTION/CONDITIONAL/DECISION MAKING STATEMENTS
The statement that allows us to alter the flow of control (top to bottom) of a program depending upon conditions is
known as conditional statement.
Selection statements are used in a program to check condition(s) to perform a specific action.
 Java supports two selection statements:
* if-else statement
* switch-case statement

If-else STATEMENT
 It is an example of bi-directional branching statement.
 It checks the condition(s) and transfer the control to the execution of either of the two blocks of statement.

General forms of if statement:

1
2
switch-case STATEMENT
The switch statement is Java’s multi-way branch statement. It provides an easy way to dispatch execution to
different parts of your code based on the value of an expression. As such, it often provides a better alternative
than a large series of if-else-if statement.
➢ By switch statement we can check any expression and transfer the control to the given case.
➢ The case is terminated by a break statement.
➢ If switch constant does not match with any of the case, then the default is executed.

Syntax example: example:


switch (expression) switch (n) n is an integer switch (ch) ch is a character.
{ { {
case value1: case 1: case ‘h’:
Statement(s); System.out.println(“hello”); System.out.println(“hello”);
break; break; break;
case value2: case 2: case ‘W’:
Statement(s); System.out.println(“welcome”); System.out.println(“welcome”);
break; break; break;
case value3: case 3: case ‘B’:
Statement(s); System.out.println(“Bye Bye”); System.out.println(“Bye Bye”);
break; break; break;
--------- default : default :
………… System.out.println(“Sorry”); System.out.println(“Sorry”);
default : } }
Statement(s);
}

Fall through issue:


If any case statement does not have break statement after it then it will execute the next case(s) also. The
misuse or missing of break statement leads to fall of control (e.g. switch statement without break clauses).

Write the differences between if-else & switch-case

a) “if” can evaluate complex relational or logical a) switch statement can only work for equality
expressions comparisons

b) Many different variables can be used to form b) Only a single value is used for Selection.
complex expression.

c) if – else can handle floating point tests c) Only byte, short, int, String or char can be used.

d) if-else is Bi-directional statement d) switch-case is Multi-branching statement

You might also like