Day Nine Is Felling Fine In My Month of Java

in #java6 years ago

Adding More Internet Pollution, One Day At A Time

code-computer-cyberspace-225769.jpg
Image courtesy of pexels.com

I ask myself if I am polluting the world with more crap about Java. A simply internet search results in hundreds of pages on Java, but at the same time, this is giving me the momentum to keep going and hopefully I can make it through the month with more knowledge that when I started.

What Did I learn Today


Loops: Loops are used to repeat a set of statements until a particular condition is met
There are three types of loops in Java for loops, while loops and do while loops

For loops: The For loop is first initialized and this is performed only once. The condition in the for loop is then evaluated on each iteration before the statement is then run. If the condition is true the statement inside the body of the loop is run, if false the loop ends and the next statement in the application is then run. After each execution of the loops body, the loop counter is then incremented.

For example for( int i = 10; i > 1; i--)
int i = 10 is the initialisation
i > 1 is the condition
i-- is the loop counter

Sample Code

  1 class ForLoopExample {
  2         public static void main(String args[]) {
  3                 for( int i = 10; i > 1; i--) {
  4                         System.out.println("The value of i is: " + i);
  5                 }
  6         }
  7 }

Output

java ForLoopExample 
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2

Example of An Arrays in For Loops

  1 class ForArrayLoop {
  2         public static void main(String args[]) {
  3                 String arr[] = {"this", "is", "an", "array"};
  4                 for (String str : arr) {
  5                         System.out.println(str);
  6                 }
  7         }
  8 }

Output

java ForArrayLoop 
this
is
an
array

While Loops: In a while loop the condition is evaluated first and if it returns true then the statement inside the loop is executed. If it is false, the loops is complete and code jumps to the next statement. Unlike the for loop, the increment will happen inside the loop statement allowing the condition to eventually be false.

Sample Code

  1 class WhileLoopExample {
  2         public static void main(String args[]) {
  3                 int i = 10;
  4                 while(i > 1) {
  5                         System.out.println(i);
  6                         i--;
  7                 }
  8         }
  9 }

Output

java WhileLoopExample 
10
9
8
7
6
5
4
3
2

Try no to create an infinite loop as it will not stop. Sometimes you will do this by mistake but sometimes you will actually need to do this as part of your code

while (true){
    statement(s);
}

Do While Loops: In this case the statement in the loop will always run once as the condition is tested at the end of the loop. Just like the while loop the increment needs to happen inside the loop statement.

Sample Code

  1 class DoWhileLoopExample {
  2         public static void main(String args[]) {
  3                 int arr[]={2,11,45,9};
  4                 int i=0;
  5                 do {
  6                         System.out.println(arr[i]);
  7                         i++;
  8                 } while(i<4);
  9         }
 10 }

Output

java DoWhileLoopExample 
2
11
45
9

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 8 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/java-folder-structure-in-our-month-of-java

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63252.26
ETH 2662.75
USDT 1.00
SBD 2.79