Download presentation
Presentation is loading. Please wait.
Published byNickolas Nelson Modified over 8 years ago
1
Session Three Review & Conditional Control Flow
2
Java File Hierarchy Projects Packages Classes Methods
3
Variables Everything is case-sensitive 2 types Primitive (int, float, double) Non-primitive (String) 3 parts Type Name Value int num = 5;
4
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; int num; num = 5; Both of these are equally valid Declaration First Assignment (aka Initialization)
5
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 String str = “5”; //correct int num = “5”; //wrong int num = 5; //correct
6
New Stuff: Conditional Control Flow The program changes what it does depending on some condition if statements int num = 5; if (num > 10) { //skips this code } //continues here int num = 5; if (num < 10) { //runs this code } //continues here
7
Relational Operators What you use to compare two things OperatorUse a == bEqual to a != bNot equal to a > bGreater than a >= bGreater than or equal to a < bLess than a <= bLess than or equal to
8
Relational Operators Now write a program that uses all of these OperatorUse a == bEqual to a != bNot equal to a > bGreater than a >= bGreater than or equal to a < bLess than a <= bLess 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 }
9
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
10
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.”); }
11
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
12
Logical Operators b1b2b1 && b2 false truefalse truefalse true b1b2b1 || b2 false true falsetrue 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 }
13
if (a < b) { if (c a) { System.out.println("Yes.”); } else { System.out.println("No."); } } else { if (c b) { System.out.println("Yes."); } else { System.out.println("No."); }
14
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
15
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"); }
16
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"
17
While Loops Executes a block of code over and over again until a condition is false while (condition) { //run this code } //continues here
18
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
19
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
20
End Mess around with Java, practice makes perfect This PowerPoint will be available online
21
For Loops Executes a block of code repeatedly until a particular condition is satisfied For (initialization; termination; increment) { //run this code } //continues here
22
For Loops Example This prints out numbers 1 – 10 for (int num = 1; num <= 10; num++) { System.out.println(num); } //continues here
23
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.