Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Flow of Control Part 1: Selection
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Learning Javascript From Mr Saem
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Repetition Statements
CompSci 230 S Programming Techniques
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
Java Language Basics.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Loop Structures.
User input We’ve seen how to use the standard output buffer
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Introduction to Programming in Java
Building Java Programs
Building Java Programs
Building Java Programs
Arrays, For loop While loop Do while loop
Looping and Repetition
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
An Introduction to Java – Part I, language basics
Java Language Basics.
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
CS 200 Loops Jim Williams, PhD.
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Control Structure Chapter 3.
Building Java Programs
Building Java Programs
Loops CGS3416 Spring 2019 Lecture 7.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Loops and Iteration CS 21a: Introduction to Computing I
Control Structure.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Looping and Repetition
Presentation transcript:

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; int num; num = 5; Both of these are equally valid 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 String str = “5”; //correct int num = “5”; //wrong 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) { //skips this code } //continues here int num = 5; if (num < 10) { //runs this code } //continues here

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

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 }

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 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 }

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."); }

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