20CS43P: OOP With Java
Object Oriented Programming and Design with Java
Week 5
5.1 Conditional statements
Decision making structures have one or more conditions to be evaluated or tested by the program, along with a
statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
Java programming language provides following types of decision making statements.
1. if statement : An if statement consists of a boolean expression followed by one or more statements.
2. if...else statement : An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.
3. nested if statement : You can use one if or else if statement inside another if or else if statement(s).
4. switch statement : A switch statement allows a variable to be tested for equality against a list of values.
5.1.1 if statement in java
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
Following is the syntax of an if statement −
if(Boolean_expression)
{
// Statements will execute if the Boolean expression is true
}
If the Boolean expression evaluates to true then the block of code inside the if statement will be executed.
If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed.
Flow Diagram
Shatajbegum Nadaf, SCKGPT, Bankapur Page 1
20CS43P: OOP With Java
Example
public class Test {
public static void main(String args[]) {
int x = 10;
if( x < 20 ) {
[Link]("Number is less than 20");
}
}
}
Output
Number is less than 20
5.1.2 if-else statement in java
An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.
Syntax
Following is the syntax of an if...else statement −
if(Boolean_expression)
{
// Executes when the Boolean expression is true
}
else
{
// Executes when the Boolean expression is false
}
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code
will be executed.
Flow Diagram
Shatajbegum Nadaf, SCKGPT, Bankapur Page 2
20CS43P: OOP With Java
Example
public class Test {
public static void main(String args[]) {
int x = 30;
if( x < 20 ) {
[Link]("This is if statement");
}else {
[Link]("This is else statement");
}
}
}
Output
This is else statement
5.1.3 The if...else if...else Statement
An if statement can be followed by an optional else if...else statement, which is very useful to test various
conditions using single if...else if statement.
When using if, else if, else statements there are a few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax
Following is the syntax of an if...else statement −
if(Boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
}
else if(Boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
else if(Boolean_expression 3)
{
// Executes when the Boolean expression 3 is true
}
else {
// Executes when the none of the above condition is true.
}
Example
public class Test {
public static void main(String args[]) {
int x = 30;
if( x == 10 ) {
[Link]("Value of X is 10");
}
Shatajbegum Nadaf, SCKGPT, Bankapur Page 3
20CS43P: OOP With Java
else if( x == 20 ) {
[Link]("Value of X is 20");
}else if( x == 30 ) {
[Link]("Value of X is 30");
}else {
[Link]("This is else statement");
}
}
}
Output
Value of X is 30
5.1.4 nested if statement in java
We can use one if or else if statement inside another if or else if statement.
Syntax
The syntax for a nested if...else is as follows −
if(Boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
}
You can nest else if...else in the similar way as we have nested if statement.
Example
public class Test {
public static void main(String args[]) {
int x = 30;
int y = 10;
if( x == 30 ) {
if( y == 10 ) {
[Link]("X = 30 and Y = 10");
}
}
}
}
Output
X = 30 and Y = 10
5.1.5 switch statement in java
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each case.
Syntax
The syntax of enhanced for loop is −
Shatajbegum Nadaf, SCKGPT, Bankapur Page 4
20CS43P: OOP With Java
switch(expression)
{
case value :// Statements
break; // optional
case value :// Statements
break; // optional
// You can have any number of case statements.
default : // Optional
// Statements
}
The following rules apply to a switch statement −
The variable used in a switch statement can only be integers, convertable integers (byte, short, char),
strings and enums.
You can have any number of case statements within a switch. Each case is followed by the value to be
compared to and a colon.
The value for a case must be the same data type as the variable in the switch and it must be a constant or a
literal.
When the variable being switched on is equal to a case, the statements following that case will execute
until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line
following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to
subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The
default case can be used for performing a task when none of the cases is true. No break is needed in the
default case.
Flow Diagram
Shatajbegum Nadaf, SCKGPT, Bankapur Page 5
20CS43P: OOP With Java
Example
public class Test {
public static void main(String args[]) {
// char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :[Link]("Excellent!");
break;
case 'B' :[Link]("Very Good");
break;
case 'C' :[Link]("Well done");
break;
case 'D' :[Link]("You passed");
break;
case 'F' :[Link]("Better try again");
break;
default :[Link]("Invalid grade");
}
[Link]("Your grade is " + grade);
}
}
Output
Well done
Your grade is C
5.2 Iterative statements: Loop Control
There may be a situation when you need to execute a block of code several number of times. In general,
statements are executed sequentially: The first statement in a function is executed first, followed by the second,
and so on.
Programming languages provide various control structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement or group of statements multiple times
5.2.1 while Loop in java
A while loop statement in Java programming language repeatedly executes a target statement as long as a
given condition is true.
Syntax
The syntax of a while loop is −
while(Boolean_expression)
{
// Statements
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and
true is any non zero value.
Shatajbegum Nadaf, SCKGPT, Bankapur Page 6
20CS43P: OOP With Java
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed.
This will continue as long as the expression result is true. When the condition becomes false, program control
passes to the line immediately following the loop.
Flow Diagram
Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the
result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
[Link]("value of x : " + x );
x++;
[Link]("\n");
}
}
}
Output
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
Shatajbegum Nadaf, SCKGPT, Bankapur Page 7
20CS43P: OOP With Java
5.2.2 do while loop in java
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least
one time.
Syntax
Following is the syntax of a do...while loop −
do
{
// Statements
}while(Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute
once before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop
execute again. This process repeats until the Boolean expression is false.
Flow Diagram
Example
public class Test {
public static void main(String args[]) {
int x = 10;
do {
[Link]("value of x : " + x );
x++;
[Link]("\n");
}while( x < 20 );
}
}
Output
value of x : 10
value of x : 11
Shatajbegum Nadaf, SCKGPT, Bankapur Page 8
20CS43P: OOP With Java
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
5.2.3 for loop in java
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be
executed a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax
The syntax of a for loop is −
for(initialization; Boolean_expression; update)
{
// Statements
}
Flow Diagram
Here is the flow of control in a for loop −
The initialization step is executed first, and only once. This step allows you to declare and initialize any loop
control variables and this step ends with a semi colon (;).
Shatajbegum Nadaf, SCKGPT, Bankapur Page 9
20CS43P: OOP With Java
Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body
of the loop will not be executed and control jumps to the next statement past the for loop.
After the body of the for loop gets executed, the control jumps back up to the update statement. This
statement allows you to update any loop control variables. This statement can be left blank with a semicolon
at the end.
The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body
of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop
terminates.
Example
Following is an example code of the for loop in Java.
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x + 1) {
[Link]("value of x : " + x );
[Link]("\n");
}
}
}
This will produce the following result −
Output
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
5.2.4 Enhanced for loop in Java
As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements
including arrays.
Syntax
Following is the syntax of enhanced for loop −
for(declaration : expression) {
// Statements
}
Declaration − The newly declared block variable, is of a type compatible with the elements of the array you are
accessing. The variable will be available within the for block and its value would be the same as the current
array element.
Expression − This evaluates to the array you need to loop through. The expression can be an array variable or
method call that returns an array.
Shatajbegum Nadaf, SCKGPT, Bankapur Page 10
20CS43P: OOP With Java
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
[Link]( x );
[Link](",");
}
[Link]("\n");
String [] names = {"Shataj", "Seema", "Savitri", "Shilpa"};
for( String name : names ) {
[Link]( name );
[Link](",");
}
}
}
Output
10, 20, 30, 40, 50,
Shataj,Seema,Savitri,Shilpa
5.2.5 Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all
automatic objects that were created in that scope are destroyed. Java supports the following control statements:
break and continue.
Break statement
The break statement in Java programming language has the following two usages −
When the break statement is encountered inside a loop, the loop is immediately terminated and the
program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement (covered in the next chapter).
Flow Diagram
Shatajbegum Nadaf, SCKGPT, Bankapur Page 11
20CS43P: OOP With Java
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
[Link]( x );
[Link]("\n");
}
}
}
Output
10
20
Continue statement
The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to
the next iteration of the loop.
In a for loop, the continue keyword causes control to immediately jump to the update statement.
In a while loop or do/while loop, control immediately jumps to the Boolean expression.
Flow Diagram
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
Shatajbegum Nadaf, SCKGPT, Bankapur Page 12
20CS43P: OOP With Java
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
[Link]( x );
[Link]("\n");
}
}
}
Output
10
20
40
50
Shatajbegum Nadaf, SCKGPT, Bankapur Page 13