1 of 11

Coding Club

Eagle Ridge Middle School

Akash Pamal

Session 5

2 of 11

Today’s Talk

  • Java Libraries and While Loops
  • Using Java library APIs (Eg: Math)
  • Practice Problems

3 of 11

What is a while loop?

While (number >= 1){

Number = number/2;

Count++

}

4 of 11

Chaining - if / else

int a = 20;

if (a > 0) {System.out.println("A is a positive number");} else if (a < 0) {

System.out.println("A is a negative number");

} else {

System.out.println("I am not sure what A is");

}

5 of 11

Java Library APIs

  • Java Language provides a rich set of library routines
    • For performing standard math calculations - like square root, cube root, absolute value, trigonometric functions like sin, cos etc
    • For reading input from user - like Scanner
    • For printing results to the user - like System.out
    • And many more

6 of 11

Java Library APIs

  • The library APIs are grouped together into classes and packages
  • Before using a library API, the class or package needs to be imported into your program
    • import java.lang.Math
    • Import java.util.Scanner

7 of 11

While Loop

int a = 1;

while (as long as the condition is true) {System.out.println("Value of a is " + a);

a = a + 1;}

System.out.println("Finished the while loop”);

8 of 11

While Loop

int a = 1;

while (a < 10) {System.out.println("Value of a is " + a);

a = a + 1;�}

9 of 11

Practice Problem

  • Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number

10 of 11

Practice Problem

  • Given a num, find how many times can we divide it by 2

11 of 11

Thank You