ITC 1233 - OBJECT ORIENTED
PROGRAMMING
CONTROL STATEMENTS IN JAVA
Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
RECALLING…
ACTIVITY 01
public class Arithmetic{
public static void main ( String[] args ) {
int a = 17;
int b = 6;
System.out.println ( a + b );
System.out.println ( a – b );
System.out.println ( a * b );
System.out.println ( a / b );
System.out.println ( a % b );
}
}
ACTIVITY 02
public class Assignment{
public static void main ( String[] args ) {
int a = 45;
int b = 78;
int c;
System.out.println ( c = a );
System.out.println ( b += a );
System.out.println ( b -= a);
System.out.println ( b *= a );
System.out.println ( b /= a );
System.out.println ( b ^= a );
System.out.println ( b %= a );
}}
ACTIVITY 03
public class Comparison{
public static void main(String[] args) {
int a = 29;
int b = 32;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
}}
ACTIVITY 04
public class Logical{
public static void main ( String args[] ) {
int a = 16;
System.out.println ( a<5 && a<50 );
System.out.println ( a<5 || a<50 );
System.out.println ( ! ( a<5 && a<50
));
}
}
ACTIVITY 05
public class Bitwise{
public static void main( String[] args ) {
int a = 32;
int b = 76;
System.out.println ( a&b );
System.out.println ( a|b );
System.out.println ( a^b );
System.out.println ( ~a );
}
}
ACTIVITY 06
public class Unary{
public static void main ( String[] args ) {
int a = 10;
boolean b = True;
System.out.println ( a++ );
System.out.println ( ++a );
System.out.println ( a-- );
System.out.println ( --a );
System.out.println ( !b );
}
}
WHAT WOULD BE THE OUTPUT OF THIS EQUATION?
x = 4 / 2 + 8 * 4 - ( 5+ 2 ) % 3
CONTROL STATEMENTS IN JAVA
CONTROL STATEMENTS
A control statement in Java is a statement that determines whether
certain statements will be executed or not. It controls the flow of a
program.
A SIMPLE PROGRAM WITHOUT CONTROL
STATEMENTS
CONTROL STATEMENTS
SELECTION STATEMENTS
Selection statements are also known as branching statements or conditional or
decision-making statements.
It is used to select part of a program to be executed based on condition.
The selection statements in Java.
• if statement.
• if-else statement.
• If-else if statement. (Nested)
• Switch-case statement
IF-ELSE STATEMENT
IF-ELSE STATEMENT CONTD;
SWITCH
STATEMENT
SWITCH
STATEMENT
CONTD;
ITERATIVE STATEMENTS
In Java, repetition statements, often referred to as loops, allow you to
repeatedly execute a block of code based on a condition. They are used
when you need to perform a specific task multiple times.
Followings are the ways of achieving iterations within the programs
• For loop
• Enhanced for loop (For-each loop)
• While loop
• Do - while loop
WHILE STATEMENT
WHILE STATEMENT CONTD;
DO-WHILE
STATEMENT
DO-WHILE STATEMENT CONTD;
FOR STATEMENT
FOR STATEMENT CONTD;
JUMP STATEMENTS
In Java, jump statements are used to control the flow of a
program by transferring control to a different part of the code.
Java provides three main jump statements.
• Break statement
• Continue statement
• Return statement
BREAK STATEMENT
Whenever a break
statement is used, the loop
is terminated and the
program control is
resumed to the next
statement following the
loop.
BREAK STATEMENT
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
CONTINUE STATEMENT
The continue keyword
causes the loop to
immediately jump to the
next iteration of the
loop.
CONTINUE STATEMENT
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; }
System.out.println(i);
}
RETURN STATEMENT
The return statement is used to exit from a method and
return a value (if the method has a return type). It is used to
prematurely terminate the execution of a method and
pass control back to the calling method.
RETURN STATEMENT
public int divide(int numerator, int denominator) {
if (denominator == 0) {
return -1;
}
return numerator / denominator;
}
ACTIVITY 01
Draw flow charts for the following scenarios.
1. Read the set of 10 integers, display them and display the
total of them at the end.
2. Read set of 12 integers and display the number of odd
numbers out of them.
FUNCTIONS IN JAVA
Have You Heard Before Regarding Java FUNCTIONS?
METHODS IN JAVA
• A block of code
• Can only run by calling the method name.
• Can pass required data into the method using parameters.
• Used for performing some specific task together with various
statements.
• Methods can reuse: where code at once, and use it many
times.
METHODS CONTD;
Syntax:
public class MyClass{
static void firstMethod() {
// code to be executed
}
}
CALLING A METHOD
When calling a method: Need to mention the method name with parentheses and
semicolon.
public class MyClass {
static void firstMethod() {
System.out.println("This is the way of calling a method!");
}
public static void main(String[] args) {
firstMethod();
}
}
// Outputs "This is the way of calling a method!"
PARAMETERS AND ARGUMENTS
• Data which can be passed into a method is called Parameters.
• Can also be defined as variables inside a method.
• Can have any number of parameters inside a method.
• When it comes to method calling,
• Values / data should be passed for the parameters is called Arguments.
• Syntax is same as parameters.
• Any type of data can be passed.
PARAMETERS AND ARGUMENTS
public class MyClass{
static void firstMethod(String fname) {
System.out.println(fname + " Fernando");
}
public static void main(String[] args) {
firstMethod("Samson");
firstMethod("Shenya");
}
}
// Samson Fernando
// Shenya Fernando
SCOPE
Variables are only accessibles inside the region what have created
in java.
• Method scope - belongs to the area of inside a method body.
• Block scope - belongs to the area of specific code block.
METHOD SCOPE
public class MyClass{
public static void main(String[] args) {
// Code here CANNOT use x
int x = 100;
// Code here can use x
System.out.println(x);
}
}
BLOCK SCOPE
public class MyClass {
public static void main(String[] args) {
// Code here CANNOT use x
{ // This is a block
// Code here CANNOT use x
int x = 100;
// Code here CAN use x
System.out.println(x);
} // The block ends here
// Code here CANNOT use x
}
}
ANY QUESTIONS?