CS8392-Object
Oriented Programming
UNIT - I
INTRODUCTION TO OOP AND JAVA
FUNDAMENTALS
Prepared by,
Mrs. T. SUGANYA,
M.E.,
JAVA
Topics
Iteration Statements
Jump Statements
Simple java program execution
Fundamental Programming
Structures
8. Control Statement
Used to change the sequential
flow of execution
Looping/Iteration statements are
used to execute a set of statements
repeatedly until some condition holds
true.
Types
for loop
while loop
do while loop
For loop
for ( initialization ; condition;
increment/decrement)
statement;
• Control Variable is used to control the no. of
repetitions
• Initialize the control variable i=1
• Condition i<=10
• Increment or decrement the control variable
i++
Valid for
for(i=1,j=1;i<=6;i++,j++)
for(;i<=10;)
For Statement
class iteration
{
public static void main(String args[])
{
int i; Number
System.out.println(“Numbers”); s
i=1
for( i=1;i<=5;i++) i=2
{ i=3
i=4
System.out.println(“i=”+i);
i=5
} Over
System.out.println(“Over”);
}
while loop
initialization ;
while(condition)
{
Body of the loop;
increment/decrement;
}
• Initialization of the control variable should be
done prior
• Condition is specified in the loop
• Increment or decrement the control variable
should be done within the body of the loop
while Statement
class iteration
{
public static void main(String args[])
{
int i=1; Number
System.out.println(“Numbers”); s
i=1
while( i<=5) i=2
{ i=3
i=4
System.out.println(“i=”+i);
i=5
i++; Over
}
System.out.println(“Over”);
}
do… while loop
initialization ;
do
{
Body of the loop;
increment/decrement;
}
while(condition);
• Initialization of the control variable should be
done prior
• Condition is specified in the loop
• Increment or decrement the control variable
should be done within the body of the loop
do…while Statement
class iteration
{
public static void main(String args[])
{
int i=1; Number
System.out.println(“Numbers”); s
i=1
do i=2
{ i=3
i=4
System.out.println(“i=”+i);
i=5
i++; Over
}
while(i<=5);
System.out.println(“Over”);
}
Break Statement
terminates the loop (for, while
and do...while loop) immediately
when it is encountered. The
break statement is used with
decision making statement such
as if...else, switch.
break Statement
class iteration
{
public static void main(String args[])
{
int i;
System.out.println(“Numbers”);
for( i=1;i<=10;i++)
{
if(i==3)
break;
System.out.println(“i=”+i);
}
System.out.println(“Over”);
}
continue Statement
is a jump statement-can
be used only inside for loop,
while loop and do-while loop.
break Statement
class iteration
{
public static void main(String args[])
{
int i;
System.out.println(“Numbers”);
for( i=1;i<=10;i++)
{
if(i==3)
continue;
System.out.println(“i=”+i);
}
System.out.println(“Over”);
}
return Statement
Used to transfer back the program
control to the caller of a method.
Every method in Java is declared
with a return type and it is
mandatory for all java methods.
A return type may be a primitive
type like int, float, double, a
reference type or void
type(returns nothing)
Eg:
return a; return 10;
return;
Scanner methods - Input
Method Description
nextBoolea Reads a boolean value from the user
n()
nextByte() Reads a byte value from the user
nextDouble Reads a double value from the user
()
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Enter any number: 101
Output: The number entered by user:
101
import java.util.Scanner; // Import the
Scanner class
class MyClass {
public static void main(String[] args)
{
// Create a Scanner object
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
// Read user input
String userName = myObj.nextLine();
// Output user input
System.out.println("Username is: " +
userName);
}
Download the Zip
Extract the ZIP folder
Install Java application –
double click
Open Notepad/Notepad+
+
Save the file
Compile & Run the
Program