“ 'Switch' and Arrays
Construct”
Switch
•Switch statement is a control flow statement that allows you
to select one of many code blocks to be executed based
on the value of a variable or an expression. It provides an
alternative to using multiple if-else statements when you
have multiple conditions to check.
Syntax
switch(expression){
case value1: // code to be executed if expression matches value1
statements;
break;
case value2:
statements;
break;
.
.
case valueN:
statements;
break;
default:
statements;
}
Note
• The case values can be compared only for equality with the
switch expression
• The expression must be of type int, char, short, byte
• Each case value must be a constant, not a variable
• Duplicate case values are not allowed
• The default part is optional
nlp-ai@[Link]
Note
• If a match is found, the code block following that case is
executed. If no match is found, the program jumps to the
default block (optional).
•The break statement is used to terminate the switch
statement. Without it, the program would continue executing
the code in the subsequent case blocks, even if their
conditions are not met.
•The default block (optional) is executed when none of the
case values match the expression. It acts as a fallback option.
Program
public class try_switch_ex1 {
public static void main(String args[]) {
int i=0,j=1;
switch (i+j){
case 5:
[Link]("The result is 5");
break;
case 1:
[Link]("The result is 1");
break;
default:
[Link]("Printing in the default part");
}
}
}
Arrays
Definition:
An array is a group/collection of variables of the same type
that are referred to by a common name and an index
Examples:
• Collection of numbers
• Collection of names
• Collection of suffixes
Examples
Array of numbers:
10 23 863 8 229
Array of names:
Sholay Shaan Shakti
Array of suffixes:
ment tion ness ves
Analogy
Array is like a pen box with fixed no. of slots of same size.
Syntax
Declaration of array variable:
data-type variable-name[];
eg. int marks[];
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
Syntax…
Accessing elements in the array:
Specific element in the array is accessed by specifying name
of the array followed the index of the element. All array
indexes in Java start at zero.
variable-name[index] = value;
eg. marks[0] = 10;
And
marks[2] = 863;
Example
STEP 1 : (Declaration)
int marks[];
marks 🡪 null
STEP 2: (Memory Allocation)
marks = new int[5];
marks 🡪 0 0 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
STEP 3: (Accessing Elements)
marks[0] = 10;
marks 🡪 10 0 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
Program
class try_array {
public static void main(String args[]) {
int marks[];
marks = new int[3];
marks[0] = 10;
marks[1] = 35;
marks[2] = 84;
[Link](“Marks obtained by 2nd student=” + marks[1]);
}
}
Note
• Arrays can store elements of the same data type. Hence an
int array CAN NOT store an element which is not an int.
Though an element of a compatible type can be converted to
int and stored into the int array.
eg. marks[2] = (int) 22.5;
• Array indexes start from zero. Hence ‘marks[index]’ refers
to the (index+1)th element in the array and ‘marks[size-1]
refers to last element in the array.
eg. marks[0] refers to 1st element, marks[1] refers to 2nd
element… etc. etc.
Alternative Syntax
Combined declaration & memory allocation:
data-type variable-name[] = new data-type[size];
eg. int marks[] = new int[5];
This will declare an int array ‘marks’ and will also allocate
memory of 5 integers to it.
Combined declaration, allocation & assignment:
data-type variable-name[] = {comma-separated values};
eg. int marks[] = {10, 35, 84, 23, 5};
This will declare an int array ‘marks’, will allocate memory
of 5 integers to it and will also assign the values as-
marks 🡪 10 35 84 23 5
marks[0] marks[1] marks[2] marks[3] marks[4]
‘if-else’ Program- What is the Output?
‘if-else’ Program- What is the Output?
A. In the if-else Program,
a.1 what is the out if the int_number=0?
a.2 what is the out if the int_number=6?
a.3 what is the out if the int_number=1?
‘switch’ Program- What is the Output?
‘switch’ Program- What is the Output?
A. In the switch Program,
a.1 what is the out if the int_number=0?
a.2 what is the out if the int_number=5?
a.3 what is the out if the int_number=2?
Assignment
1. Write a program that uses two arrays of size 5. One string
array for storing names of students and one int array for
storing marks obtained by the students. Assign values to
the elements in the arrays. Print names and marks of
students who have scored more than 35 marks.
2. Write a program same as the above, but don’t use
numbers (like 0, 1, 2…etc) to access the elements in the
array. Declare an int variable ‘i’ and use it as an index into
the arrays.
End
Thank you…