You are on page 1of 8

For Loop

■ Another kind of looping structures is the for loop. It differs from the while loop in that the control
variable initialization, condition checking and control variable increment are done in the same line,
Example:
for (int i; i <= 10 ; i++ )
{ statements; }
■ for is a keyword and the sentence between curly brackets consists of 3 parts separated by a
semicolon:
– The first part declares and initialize the control variable i to an initial value.
– The second part is the condition to be checked to decide whether to stop the loop or no
(depending on the last value, 10 in the example).
– The last part says that the control variable should be incremented after each iteration.

Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Example:
■ Repeat the example discussed in the previous lecture (drawing stars) using for loop.
■ Answer:
public class Stars2{
public static void main(String args[]){
String star, newstar; // declaration
star = "*"; //initialization
newstar = star;
for (int i=1; i <=5; i++ ) // for loop
{ System.out.printl(newstar);
newstar = newstar+ star; }
System.out.print(“\n”);
}}

Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Nested Loops:
■ Nested loops means a loop inside another loop. They could be all of same type or different say one is a for
loop and the second is a while loop for example.
■ Lets repeat the previous example using two loops instead of one. Note that we will need 2 control variables,
one for each loop.
public class Stars3{
public static void main(String args[]){
String star;
int counter , i ;
counter =1;
star = "*";
while (counter <=5 )
{ for( i=1; i<= counter; i++)
{ System.out.print (star); }
counter = counter +1;
System.out.print(“\n”);
} Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Nested Loops (continue) :
■ By modifying this program little bit we can get the stars drawn in the other way around starting by
5 and ending by 1:
public class Stars3{
public static void main(String args[]){
String star;
int counter , i;
counter =1;
star = "*";
while (counter <=5 )
{ for( i=5; i>= counter, i--)
{ System.out.print (star);}
counter = counter +1;
System.out.print(“\n”);
} *****
}} ****
■ The output will look like: ***
**
*

Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Arrays
■ Arrays: data structures consisting of related data items (elements) of the same type.
■ To refer to a particular element in the array you should specify the name of the reference to the
array and the position number of that element in the array.
■ Array declaration and creation:
– int C[];  declares an array of type integer with reference named C.
– C = new int [12]  the array is created to fit 12 elements
– Or : int C[]= new int[12]
■ All the 12 elements of the array above are initially set to zero.
■ More then one array could be declared in the same row if they are of the same type exactly like
variables , for example:
String b[] = new String[100], X []= new String [27]
■ Array size is given by arrayname.length.

Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Arrays
■ Try the following code:
import javax.swing.*;
public class Arrays1{
public static void main(String args[])
{
int Arr[];
Arr = new int[10];
String result;
result = "Index \t Value \n";

for(int counter = 1; counter < Arr.length; counter ++)


result += counter + "\t" + Arr[counter]+"\n";

JTextArea resultArea = new JTextArea();


resultArea.setText(result);
JOptionPane.showMessageDialog(null,resultArea);
System.exit(0);
}}

■ Note that A JTextArea is a multi-line area that displays plain text


Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Arrays (continue)
■ Arrays values can be either initialized to fixed values as in the following example:
import javax.swing.*;
public class Arrays2{
public static void main(String args[])
{ int Arr[]={25,20,30,15,19,34,33,27,9,18};
String result;
result = "Index \t Value \n";

for(int counter = 1; counter < Arr.length; counter ++)


result += counter + "\t" + Arr[counter]+"\n";

JTextArea resultArea = new JTextArea();


resultArea.setText(result);
JOptionPane.showMessageDialog(null,resultArea);
System.exit(0); }}
■ Note if the array is of type string its elements should be enclosed between double quotations and
single quotation if it is of type char.
Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel
Arrays (continue)
■ Another way to insert element values at run time as in the following code:
import javax.swing.*;
public class Arrays3{
public static void main(String args[])
{
int Arr[ ];
Arr = new int[10];
String result,element;
result = "Index \t Value \n";
for(int counter = 1; counter < Arr.length; counter ++)
{
element = JOptionPane.showInputDialog("insert array element number"+counter);
Arr[counter] = Integer.parseInt(element);
result += counter + "\t" + Arr[counter]+"\n";
}
JTextArea resultArea = new JTextArea();
resultArea.setText(result);
JOptionPane.showMessageDialog(null,resultArea);
Text book: “ Java How To Program ” 5th ed., H. M . Deitel and P. J Deitel

You might also like