You are on page 1of 0

Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved.

No part of this material may be reproduced and sold.


1
Introduction to Programming
Introduction to Programming
(
(
Using Java)
Using Java)
Nandika Kasun
Nandika Kasun
University of Colombo School of Computing (UCSC)
University of Colombo School of Computing (UCSC)
University of Colombo
University of Colombo
Sri Lanka
Sri Lanka
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
2
Control Statements
Control Statements
Control Statements
If ....Else Statements
if ( Expression )
statement1;
else
statement2;
If Expression true then statement1 is executed;
otherwise statement2 is executed. else is optional.
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
3
If ..else statements
If ..else statements
If more Statements are to be performed, all the
statements should be included within curly brackets.
If ( Expression) {
statement a;
statement b;
}
else {
statement c;
statement d;
}
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
4
Conditional Operator ?:
Conditional Operator ?:
Expression ? Statement 1 : Statement 2
If expression is true
If expression is false
X= ((marks>50) ? Pass : Failed);
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
5
Control Statements
Control Statements
The switch Statement
switch (Test) {
case valueOne :
resultOne;
break;
case valueTwo :
resultTwo;
break;

default : defaultResult;
}
Variable or
Expression
that evaluates
to byte,char or
int
cannot be float,
long String
or any other
object
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
6
Control Statements
Control Statements
The switch Statement contd..
Test is compared with each of the case values
if a match is found, statement(s) are executed.
If a match is not found, default (optional)
statements are executed. (if default statement
is not there, case statement completes without
doing anything )
if break statement is not included
when a match is found, corresponding
statements and all the other case
statements further down are executed
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
7
Control Statements
Control Statements
The switch Statement contd..
Multiple statements within a case value can be used
without curly brackets
the test condition is limited to testing simple equality
unlike in if statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
8
The do . while Statement
do {
statement 1;
statement 2;
..
..
statement n
} while (Boolean expression)
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
9
The do . while Statement contd..
The enclosed Statements will be executed
at least once irrespective of the Boolean
expressions result.
As long as the Boolean expressions yields
true the Statements will be repeated.
The curly brackets are not necessary for a
single Statement.
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
10
Boolean
Expression ?
Statement 1;
Statement 2;
..
Statement n;
True
False
The do..while
Loop
do while statement
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
11
The while Statement
while (Boolean expression) {
statement 1;
statement 2;
..
..
Statement n
}
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
12
The while Statement
The Boolean expression is checked first.
If it yields true, Statements 1 to n are
executed.
Boolean expression is once again checked.
if yields true Statements are executed again.
This process is repeated till Boolean
expression yields false.
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
13
Boolean
Expression ?
Statement 1;
Statement 2;
..
Statement n;
True
False
The while Loop
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
14
The for Statement
for (initilization; Boolean expression; increment) {
statement 1;
statement 2;
..
..
Statement n
}
If there is only
a single statement
Curly brackets are
not necessary
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
15
The for Statement
Initialization;Boolean Expression ;Increment)
for (
Statement 1
..
Statement n
{
1
4
3
2
false
true
5
Steps 3,4 & 5
is repeated till
expression becomes
false
Control Statements
Control Statements
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
16
Jump Statements
Jump Statements
break statement
jumps out of a loop and effectively bypass
the loop condition.
continue statement
jumps out of the current iteration of the loop
return statement
terminates the method (function). Jumps to
the place immediately after the function call.
(break, continue,
(break, continue,
return)
return)
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
17
Labeled loops
A label break statement terminates the current
loop and proceeds to the first statement that
follows the loop that is labeled by the identifier
that follows the keyword break.
for (int i=0; i<3; i++)
{
resume:
for ()
{
..
if (..) break resume;
}
}
eg.
Labelled Loop
Labelled Loop
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
18
Recursion
Recursion
Methods that invoke themselves are known as
Recursive methods.
Recursion Example
class Factorial {
static long calcFact(int n) {
if n < 2
return 1;
return n*calcFact(n-1);
{
}
Recursive call
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
19
Recursion
Recursion
Class TestFactorial {
public static void main (String args[ ]) {
System.out.print(Factorial of 5 is);
System.out.println( Factorial.calcFact(5));
}
}
To test the Factorial Class
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.
20
Summary
Summary
Exercise:
Exercise:

Implement a for loop using a while loop


Implement a for loop using a while loop

Write a program which will display the followings


Write a program which will display the followings
*
*
**
**
***
***
****
****
*
*
***
***
*****
*****
This week we learned control statements