0% found this document useful (0 votes)
74 views22 pages

Java - For Each Loop

The Java for-each loop, also known as the enhanced for loop, allows code to iterate over arrays or collections in a simple and readable way by eliminating the need for an explicit iterator or index variable. It cannot traverse elements in reverse order or skip elements, but it makes code more readable and eliminates possible errors from incorrect use of iterators compared to a basic for loop.

Uploaded by

sangeethavc
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
74 views22 pages

Java - For Each Loop

The Java for-each loop, also known as the enhanced for loop, allows code to iterate over arrays or collections in a simple and readable way by eliminating the need for an explicit iterator or index variable. It cannot traverse elements in reverse order or skip elements, but it makes code more readable and eliminates possible errors from incorrect use of iterators compared to a basic for loop.

Uploaded by

sangeethavc
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 22

Java -for each

loop
for - each loop
★ It is an enhanced for loop

Advantage:

★ It makes the code more readable.


★ It eliminates the possibility of programming errors.

Disadvantage:

★ It cannot traverse the elements in reverse order


★ It do not have the option to skip any element in array

for(int i=1;i<=3;i++) or for(int i=10;i>=0;i--)


Syntax
Normal for loop:
for(data_type variable : array | collection)
for(int i=1;i<=3;i++)
{

//body of for-each loop

Rules
1. It starts with the keyword for .
2. Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base
type of the array, followed by a colon, which is then followed by the array name.
3. In the loop body, you can use the loop variable you created rather than using an indexed array element.
4. It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)
Example
class ForEachExample1
Output
{ 12
public static void main(String args[]) 12
{ 14
//declaring an array 44
int arr[]={12,13,14,44};

//traversing the array with for-each loop

for(int i:arr)

System.out.println(i);

} }

}
Activity 1 :Program
to find total of
array using
for each loop
Thank You
Switch case
Switch - case
★ Switch statement is Java’s multiway branch statement

switch (expression) ...


{
case value N:
case value 1:
// statement sequence
// statement sequence
break;
break;
default:
case value 2:
// statement sequence // default statement
break; sequence
}
Rules

The expression must be of type byte, Duplicate case values are not allowed.
short, int, or char.

The value for a case must be of the The value for a case must be a constant
same data type as the variable in the or a literal. Variables are not allowed.
switch.
The value of the expression If a match is found, the code
Working of is compared with each of the sequence following that
literal values in the case case statement is executed.
Switch - Case statements.

If none of the constants If no case matches and no


matches the value of the default is present, then no
expression,then the default further action is taken.
statement is executed.
Break in Switch
★ The break statement is used inside the switch to terminate a statement
sequence.
★ When a break statement is encountered, execution branches to the first line
of code that follows the entire switch statement.
★ This has the effect of “jumping out” of the switch
Example Program
int day = 4; case 7:
switch (day) {
case 1: System.out.println("Sunday");
System.out.println("Monday");
break; break;
case 2:
System.out.println("Tuesday");
}
break;
case 3:
System.out.println("Wednesday");
Break;
case 4: System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
Switch without break
public class SwitchExample2 {

public static void main(String[] args) {


Output
int number=20;
20
//switch expression with int value 30
Not in 10, 20 or 30
switch(number){

//switch cases without break statements

case 10: System.out.println("10");

case 20: System.out.println("20");

case 30: System.out.println("30");

default:System.out.println("Not in 10, 20 or 30");

}
Nested Switch
Switch as part of the statement sequence of an outer switch. This is called a Nested switch

Example :

switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...
Importance of Switch
★ The switch differs from the if statement in that switch can only test for equality,
if can even test boolean.
★ In nested if switch statement and an enclosing outer switch can have case
constants in common.
★ A switch statement is usually more efficient than a set of nested ifs.

Activity 2 - Why switch is more efficient than nested if?...........Find the


answer from text
Thank You
Bitwise Shift Operator In Java
Program
public class Hello {
public static void main(String args[])

{
int a=8,int b=-8;

//Left Shift
int a1=a<<1; //Means shift one position left
System.out.println("Left shift of Positive 8 by 1 position :"+a1);
System.out.println("Binary Representation :"+Integer.toBinaryString(a1));

int b1=b<<1; //Means shift one position left


System.out.println("Left shift of Negative 8 by 1 position :"+b1);
System.out.println("Binary Representation :"+Integer.toBinaryString(b1)); Left shift of Positive 8 by 1 position :16

Binary Representation :10000

Left shift of Negative 8 by 1 position :-16

Binary
Representation :111111111111111111111111
11110000
Program…...
//Right Shift

int a2=a>>1; //Means shift one position right


System.out.println("Right shift of Positive 8 by 1 position :"+a2);
System.out.println("Binary Representation :"+Integer.toBinaryString(a2));

int b2=b>>1; //Means shift one position right


System.out.println("Right shift of Negative 8 by 1 position :"+b2);
System.out.println("Binary Representation :"+Integer.toBinaryString(b2));

Right shift of Positive 8 by 1 position :4


Binary Representation :100
Right shift of Negative 8 by 1 position :-
4
Binary
Representation :1111111111111111111111
1111111100
Program …...
//Right Shift With Zero Fill

int a3=a>>>1; //Means shift one position right


System.out.println("Right shift with Zero Fill of Positive 8 by 1 position :"+a3);
System.out.println("Binary Representation :"+Integer.toBinaryString(a3));

int b3=b>>>1;
System.out.println("Integer Right shift with Zero Fill of Negative 8 by 1 position :"+b3);
System.out.println("Binary Representation :"+Integer.toBinaryString(b3));
Right shift with Zero Fill of Positive 8
by 1 position :4
}
Binary Representation :100
}
Integer Right shift with Zero Fill of
Negative 8 by 1
position :2147483644

Binary
Representation :1111111111111111111
Activity 3 - Prepare a note on Bitwise
Shift operator in Java.
Thank You

You might also like