Loops(part1)

Damitha karunarathne
3 min readMay 31, 2020

--

Hi readers!!! Hope you read out my previous articles which were all about Java programming languages.This is my eight article and I am going to discuss here about loops in Java.Mainly here I am going to discuss about for loop from this article and I may go further about loops with in next articles.

Loops in Java

Loops are handy in cases in which you want to run the same code repeatedly, adding a different value each time.Java has three types of loops: for, while, and do while.

for loop

The for loop is commonly used when creating a loop.

for ( initialization ; condition ; iteration))
{
code block to be executed
}

Ex:1

public class ThirdProgram{
public static void main(String args[]){
for (int a = 1; a <= 10; a++)System.out.println(a + "\n");

}
}
//out put as follow
1
2
3
4
5
6
7
8
9
10

Also above loop can be written as below,initialization is optional, and can be omitted, if your values are set before the loop starts.In here we get same result as above

int a=1;
for(;a<=10;a++)

Also we can write above statement as below.Iteration can be omitted if you increment your values inside the loop.

int i = 0;
for (; i < 10; ) {
System.out.println(i);
i++;
}

Ex:2

int x=0;
for(x=0;x<=10;x+=2)
System.out.println(x);
//output
0 2 4 6 8 10

This is another try,

int x=0;
for(x=0;x<=10;x+=2)
System.out.println("number is"+x+"\n");

Ex:3

int i,j;
for (i=1, j=7; i<=5 && j<=10; i++,j++)
System.out.println("i: " + i + " j: " +j + "\n");
//output
i:1 j:7
i:2 j:8
i:3 j:9
i:4 j:10

Ex:4

Below examples answers are depend on the curly braces.

var x=1;
for(x; x<10;x++){}
x+=x+100;
System.out.println(x) //output:120 [[[....nothing in the curly braces]]]
var x=1;
for(x; x<10;x++)
{
x+=x+100;
}
System.out.println(x) //output:103
In similar for this we can get below example toovar i = 1;
for (; i<=5; i++)
{
System.out.println(i)
}
System.out.println('Now, i = '+i)
//It will give : 1 2 3 4 5 Now, i = 6
var x=1; 
for(x; x<10;x++)
{
x+=x+100;
System.out.println(x);
}
//output:102 [

Ex:5

for (int i = 0; i <= 10; i ++)  
{
if (i % 2 != 0)
{
System.out.println(i);
}
}
//output
1 3 5 7 9

Ex:6

int x;
System.out.println("enter a number");
int num=sc.nextInt();

for(x = num; x < 10;x++)
System.out.println("Next number is "+x);

System.out.println("Done.");
//out put is
enter a number // let us think that entered number is 5, This may continues as
Next number is 5
Next number is 6
Next number is 7
Next number is 8
Next number is 9

Ex:7

Let us see to add these for loops for Strings too.Here we can’t use two data types in side a loop.If we added char we can’t add again int there.

int i=1; 
for(char x='a'; x<='z'; x++)
{
System.out.println("Alphabet's "+i+". letter is " + x);
i++;
}
//out put is
Alphabet's 1.letter is a
Alphabet's 2.letter is b
Alphabet's 3.letter is c
Alphabet's 4.letter is d
Alphabet's 26.letter is z

Ex:8

To type same words for 10 times we can use for loop

int i;
for(int i=0; i<=10; i++)
{
System.out.println("Great");
}
//out put is typing 'Gtreat' for 10 types

Ex:9

for(int x=2; x<=1000; x=x*x) 
{
System.out.println(x);
}
//output is
2 4 16 256

I have discussed here 9 Examples regarding with for loop.Loops are highly used in when we create software products.I may discuss about whileloop and dowhile loop with in my next article and also about star patterns.Thank you for reading.Keep in touch.

--

--