View on GitHub

reads

https://yazansneneh.github.io/reads/

Read: 02 - Arrays, Loops, Imports

Packages and Import

Package declaration

Package declaration syntax

Imports

Common imports

most imported packages are:

Loops

for loops:

#### While loop and do white:
  * I use it when I don't know how many times i need to iterate, for example a user want to enter password.
  * syntax  `while(condition) {code ....};`
  * as long as the condition evaluate to true, the while loop keep running.
  

2.1 do while:
  * It's the while loop but do while exist because i might want to execute code inside at least once.
  * syntax:  `do { code ...} while(condition);`


### Branching Statements
#### break
* break is used when i want to break the iteration statement before it's finished based on a condition.
* so basically let's say my for loop will iterate 50 times, and i tell it to exit loop if it exceed 20 times.
* example:

for(int i=0; i<5; i++){ if(i>2){ break; // loop when i = 0, 1,2 }; };

#### continue:
* i use it when i want to skip executing code based on condition, for example i want for loop to keep iterating to the end, but on 5th iteration it should not execute.
* example:

for(int i=0; i<5; i++){ if(i>2){ continue; // skip executing when i > 2 }; };


## Arrays
* Arrays are variables container, that means it contain more than 1 variable.
* it contains as many variables as i store in it.
* java start assigning indexes from 0, meaning when i have array of size 5 data will be stored at indexes from 0 to 4 (count them it's 5).
* to refer to a variable or target a variable i have to use indexes.

* to declare an array I should use following: 
  * int[] arr;
* to make an array with specific length: 
  * int[] arr = new int[5];
* to get the length of array i use property
  *  arr.length;
* **shortcut**, i can create array and assign values to indexes at the same line.
     int[] arr = {1,2,3,4,5,6};

#### forEach:
  * it's a shorthand version of for loop.
  * it's used when i want to iterate over array.
  * for (int element : arrName){ execute code on element};
  * example :

int[] nums = {1,2,3,4,5};

for (int num : nums){ System.out.println(num); };