Download presentation
Presentation is loading. Please wait.
1
CMT4001 -- Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4
2
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;
3
Repetitions while Loops do Loops for Loops break and continue
4
while Loop The syntax for the while loop is: while ( continue – condition) { // Loop body } Example : TestWhile.java
5
while Loop Flow Chart Continue condition ? Statement(s) Next statement false true
6
//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
7
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); }
8
do Loop The do loop is a variation of while loop. Its syntax is : do { // loop body }while(condition) Example: TestDo.java
9
do Loop Statement(s) Continue condition ? Next statement true false
10
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);
11
TestDo.java System.out.println(“The sum is “+ sum): }
12
The for Loop The general syntax for for loop is: for( i= initialValue; i<endValue; i++) { // for loop body }
13
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! ); }
14
for Loop Flow Chart
15
The for Loop i = 0; i < 100 ?i=++ System.out.println (“Welcome to Java!”); Next statement
16
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;
17
TestSum.java // display result System.out.println(“The sum is “+ sum); }
18
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(“ | “);
19
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); }
20
TestMulTable.java System.out.println(); }
21
The break Keyword
22
The continue Keyword
23
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;
24
TestBreak.java if(sum >=6) break; } System.out.println(“The sum is”+ sum); }
25
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; }
26
TestContinue.java System.out.println(“The sum is “+ sum); }
27
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);
28
PrintPyramid.java //print ending numbers for(int num=2; num<=row; num++) System.out.print(num); // start a new line System.out.println(); }
29
Methods public static int max(int num1, int num2) { int result = 0; if(num1>num2) result = num1; else result = num2; return result; } Return value returnValueTypemethodName parameters modifier Method body
30
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); }
31
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; }
32
TestMax.java void nPrintln(String message, int n) { for( int i=0; i< n; i++) System.out.println(message); }
33
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);
34
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);
35
TestPassByValue.java //swapping n1 with n2 int temp=n1; n1=n2; n2=temp; System.out.println(“ After swapping n1 is “+n1+”n2 is”+n2); }
36
TestPassByValue.java 2 2 1 n1 n2 temp 1 2 n1 n2 1 2 num1 num2 swap(n1,n2) swap(num1, num2) Pass by value Execute swap swap
37
Summary Repetitions; while loop; do loop; for loop; nested for loop; methods;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.