View on GitHub

reads

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

Java Basics

Variables

This section discusses this relationship

variables kinds in java.

Datatypes

Expressions, Statements, and Blocks

Expressions statement

int sumResult = sum({1,2,3,4,5}); // whatever happen it will evaluate to single value.

  * example 3: operators or logical operators:
    * `boolean result = true || false; // will evaulate to single value.`
    * `boolean result = 3>5;`;

* **Note: Control Flow Statements** for loops and if statement will not evaluate to single statement so it's not expression statement.

### Statements
* statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

### Blocks
 a 0 or more statements grouped together, lives between curly braces.
 * for example: **classes are blocks of code**, **Methods are blocks of code**.

## Control Flow Statements
* the normal flow of the program is: from top to bottm, left to right.
  * top to bottm: the interpreter will read code line by line in order from top to bottm.
  * left to right: in each line currently interpreter reading, it will read from left to right.
* **however conditionals can break and control and modify the normal flow of the program, how?**
  * I have conditional statement `if(){ code here..}`, the code will not be executed,
     unless the condition provided is true.
  * if condition is false then program will skip the lines of code. 
* java uses booleans to evaluate conditional statements.
* true or false are the returning result, the code will be executed.
* example:

int x= 5; int y= 5; if(x == y){ System.out.println(‘they are equal’); }

* since they are equal the result will evaluate to true, therefore the code will be executed.
if x = 5 and y=6 then the result will be false thus code will not be executed.

###  decision statements
it tell the program what should do if the statement evaluate to true or false.
#### if statements, else and if else
* **if** block, will be executed when condition provided evaluate to true.
* **else** block is executed when program evaluate to false.

if(true){ execute code here…. }else{ execute code if condition is not true.
}

* else if, when I expect more than one condition to be true but each one execute it's own block, look at example:

int testscore = 76; char grade;

    if (testscore >= 90) {
        grade = 'A';
    } else if (testscore >= 80) {
        grade = 'B';
    } else if (testscore >= 70) {
        grade = 'C';
    } else if (testscore >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }
    System.out.println("Grade = " + grade);
} } ``` * **if - else** statements doesn't have to be in several lines with {}, if can be used in one line, or without the {}, for a single line statment: 1. if else without {}: ```   if (a == b)
  System.out.println("Another line Wow!");   else
  System.out.println("Double rainbow!");
2. a single line statment:

a ==b? executed code if true(if part) : executed code if false(else part).

### loop statements
* loop statement allow me to run piece of code more than once.
* There are 2 types of loops for loops and while loops.

#### for loops:
* i use for loop when i know the end of the loop (or at least i can the program), for example i give user 5 attempts to enter correct password.
* syntax for(int i= 0; i<5; i++){ code statements here....}; 
    1. first part: is the loop start and current iterate number, and it will always compiler read it first.
    2. second part: as long as the comprasion operator evaluate to true the code inside braces will run, and it get's executed 2nd.
    3. third part: when the current round is finished this block get executed.

* **important note about for loop:** first part and second part will be executed before the loop start,
   and third part will be executed after the round finished.

#### 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); };

#### 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 }; }; ```

# Reddit thread on compiling