Session One

Session Three
Review & Conditional Control Flow
Java File Hierarchy
• Projects
• Packages
• Classes
• Methods
Variables
• Everything is case-sensitive
• 2 types
• Primitive (int, float, double)
• Non-primitive (String)
• 3 parts
• Type
• Name
• Value
int num = 5;
Variable Assignment
• Every variable must has some value in order to use it
• Initialization is the first assignment
• Try to initialize it when you declare it
int num = 5;
Both of these
are equally valid
int num;
num = 5;
Declaration
First Assignment
(aka Initialization)
Variable Assignment
• Can only assign a variable it’s type of data
• Sometimes Java automatically converts stuff
• You can do it manually if you need to
String str = 5; //wrong
int num = “5”; //wrong
String str = “5”; //correct
int num = 5; //correct
New Stuff: Conditional Control Flow
• The program changes what it does depending on some condition
• if statements
int num = 5;
if (num < 10) {
//runs this code
}
//continues here
int num = 5;
if (num > 10) {
//skips this code
}
//continues here
Relational Operators
• What you use to compare two things
Operator
Use
a == b
Equal to
a != b
Not equal to
a>b
Greater than
a >= b
Greater than or equal to
a<b
Less than
a <= b
Less than or equal to
Relational Operators
• Now write a program
that uses all of these
Operator
Use
a == b
Equal to
a != b
Not equal to
a>b
Greater than
a >= b
Greater than or equal to
a<b
Less than
a <= b
Less than or equal to
public static void main(String[] args){
int a = 5;
int b = 5;
if (a == b) {
System.out.println(“==”);
}
// other if statements here
}
Conditional Control Flow
• if… else statements
• “else” does stuff if the condition is false
if (condition) {
//condition is true
} else {
//condition is false
}
//continues here
boolean Primitive Type
• boolean variables store either true or false
• Can be used in conditional control flow
boolean logic = true;
if (logic) {
System.out.println(“logic is true.”);
}
Logical Operators
• Used with boolean value types
• ! – logical NOT
• inverts boolean values
• && - logical AND
• true if both are true
• || - logical OR
• true if one is true
!true == false
!false == true
true && false == false
true || false == true
Logical Operators
b1
b2
false
false
false
b1 && b2
b1
b2
b1 || b2
false
false
false
false
true
false
false
true
true
true
false
false
true
false
true
true
true
true
true
true
true
• Make 3 int variables: a, b, and c.
• If c is between a and b, print out “Yes.”
• If not, print out “No.”
if (condition) {
//condition is true
} else {
//condition is false
}
if (a < b) {
if (c < b && c > a) {
System.out.println("Yes.”);
} else {
System.out.println("No.");
}
} else {
if (c < a && c > b) {
System.out.println("Yes.");
} else {
System.out.println("No.");
}
}
Conditional Control Flow
• “else if” statements
• A way to chain multiple if
statements
• It will execute the block of the
first true condition
• Everything continues at the end
if (condition1) {
//condition1 is true
} else if (condition2) {
//condition2 is true
} else if (condition3) {
//condition3 is true
} else {
//all conditions are false
}
//everything continues here
What’s the difference?
int a = 5;
int b = 5;
if (a == b) {
System.out.println("a==b");
} else if (a >= b) {
System.out.println("a>=b");
}
int a = 5;
int b = 5;
if (a == b) {
System.out.println("a==b");
}
if (a >= b) {
System.out.println("a>=b");
}
What’s the difference?
int a = 5;
int b = 5;
if (a == b) {
System.out.println("a==b");
} else if (a >= b) {
System.out.println("a>=b");
}
// prints out only "a==b"
// remember, else if statements won’t
// be checked if a previous condition
// was true.
int a = 5;
int b = 5;
if (a == b) {
System.out.println("a==b");
}
if (a >= b) {
System.out.println("a>=b");
}
// prints out "a==b" and "a>=b"
While Loops
• Executes a block of code over and over again until a condition is false
while (condition) {
//run this code
}
//continues here
While Loops Example
• This prints out numbers 1 – 10
• Once num == 11, the condition
is false, and it continues at the
end of the block
int num = 1;
while (num <= 10) {
System.out.println(num);
num = num + 1;
}
//continues here
Caution With Using Loops
• It’s easy to get a program stuck in an infinite loop
int num = 1;
while (num <= 10) {
System.out.println(num);
}
// won’t ever get here
End
• Mess around with Java, practice makes perfect
• This PowerPoint will be available online
For Loops
• Executes a block of code repeatedly until a particular condition is satisfied
For (initialization; termination; increment) {
//run this code
}
//continues here
For Loops Example
• This prints out numbers 1 – 10
for (int num = 1; num <= 10; num++) {
System.out.println(num);
}
//continues here
Break Statement Example
• The break statement stops a loop before the condition is satisfied.
• This prints out numbers 1 – 10
int num = 1;
for (int num = 1; num <= 10; num++) {
System.out.println(num);
if (num==5) {
break;
}
}
//continues here