Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
DECISION MAKING STRUCTURES Computer Programming 2.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Decision Making Selection structures (if....else and switch/case)
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
JDBC Tutorial CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Applications Development
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Control Statements: Part1  if, if…else, switch 1.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 4: Control Structures I
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4: Control Structures I
ECE Application Programming
Chapter 6 More Conditionals and Loops
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Expressions and Control Flow in JavaScript
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)
Chapter 4: Control Structures I
More Selections BIS1523 – Lecture 9.
Selection (if-then-else)
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
The switch statement: an alternative to writing a lot of conditionals
AP Java Review If else.
SELECTIONS STATEMENTS
AP Java Review If else.
Fundamental Programming
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
HNDIT11034 More Operators.
Presentation transcript:

Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/11 ▪C▪Conditions ▪P▪Program

Conditional Statements ▪Java has three conditional statements, similar to C++ ›if-else ›switch-case ›Conditional ternary operator ?: ▪The conditional and boolean operators work similar to C++ as well USC CSCI 201L3/11 Conditions

Comparison and Boolean Operators ▪Comparison operators ==!= >>= <<= ›NOTE: Comparison operators only work for primitive data types. Methods will be created for object comparisons ▪Boolean operators Bitwise boolean operators & | USC CSCI 201L4/11 Conditions Logical boolean operators && || !

if Statements 1 int num=1; 2 if (num < 1) { 3 4 } 5 else if (num > 1) { 6 7 } 8 else { 9 10 } USC CSCI 201L5/11 Conditions 1 int num=1; 2 if (num < 1) { 3 4 } 5 if (num > 1) { 6 7 } 8 if (num == 1) { 9 10 } What is the difference between these two code snippets?

switch Statements ▪switch statements can only take a byte, short, int, or char ▪The values in the cases must be hard-coded (no variables) ›The conditions must match equality ▪You can have a default case if you want ▪Once one case becomes true, the rest of the cases will not be checked 1switch (grade) { 2case ‘A’: 3System.out.print (“Excellent”); 4case ‘B’: 5System.out.print (“Good”); 6break; 7case ‘C’: 8System.out.print (“Eh”); 9case ‘D’: 10System.out.print (“I guess”); 11break; 12case ‘F’: 13 System.out.print (“Failed”); 14} USC CSCI 201L6/11 Conditions

Multi-Condition switch Statement 1switch (grade) { 2case ‘A’: 3case ‘B’: 4case ‘C’: 5case ‘D’: 6 System.out.print (“Passed”); 7break; 8case ‘F’: 9default: 10 System.out.print (“Failed”); 11} USC CSCI 201L7/11 Conditions

switch vs if ▪It does not make a difference if you use an if statement or a switch statement ›There is no change in efficiency ▪Every switch statement can be written as an if statement, but not vice versa – why not? USC CSCI 201L8/11 Conditions

Conditional Ternary Operator ▪There is only one ternary operator in Java (and C++) ▪The conditional ternary operator can assign a value based on the result of a condition 1 int num = 2; 2 String status; 3 if (num < 3) { 4 status = “low”; 5 } 6 else { 7 status = “high” 8 } 9 10 status = (num < 3) ? “low” : “high”; USC CSCI 201L9/11 Conditions

Outline USC CSCI 201L10/11 ▪C▪Conditions ▪P▪Program

Program ▪Write a program to prompt the user for two numbers and takes one parameter off the command line to represent the operation he would like to perform with the two numbers. Perform that operation. Use a case statement to determine the operation. A sample execution is below. USC CSCI 201L11/11 Program c:\>java csci201.Operation / Please enter first number: 21 Please enter second number: 3 21 / 3 = 7 c:\>