> For the complete documentation index, see [llms.txt](https://nissan-goldberg.gitbook.io/java101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nissan-goldberg.gitbook.io/java101/lesson-2/untitled.md).

# Lesson 2 - While

Todays lesson

* More Conditionals - `if` and `else`
* `while` , `break` and `do-while`

## שאלת חימום

מצא את המקסימום בין 4 מספרים

```java
public class Main {
    public static void main(String[] args) {
        int a = 1, b=71, c=3, d= 10, max;

        max = (a >b) ? a : b;
        max = Math.max(max, c); //same thing
        max = (max >d) ? max : d;

        System.out.println(max);
    }
}
```

```
71
```

## כמה ספרות וכמה אפסים

קלוט מספר n

* הדפס מספר הספרות
* הדפס מספר האפסים

```java
public class Main {
    public static void main(String[] args) {

        int num = MyConsole.readInt("enter a number: ");
        System.out.println("You entered a " + (num >= 0 ? "positive" : "negative") +" number");

        int countZeros=0;
        int countDigits=0;
        int absNum = num>=0 ? num : -num;

        if (num==0){
            countZeros=1;
            countDigits=1;
        }

        while (absNum > 0) {
            if (absNum%10==0)
                countZeros++;
            absNum /= 10;
            countDigits++;
        }
        System.out.println("number of digits: " + countDigits);
        System.out.println("number of zeros: " + countZeros);
    }
}
```

```
enter a number:  -30012
You entered a negative number
number of digits: 5
number of zeros: 2
```

זה if מקוצר

```java
 int absNum = num>=0 ? num : -num;
```

![](https://i.ibb.co/GJTgFyL/ternary-conditional-operator.png)

### version 2

let `num` be a random number from `-1000` until `9999` (including)

```java
        int max = 10_000, min = -1_000;
        int num = (int) ((Math.random() * (max - min)) + min); //random number from -1,000 to 9,999 (including)
        System.out.println("Random num:" + num);
```

```
Random num:6318
You entered a positive number
number of digits: 4
number of zeros: 0
```

> Note: it includes the min but not the max \[min, max)

## Rotate Square

![](https://i.ibb.co/gt5vQbR/cube-shifter-easy.png)

יש לסובב את הקוביה

```java
public class Main {
    public static void main(String[] args) {
         int a = 1, b =2, c=3, d=4, temp;

         temp = d;
         d = c;
         c = b;
         b = a;
         a = temp;

        System.out.println("a: " + a + "\nb: " + b + "\nc: " + c + "\nd: " + d);
    }
}
```

```
a: 4
b: 1
c: 2
d: 3
```

#### טבלת מעקב

![track-table](https://i.ibb.co/bzs5kZK/track-table.png)

### Version 2 : n right or left shifts

![](https://i.ibb.co/t4v0v6M/cube-shifter.png)

קלוט מספר n:

* אם הוא חיובי אז סובב n פעמים ימינה
* אם הוא שלילי אז סובב n פעמים שמאלה

```java
public class Main {
    public static void main(String[] args) {
         int a = 1, b =2, c=3, d=4, temp;
         boolean isRightShift=true;

         int n_shifts = MyConsole.readInt("enter:\n* posivite num for right shift" +
                 "\n* negative for left shift\ninput :");
         if (n_shifts<0) {
             isRightShift = false;
             n_shifts = n_shifts * -1; //since while works on positive number, otherwise we would need 2 different whiles
             System.out.println("Left shifting " + n_shifts +" times");
         } else if (n_shifts==0)
             System.out.println("No shifts");
          else
             System.out.println("Right shifting " + n_shifts +" times");


         while (n_shifts>0){
             if (isRightShift) {
                 temp = d;
                 d = c;
                 c = b;
                 b = a;
                 a = temp;
                 n_shifts = n_shifts-1;
                 System.out.println("a: " + a + "\tb: " + b + "\tc: " + c + "\td: " + d);
             }else {
                 temp = a;
                 a = b;
                 b = c;
                 c = d;
                 d = temp;
                 n_shifts = n_shifts-1;
                 System.out.println("a: " + a + "\tb: " + b + "\tc: " + c + "\td: " + d);
             }
         }
    }
}
```

```
enter:
* posivite num for right shift
* negative for left shift
input : -2
Left shifting 2 times
a: 2    b: 3    c: 4    d: 1
a: 3    b: 4    c: 1    d: 2
```

#### note

since `while (n_shifts>0)` checks for positive numbers and we do `n_shifts = n_shifts-1;` inside the while loop without `n_shifts = n_shifts * -1` if we have a left shift we would need **another** ***while*** loop

## Find Aprox. log2

מצא עבור קלט x את:

$$
log\_2x=answer
$$

קודם כל בא נבין מה קורה כאן

$$
2^3=2*2*2=8
$$

$$
log\_28=3
$$

אם הוא לא חזקה שלמה הדפס את חזקה שלמה ועוד .x

```java
public class Main {
    public static void main(String[] args) {
        float x = (float)MyConsole.readDouble("enter positive num: ");

        while (x<0.)
            x = (float)MyConsole.readDouble("Try again\nPlease enter a positive num: ");

//        float x = 1<<10;
//        float x = 1_024f;
        float original = x;
        int count = 0;
        boolean isPowOf2 = false;

        while(x > 2) {
            x = x / 2;
            count +=1;
            if (x==2.){
                System.out.println("log_2("+original+")="+(count+1));
                isPowOf2 = true;
            }
        }
        if (isPowOf2==false)
            System.out.println("log_2("+original+")="+count + ".x");
    }
}
```

```
enter num:  1024
log_2(1024.0)=10
```

2nd run

```
enter num:  1025
log_2(1025.0)=10.x
```

> To find the real answer we should use a Talyor series.

## Find Parallel lines

מצא האם 2 ישרים:

* מקבילים
* מאונכים
* או לזה זה ולא זה

![](https://dr282zn36sxxg.cloudfront.net/datastreams/f-d%3A3ceeb0ba9414cc883c09344f3559186ae97bd2434e1d67cdfe4483d5%2BIMAGE_TINY%2BIMAGE_TINY.1)

[img source](https://www.ck12.org/c/geometry/parallel-lines-in-the-coordinate-plane/lesson/Parallel-Lines-in-the-Coordinate-Plane-BSC-GEOM/)

![Find The Slope Of A Line That Passess Through 2 Points - YouTube](https://i.ytimg.com/vi/KC3Eh8ZH3F0/maxresdefault.jpg)

[img source](https://www.google.com/url?sa=i\&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKC3Eh8ZH3F0\&psig=AOvVaw3g34ALI2noOZ2DNL3ck-29\&ust=1615891581323000\&source=images\&cd=vfe\&ved=0CA0QjhxqFwoTCMDGurCPsu8CFQAAAAAdAAAAABAc)

![](https://i.ibb.co/SVgzZVN/slopes.png)

```java
public class Main {
    public static void main(String[] args) {
        //parallel - 2 greens
//        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
//        int l2_x1 = 3, l2_y1 = 0, l2_x2 =0, l2_y2=-2;

        //perpendicular - green and orange
//        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
//        int l2_x1 = 0, l2_y1 = 0, l2_x2 =-2, l2_y2=3;

        //neither - green and purple
        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
        int l2_x1 = 0, l2_y1 = 0, l2_x2 =1, l2_y2=3;

        double Δ_l1_y = l1_y1-l1_y2, Δ_l1_x = l1_x1-l1_x2;
        double Δ_l2_y = l2_y1-l2_y2, Δ_l2_x = l2_x1-l2_x2;

        double slope_l1 = Δ_l1_y/Δ_l1_x;
        double slope_l2 = Δ_l2_y/Δ_l2_x;

        if (slope_l1==slope_l2)
            System.out.println("The lines are parallel\n"
            + "slope is:" + slope_l1);
        else if((-1*Δ_l1_x)/Δ_l1_y==slope_l2)
            System.out.println("The lines are perpendicular\n"
                    + "* slope of line 1 :" + slope_l1
                    + "\n* slope of line 2 :" + slope_l2);
        else
            System.out.println("lines are neither:" +
                    "\n* parallel\n* perpendicular"
                    + "\n\t* slope of line 1 :" + slope_l1
                    + "\n\t* slope of line 2 :" + slope_l2);
    }
}
```

```
lines are neither:
* parallel
* perpendicular
    * slope of line 1 :0.6666666666666666
    * slope of line 2 :3.0
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nissan-goldberg.gitbook.io/java101/lesson-2/untitled.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
