Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can...

19
Java Day 3 Conditional Statements and Loops

Transcript of Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can...

Page 1: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Java Day 3Conditional Statements and Loops

Page 2: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Relational and Equality operators

Gives a boolean (true or false)

x > y is x greater than y?

x < y is x less than y?

x >= y is x greater than or equal to y?

x <= y is x less than or equal to y?

x == y is x equal to y?

x != y is x not equal to y?

Page 3: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

The !

! means “not”. It inverts whatever you put it before.

Ex) !true is the same as false

Ex) !false is the same as true

!(x == y) is the same as x != y

Page 4: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

== vs =

== is an EQUALITY operator. It gives a true or a false

= is an ASSIGNMENT operator. It assigns whatever is on the right to

whatever is on the left.

Ex) x = 5; x now means 5.

Ex) x == 5; this line means either true or false.

When comparing objects with ==, it compares the location of the

objects in the computer’s memory, not the actual objects.

Don’t use == when comparing objects!

Page 5: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

equals() and equalsIgnoreCase()

equals() is a method for objects in java. When used with Strings, it

checks if two strings are the same set of letters (but it is case

sensitive!)

Ex) “Hello World!”.equals(“Hello World!”); //returns true

Ex) “hello world!”.equals(“Hello World!”); //returns false

equalsIgnoreCase() works the same way as equals(), but it doesn’t pay

attention to the case (hence the “IgnoreCase” part)

Ex) “hello world!”.equals(“Hello World!”); //returns true

Page 6: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

compareTo()

compareTo() is a method that can be used to compare strings. For

instance, if you wanted to know if the word “hello” comes before the

word “world” alphabetically, you would use it like this:

“hello”.compareTo(“world”); or “world”.compareTo(“hello”);

If the first string comes first in the alphabet, the method returns a

negative number. If the second string comes first in the alphabet, it

returns a positive number. If they are the same, it returns 0.

Page 7: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Ifs and Elses

If statements will only run a designated chunk of code if the conditional

part of the if statement is true.

Ex) if (x >= 10) {

System.out.println(“X is bigger than 10!”);

}

The part in the braces will only run if the first part (x >= 10) is true. If it isn’t

true, java will skip the part in the braces.

Page 8: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Ifs and Elses

Elses will only run if the if statement before it is false. There is only one

else in any if-else chunk of code.

Ex)

x = 5;

if (x >= 10) { /*whatever code*/ }

else {

x = 15;

}

Page 9: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Ifs and Elses

Since the x >= 10 was not true, java skipped the if statement’s code

and only ran the else’s code. If x had been, say, 15, java would have

only ran the if’s code and skipped the else’s code.

Page 10: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Else Ifs

Else ifs come after an if, but before the else. You can have as many

else ifs as you need. They are essentially the same thing as nesting an

if inside of an else:

if(x > 10) { blah }

else {

if(x > 5) {

}

}

if (x > 10) { blah }

else if ( x > 5) {

}

Is the same as:

Page 11: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Switches

Switches are are just a bunch of little if statements crammed together

into one big chunk. However, switches can only check the values of

ONE VARIABLE, and only if that one variable EQUALS various values.

The basic setup has the switch keyword, the variable, several case

statements following this, a default case, the break keyword, and the

code chunks for each case.

SWITCHES ARE THE ONLY TIME WE CAN USE THE BREAK KEYWORD!!

DON’T USE IT ANYWHERE ELSE!

Page 12: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Switches

Ex)

switch (variable) {

case 1:

//some

code

break;

default:

//more code

}

At each case statement, java checks if the variable

is equal to the value after the case keyword. If it is,

java does all of the code after that case keyword.

Unfortunately, this also means that java does all of

the code in all of the other cases after that case.

This is why we have the break keyword. It stops java

from doing all of the code. The default case is

essentially the same as an else. It catches all of the

rest of the possible values. It doesn’t need the break

because it is the last case.

Page 13: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

While Loops

While loops repeat a given chunk of code until a given conditional

statement becomes false:

while (x % 10 != 0) {

x = x + y;

y++;

}

Generally you use while loops when the number of iterations (times the

code chunk is run) is unclear. This loop runs until x is divisible by 10.

Page 14: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

For Loops

For loops will also loop through a chunk of code until a given

conditional statement is false, but they are generally used when you

know how many iterations there are. This is the basic setup:

for(int i = 0; i < 10; i++) {

//some code

}

Page 15: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

For Loops

The first part of the for loop (int i = 0) is the starting value of the variable

the loop will use to count iterations. So for this, the for loop will start

with i being 0 and go up or down from there.

The second part of the for loop is the conditional statement. This tells

the for loop when to stop looping. The for loop will continue until this

statement is no longer true.

The third part of the loop changes the variable the loop uses (generally

from the first part). Generally, the first, second, and third part of the

loop all use the same variable, but they don’t always. For example...

Page 16: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

For Loops

...A for loop can look like this:

for(int x = 0; y <10; z++) {

}

But that doesn’t come up too often, and if it does, there’s probably

some really funky math going on inside of the loop.

Page 17: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Taking User Input with the Scanner Class

A Scanner is an object in java that can take input from the user. To

create a Scanner:

Scanner input = new Scanner(System.in);

This creates a new scanner called input. When you want to take input

with the Scanner, type:

String userInput = input.nextLine(); (or .nextInt() for an int or

.nextChar() for a char)

.nextLine() gets the next string input by the user. It returns what the

user inputs, so you have to put it in a variable to use it.

Page 18: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

The Forbidden Break keyword.

The break keyword tells java to get out of a loop right then and there. It

is quick and easy, but also lazy. It is better to just adjust your

conditional statement. We WILL NOT use this keyword anywhere

except for switches. It is one of the big no-nos of programming.

Page 19: Java Day 3 - RMR 662 Programming Team · 2018. 9. 6. · A Scanner is an object in java that can take input from the user. To create a Scanner: Scanner input = new Scanner(System.in);

Program:

Create a program that:

• Prompts the user to input a number

• Takes a number input from the user until the user inputs a number

divisible by a number you choose between 1 and 10.