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

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Conditional Statements Introduction to Computing Science and Programming I.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Programming Part 1 Armond R. Smith Zhenying Wu. Overview of this Class ● Transition from FTC -> FRC ● Using Your Resources ● Java Keywords o Data Types.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
JAVA PROGRAMMING PART II.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science Selection Structures.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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.
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.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Mixing integer and floating point numbers in an arithmetic operation.
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.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
Introduction to programming in java
Chapter 6 More Conditionals and Loops
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Building Java Programs
Building Java Programs
Building Java Programs
Outline Altering flow of control Boolean expressions
An Introduction to Java – Part I, 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.
Programming 2 Decision-Making.
Building Java Programs
Control Structure Chapter 3.
Building Java Programs
Building Java Programs
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Control Structure.
Introduction to java Part I By Shenglan Zhang.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
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