1.6.1 For Loops
Examples of for loops
/**
* An example of a method containing a for loop
*
*/
public void sampleForLoop()
{
String str;
str ="";
for(int x = 0; x<5; x++){
str = str +"*";
}
System.out.println(str);
}
Note: When + is used to add two or more Strings together, the strings are joined together. Joining of two or more strings is called concatenation
public void sumOneToTen()
{
int sum;
sum =0;
for(int x = 1; x <= 10; x++){
sum = sum + x;
}
System.out.println(sum);
}
public void sumFirstTenEvenNumbers()
{
int sum;
sum =0;
for(int x = 1; x <= 10; x++){
sum = sum + x*2;
}
System.out.println(sum);
}
public void sumStartToStop()
{
int sum, start, stop;
sum =0;
start =9;
stop = 15;
for(int x = start; x <= stop; x++){
sum = sum + x;
}
System.out.println(sum);
}