0% found this document useful (0 votes)
17 views9 pages

Java Control Flow: Conditionals & Loops

Uploaded by

pr14042013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Java Control Flow: Conditionals & Loops

Uploaded by

pr14042013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

The if-else-if Ladder


if(condition)
statement; else
if(condition) statement;
else if(condition)
statement;
.
.
else statement;
Switch Statement

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

The expressionis 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 expressionis equal to value2, the code after case value2: is
executed.
• If there is no match, the code afterdefault: is executed.

switch (expression)
{
case value1: // statement sequence
break;
case value2: // statement sequence
break;
:
:
:
case valueN: // statement sequence
break;
default: // default statement
sequence
}

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 expression is true.
Block while its
controlling

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

while(condition) loop at least once, even if the conditional expression is false


for(initialization; condition; iteration)
{
to begin with {

// body of loop do { // body

// body of loop }
Sometimes it is
desirable to Here is } while (condition);
the general form of the
for execute the body of
statement:
a while

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.

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

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 For all three loops (while, do-while,
loop. for), any Intermediate code is Thus, the return statement
bypassed. immediately terminates the
Third, it can be used as a 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.

Exercise 2: Find the greatest of three numbers


Exercise 1: Get a number from the user and print whether it is
positive or negative number

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

import [Link];
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[]
{ if ( num >
num2)
Scanner in = new 1 [Link]("The greatest: "
Scanner([Link]); + if num num1);
[Link]("Input number: "); ( 1
int input = [Link](); if (num2 > num1)
if (num2 > num3)
if (input > 0) [Link]("The greatest: " + num2);
{
[Link]("Number is
if (num3 > num1)
positive");
if (num3 > num2)
}
[Link]("The greatest: " + num3);
else if (input < 0)
}
{
}
[Link]("Number is
negative");
}
else
{
[Link]("Number is zero"
}
}

Exercise 4: Demo of Switch statement


Exercise 3: Demo of if else ladder

import [Link]; mport [Link];


public class Exercise3 { public class Exercise4 {
public static void main(String public static void main(String args[])
{ {
Scanner in = new
Scanner([Link]); Scanner in = new
Scanner([Link]);
[Link]("Input sem: "
[Link]("Input sem: ");
int sem = [Link](); int sem = [Link]();

if(sem==1) switch (sem)


[Link]("You are 1st sem"{
else if(sem==2) case (1):
[Link]("You are 2nd sem" [Link]("You are 1st
else if(sem==3) sem"); break;
case (2):
[Link]("You are 3rd
[Link]("You are 2nd
sem"); else if(sem==4) sem"); break;
[Link]("You are
case 4th
(3):
sem"); else if(sem==5) [Link]("You are 3rd
[Link]("You are 5th
sem"); break;
sem"); else if(sem==6) case (4):
[Link]( [Link]("You are 4th
sem"); else sem"); break;
[Link]("Please give case (5):
the valid
sem."); [Link]("You are 5th
sem"); break;
case (6):
} [Link]("You are 6th
sem"); break;
default:
[Link]("Please give the valid
sem."); break;
}
}
}

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


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 Exercise6


public class Exercise5
{
{
public static void main(String args[])
public static void main(String
{
{
//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;
while(i<3)
//adding and printing addition
{ of 2
matrices for(int i=0;i<3; j=0;
{ while(j<3)
for(int j=0;j<3;j++){ {
[j]=a[i][j]+b[i][j]; c[i][j]=a[i][j]+b[i][j];
[Link](c[
[Link](c[i][j]+"\
}
t");
[Link]();//new line
} j++;
}
}
[Link]();//new line
i++;
}
}

Exercise 7: Addition of two matrices using do while loops

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


Object Oriented Programming and Design with Java 20CS43P

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
{

{
c[i][j]=a[i][j]
+b[i][j];
[Link](c[i][j]
+"\t");
j++;

}while(j<3);

[Link]();//
new line i++;
}while(i<3);
}
}

Exercise 8: Demo of break statement with


Exercise 9: Demo
for loop
of continue statement with for loop

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS


public class Exercise8 public class Exercise9
Object
{ Oriented Programming and Design with Java { 20CS43P
public static void main(String public static void main(String
args[]) args[])
}{ }{
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i == 4) if (i == 4)
{ {
break; continue;
} }
[Link](i); [Link](i);
} }
} }

Exercise 10: Demo of break statement with while loop Demo of continue statement with while loop
Exercise11:

public class Exercise11


public class Exercise10
{
{
public static void main(String
public static void main(String
args[])
args[])
{ int i
{
= 0;
int i = 0;
while (i < 10) {
while (i < 10) {
[Link](
i++; if (i == 4) {
if (i == 4) { i++;
break; continue;
} }
} [Link](i);
} i++;
} }
}
}

COMPUTER SCIENCE & ENGG ARUNA N- LECTURER/CS

You might also like