You are on page 1of 16

Java Programming

Soorya M
Project Engineer
Control Statements

Decision : if
switch
Iteration : for
while
do…while
Jump : break
continue
return

9 March 2022 Software Training & Development Centre


▪ if (<conditional expression>)
<statement>

▪ if (<conditional expression>)
<statement1>
else
<statement2>

9 March 2022 Software Training & Development Centre


Nested if
▪ if (<conditional expression>)
{
if (<conditional expression>)
{
}
}
else { }

9 March 2022 Software Training & Development Centre


else if ladder
if (<cond. expr.1>)
<statement 1>
else if (<cond. expr.2>)
< statement 2>

else if (<cond. expr. n>)
< statement n>
else
< statement else>

9 March 2022 Software Training & Development Centre


switch ( <expr.> ) {
case <const. expr. 1> : <statement 1>
case < const. expr. 2> : < statement 2>
:
:
case < const. expr. n> : < statement n>
default : < statement>
}

9 March 2022 Software Training & Development Centre


Iteration Statement- for

▪ for (initialization;condition;iteration)
{
}

for (i = 0; i < 10; i++)


System.out.println(i);

9 March 2022 Software Training & Development Centre


do..while & while statement
▪ do
{
} while (condition);

▪ while(condition)
{
}

9 March 2022 Software Training & Development Centre


For each:
for(type itr-var : collection) statement-block;
▪ Example:
int nums[] = { 2,5,6,13,33,23,51};
int sum =0;
for(int x:nums) sum+=x;

9 March 2022 Software Training & Development Centre


Jump statements
▪ Break
– To move control out of the block

int i = 1;
while (i<10) {
if (i == 3)
break;
System.out.println("This is a " + i + " iteration");
++i;
}

9 March 2022 Software Training & Development Centre


Jump - continue

▪ To move control to the start of next repetition

for (i=1; i<=5; ++i) {


if (i % 2 == 0)
continue;
System.out.println("This is a " + i + " iteration");
}

9 March 2022 Software Training & Development Centre


Return

▪ To explicitly return from a method


▪ Program control transfers back to the caller of the
method

9 March 2022 Software Training & Development Centre


Arrays

▪ Arrays are objects


– An array variable is an object reference
– Must create the array itself using new

type[] arrayName; int[] squares; // Example


… or …
type arrayName[];

9 March 2022 Software Training & Development Centre


Creating Array “Objects”
▪ Create an array of required length, and assign it to
the array variable
int[] squares; // Declare array variable
squares = new int[4] // Create array object

▪ Array elements are squares 0 [0]


initialized automatically 0 [1]
0 [2]
0 [3]

9 March 2022 Software Training & Development Centre


Arrays of Primitives: Summary

Declaration
squares
int[] squares; null

Creation
0
squares = new int[3]; squares
0

Initialization 0

for (int i=0; i<3; i++) 0


squares
squares[i] = i * i; 1
4
9 March 2022 Software Training & Development Centre
Array Length

▪ Every array has a property called length


 Available for all types of arrays

int[] squares = new int[3];



for (int i=0; i<squares.length; i++)
squares[i] = i * i;

9 March 2022 Software Training & Development Centre

You might also like