Download Programming with Java - Middlesex University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CMT4001 -- Programming Software Applications
Repetitions and Methods
Week 4
Dr. Xiaohong Gao
Contents
To become familiar with repetitions;
To become familiar with while loop;
To become familiar with do loop;
To become familiar with for loop;
To become familiar with nested for loop;
To become familiar with methods;
Repetitions
 while Loops
 do Loops
 for Loops
 break and continue
while Loop
The syntax for the while loop is:
while ( continue – condition)
{
// Loop body
}
Example: TestWhile.java
while Loop Flow Chart
true
Continue
condition ?
true
Statement(s)
Next
statement
false
//TestWhile.java
public class TestWhile
{
// main method
public static void main(String[]args)
{
int data;
int sum = 0;
// read an initial data
System.out.println(“Enter an int value);
data = MyInput.readInt();
// keep reading until the input is 0
while (data !=0)
{
sum+= data;
System.out.println(“Enter an int
value, the program exists if
the input is 0”);
data =MyInput.readInt();
}
System.out.println(“ The sum is” + sum);
}
}
do
Loop
• The do loop is a variation of while loop.
Its syntax is :
do
{
// loop body
}while(condition)
Example:TestDo.java
do
Loop
Statement(s)
true
Continue
condition ?
false
Next
statement
TestDo.java
// TestDo.java; test the do loop
public class TestDo
{
public static void main(String[]args)
{
int data;
int sum = 0;
do
{
data = MyInput.readInt();
sum+= data;
}while (data !=0);
TestDo.java
System.out.println(“The sum is “+ sum):
}
}
The for Loop
The general syntax for for loop is:
for( i= initialValue;
{
// for loop body
}
i<endValue;
i++)
The for Loop
int i = 0;
while (i < 100)
{
System.out.println("Welcome to Java! );
i++;
}
Example:
int i;
for (i = 0; i<100; i++)
{
System.out.println("Welcome to Java! );
}
for Loop Flow Chart
Evaluate
Control-variable Expression
expression
Adjustment
expression
Continue
condition?
true
Statement(s)
(loop-body)
Next
Statement
false
The for Loop
i = 0;
i=++
i < 100 ?
System.out.println
(“Welcome to
Java!”);
Next
statement
TestSum.java
// TestSum.java; Compute sum =0.01+0.02+...+1
public class TestSum
{
// main method
public static void main(String[]args)
{
//initialize sum
float sum = 0;
// keep adding 0.01 to sum
for(float i=0; i<=1.0f;i+0.01f)
sum+=i;
TestSum.java
// display result
System.out.println(“The sum is “+ sum);
}
}
Using nested for loops
//TestMulTable.java; display a multiplication
//table;
public class TestMulTable
{
//main method
public static void main(String[]args)
{
//display the table heading
System.out.println(“
Multiplication
table”);
System.out.println(“…………………………………………..”);
// display the number title
System.out.println(“
| “);
TestMulTable.java
for(int j=1; j<=9; j++)
System.out.println(“ “ + j):
System.out.println(“ “);
// print table body
for(int i =1; i<=9; i++)
{
for(int j=1;j<=9;j++)
{
// display the product and align properly
if(i*j <10)
System.out.print(“ “ +i*j);
else
System.out.print(“ ”+i*j);
}
TestMulTable.java
System.out.println();
}
}
}
The break Keyword
Continue
condition?
true
Statement(s)
break
Statement(s)
Next
Statement
false
The continue Keyword
Continue
condition?
true
Statement(s)
continue
Statement(s)
Next
Statement
false
TestBreak.java
//TestBreak.java;test the break keyword
//in the loop
public class TestBreak
{
// main method
public static void main(String[]args)
{
int sum =0;
int item =0;
while(item<5)
{
item++;
sum+= item;
TestBreak.java
if(sum >=6) break;
}
System.out.println(“The sum is”+ sum);
}
}
TestContinue.java
//TestContinue.java; test the continue
//keyword
public class TestContinue
{
public static void main(String[]args)
{
int sum = 0;
int item = 0;
while(item<5)
{
item++;
if(item == 2) continue;
sum+=item;
}
TestContinue.java
System.out.println(“The sum is “+ sum);
}
}
PrintPyramid.java
//PrintPyramid.java;print a pyramid of numbers
public class PrintPyramid
{
public static void main(String[]args)
{
for(int row = 1;row<6; row++)
{
// print leading spaces
for(int column =1;column<6-row;column++)
System.out.print(“ “);
// print leading numbers
for(int num=row; num>=1; num--)
System.out.print(num);
PrintPyramid.java
//print ending numbers
for(int num=2; num<=row; num++)
System.out.print(num);
// start a new line
System.out.println();
}
}
}
Methods
modifier
returnValueType
methodName
public static int max(int num1, int num2)
{
int result = 0;
parameters
Method
body
if(num1>num2)
result = num1;
else
result = num2;
return result;
}
Return value
TestMax.java
//TestMax.java;
public class TestMax
{
public static void main(String[]args)
{
int i = 5;
int j = 2;
int k = max(i,j);
System.out.println(“The maximum
between “ +i+” and”+j+” is”+k);
}
TestMax.java
// a method for finding the max
//between two numbers
static int max(int num1, int num2)
{
if(num1>num2)
return num1;
else
return num2;
}
}
TestMax.java
void nPrintln(String message, int n)
{
for( int i=0; i< n; i++)
System.out.println(message);
}
TestPassByValue.java
//TestPassByValue.java
public class testPassByValue
{
public static void main(String[]args)
{
// declare and initialise variables
int num1 =1;
int num2 =2;
System.out.println(“Before invoking the swap
method, num1 is”+num1+”and num2 is”+num2);
TestPassByValue.java
//invoke the swap method to attempt to
// swap two variables
swap(num1, num2);
System.out.println(“After invoking the swap
method, num1 is”+num1+”and num2 is”+num2);
//the method for swapping two variables
static void swap(int num1, int num2)
{
System.out.println(“
inside the swap
method”);
System.out.println(“
before swapping n1
is”+n1+”n2 is”+ n2);
TestPassByValue.java
//swapping n1 with n2
int temp=n1;
n1=n2;
n2=temp;
System.out.println(“
is “+n1+”n2 is”+n2);
}
}
After swapping n1
TestPassByValue.java
swap(num1, num2)
1
num1
Pass by
value
2
num2
n1
1
swap
n2
2
swap(n1,n2)
Execute swap
n1
2
n2
2
temp
1
Summary
• Repetitions;
• while loop;
• do loop;
• for loop;
• nested for loop;
• methods;
Related documents