"Introduction To Programming With Java": Lecture - 6
"Introduction To Programming With Java": Lecture - 6
With Java”
Lecture - 6
nlp-ai@cse.iitb
Contents for Today’s Lecture
• ‘switch’ construct
• Introduction to Arrays
nlp-ai@cse.iitb
‘if-else’ Program
class print_num {
public static void main(String args[]) {
int a_number;
a_number = 0;
if(a_number = = 1)
System.out.println("One");
else
{
if(a_number = = 2)
System.out.println("Two");
else
{
if(a_number = = 3)
System.out.println("Three");
else
System.out.println("No. is out of the range");
}
}
}
} nlp-ai@cse.iitb
‘switch’ Program
class print_num_switch {
public static void main(String args[]) {
int a_number;
a_number = 3;
switch(a_number){
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
System.out.println("No. is out of the range");
}
}
} nlp-ai@cse.iitb
Syntax
switch(expression){
case value1:
statements;
break;
case value2:
statements;
break;
.
.
case valueN:
statements;
break;
default:
statements;
}
nlp-ai@cse.iitb
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@cse.iitb
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
nlp-ai@cse.iitb
Examples
Array of numbers:
10 23 863 8 229
Array of names:
Array of suffixes:
nlp-ai@cse.iitb
Analogy
Array is like a pen box with fixed no. of slots of same size.
nlp-ai@cse.iitb
Syntax
Declaration of array variable:
data-type variable-name[];
eg. int marks[];
This will declare an array named ‘marks’ of type ‘int’. But no
memory is allocated to the array.
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
This will allocate memory of 5 integers to the array ‘marks’
and it can store upto 5 integers in it. ‘new’ is a special
operator that allocates memory.
nlp-ai@cse.iitb
Syntax…
nlp-ai@cse.iitb
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]
nlp-ai@cse.iitb
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;
System.out.println(“Marks obtained by 2nd student=” + marks[1]);
}
}
nlp-ai@cse.iitb
Note
nlp-ai@cse.iitb
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]
nlp-ai@cse.iitb
Assignment
nlp-ai@cse.iitb
End
Thank you…
nlp-ai@cse.iitb