Object Oriented Programming and Design with Java 20CS43P
Week – 05
Conditional and Iterative statements
JAVA'S SELECTION STATEMENTS [Decision Making, Branching]
Java supports two selection statements: if and switch. These statements allow you to control the
flow of your program's execution based upon conditions known only during run time.
Simple if
if (condition)
statement1;
else
statement2;
The if-else-if Ladder
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
else
statement;
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
Switch Statement
The expression is evaluated once and compared with the values of each case label.
• If there is a match, the corresponding code after the matching case label is executed.
For example, if the value of the expression is equal to value2, the code after case value2: is
executed.
• If there is no match, the code after default: is executed.
switch (expression)
{
case value1: // statement sequence
break;
case value2: // statement sequence
break;
:
:
:
case valueN: // statement sequence
break;
default: // default statement
sequence
}
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
1.27 ITERATION STATEMENTS [Decision Making, Looping]
Java's iteration statements are for, while, and do-while. These statements create what we
commonly call loops. As you probably know, a loop repeatedly executes the same set of instructions
until a termination condition is met.
While do-while for
It repeats a statement or Sometimes it is desirable to Here is the general form of the for
Block while its controlling statement:
execute the body of a while
expression is true. loop at least once, even if the
conditional expression is false for(initialization; condition; iteration)
while(condition) to begin with {
{ do { // body
// body of loop // body of loop }
} } while (condition);
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
1.28 JUMP STATEMENTS [Labelled Loops]
Java supports three jump statements: break, continue, and return. These statements transfer
control to another part of your program. Each is examined here.
In addition to the jump statements discussed here, Java supports one other way that you can
change your program's flow of execution: through exception handling. Exception handling provides a
structured method by which run-time errors can be trapped and handled by your program. It is supported
by the keywords try, catch, throw, throws, and finally. In essence, the exception handling mechanism
allows your program to perform a nonlocal branch.
Break continue return
In Java, the break statement Sometimes it is useful to force an The last control statement is
has three uses. early iteration of a loop. That is, you return. The return statement is
First, as you have seen, it might want to continue running the used to explicitly return from a
terminates a statement sequence loop, but stop processing the method. That is, it causes
in a switch statement. remainder of the code in its body for program control to transfer back
this particular iteration. to the caller of the method.
Second, it can be used to exit a
loop. For all three loops (while, do-while, Thus, the return statement
for), any Intermediate code is immediately terminates the
Third, it can be used as a bypassed. method in which it is executed.
"civilized" form of goto.
we use label before the for loop. It is useful if we have nested for loop so that we can break/continue
specific for loop.
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
Exercise 1: Get a number from the user and print whether it is Exercise 2: Find the greatest of three numbers
positive or negative number
import java.util.Scanner; public class Exercise2 {
public class Exercise1 { public static void main(String[] args) {
int num1=30; int num2=50; int num3=60;
public static void main(String[] args) if (num1 > num2)
{ if (num1 > num3)
Scanner in = new Scanner(System.in); System.out.println("The greatest: " + num1);
System.out.print("Input number: ");
int input = in.nextInt(); if (num2 > num1)
if (num2 > num3)
if (input > 0) System.out.println("The greatest: " + num2);
{
System.out.println("Number is positive"); if (num3 > num1)
} if (num3 > num2)
else if (input < 0) System.out.println("The greatest: " + num3);
{ }
System.out.println("Number is negative"); }
}
else
{
System.out.println("Number is zero");
}
}
}
Exercise 3: Demo of if else ladder Exercise 4: Demo of Switch statement
import java.util.Scanner; mport java.util.Scanner;
public class Exercise3 { public class Exercise4 {
public static void main(String args[]) public static void main(String args[])
{ {
Scanner in = new Scanner(System.in); Scanner in = new Scanner(System.in);
System.out.print("Input sem: "); System.out.print("Input sem: ");
int sem = in.nextInt(); int sem = in.nextInt();
if(sem==1) switch (sem)
System.out.println("You are 1st sem"); {
else if(sem==2) case (1):
System.out.println("You are 2nd sem"); System.out.println("You are 1st sem");
else if(sem==3) break;
case (2):
System.out.println("You are 3rd sem"); System.out.println("You are 2nd sem");
else if(sem==4) break;
System.out.println("You are 4th sem"); case (3):
else if(sem==5) System.out.println("You are 3rd sem");
System.out.println("You are 5th sem"); break;
else if(sem==6) case (4):
System.out.println("You are 6th sem"); System.out.println("You are 4th sem");
else break;
System.out.println("Please give the valid sem."); case (5):
System.out.println("You are 5th sem");
break;
} case (6):
} System.out.println("You are 6th sem");
break;
default:
System.out.println("Please give the valid sem.");
break;
}
}
} }
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
Exercise 5: Addition of two matrices using for loops Exercise 6: Addition of two matrices using while loops
public class Exercise5 public class Exercise6
{ {
public static void main(String args[]) public static void main(String args[])
{ {
//creating two matrices //creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}}; int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}}; int b[][]={{1,3,4},{2,4,3},{1,2,4}};
int c[][]=new int[3][3]; int c[][]=new int[3][3];
int i=0; int j;
//adding and printing addition of 2 matrices while(i<3)
for(int i=0;i<3;i++) {
{ j=0;
for(int j=0;j<3;j++){ while(j<3)
c[i][j]=a[i][j]+b[i][j]; {
System.out.print(c[i][j]+"\t"); c[i][j]=a[i][j]+b[i][j];
} System.out.print(c[i][j]+"\t");
System.out.println();//new line j++;
}
} }
} System.out.println();//new line
i++;
}
}
}
Exercise 7: Addition of two matrices using do while loops
public class Exercise7
{
public static void main(String args[])
{
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
int c[][]=new int[3][3];
int i=0; int j;
do
{
j=0;
do
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+"\t");
j++;
}while(j<3);
System.out.println();//new line
i++;
}while(i<3);
}
}
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER
Object Oriented Programming and Design with Java 20CS43P
Exercise 8: Demo of break statement with for loop Exercise 9: Demo of continue statement with for loop
public class Exercise8 public class Exercise9
{ {
public static void main(String args[]) public static void main(String args[])
{ {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i == 4) if (i == 4)
{ {
break; continue;
} }
System.out.println(i); System.out.println(i);
} }
} }
} }
Exercise 10: Demo of break statement with while loop Exercise11: Demo of continue statement with while loop
public class Exercise10 public class Exercise11
{ {
public static void main(String args[]) public static void main(String args[])
{ {
int i = 0; int i = 0;
while (i < 10) { while (i < 10) {
System.out.println(i);
i++; if (i == 4) {
if (i == 4) { i++;
break; continue;
} }
} System.out.println(i);
} i++;
} }
}
}
COMPUTER SCIENCE & ENGG SJP-BANGALORE BHASKAR M- LECTURER