C OMP 110 C ONDITIONALS Instructor: Jason Carter.

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

Introduction to Programming
CS110 Programming Language I
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
C OMP 110 T YPES Instructor: Jason Carter. 2 O BJECTS VS. P RIMITIVE T YPES Instances of classes and interfaces are objects All other values are primitives.
C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Jason Carter.
By - Qiong Han. DON’T FALL BEHIND IN READING Submission Instruction Everyone needs to submit the printed HARD- COPY of the codes and the grading sheets.
Comp 114 Foundations of Programming Instructor: Prasun Dewan.
Midterm Exam 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 1-5 (except char) PDF/PS with index and corrections coming.
Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
Introduction to Computer Programming Decisions If/Else Booleans.
Main and Control Flow Programming without ObjectEditor Main Class Control Flow “Real” Programming Linear Branching Looping Programmer-Defined Overloading.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.
Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods.
More on Conditionals Miscellaneous (Side effects) Style issues Early returns.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
Midterm Exam 2 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 6-9 Look at exercises.
Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Sasa Junuzovic.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Methods Instructor: Mainak Chaudhuri
C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C OMP 110/401 D OCUMENTATION : C OMMENTS Instructor: Prasun Dewan.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
C OMP 110/401 E NCAPSULATION AND L EAST P RIVILEGE Instructor: Prasun Dewan.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
A: A: double “4” A: “34” 4.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
Instructor: Sasa Junuzovic
Java for Beginners University Greenwich Computing At School DASCO
Introduction to Computer Science / Procedural – 67130
Chapter 5: Control Structures II
CSC 253 Lecture 8.
Computers & Programming Languages
Instructor: Prasun Dewan
Review Operation Bingo
CSC 253 Lecture 8.
null, true, and false are also reserved.
Stack Memory 2 (also called Call Stack)
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
SELECTION STATEMENTS (2)
ASSIGNMENT OBJECTIVES
Scope of variables class scopeofvars {
Nate Brunelle Today: Conditional Decision Statements
PROGRAM FLOWCHART Selection Statements.
Names of variables, functions, classes
Presentation transcript:

C OMP 110 C ONDITIONALS Instructor: Jason Carter

2 M ORE ON C ONDITIONALS Miscellaneous (Side effects) Style issues Early returns

3 F OUR K INDS OF M ETHODS public void setWeight (double newVal) { weight = newVal; } public static double calculatBMI( double weight, double height) { return weight/(height*height); } public int getWeight() { return weight; } public static int readNextNumber() { System.out.println(”Next Number:"); return Keyboard.readInt(); } procedure functionpure function setWeight(70); calculateBMI(70, 1.77) …   getWeight()  70 setWeight(77)  getWeight()  77 impure function getWeight()  readNextNumber ()  5 readNextNumber ()  4 impure function with side effects

4 S IDE E FFECTS public int getWeight() { System.out.println(“get Weight called” ); return weight; } public static int readNextNumber() { System.out.println(”Next Number:"); return new Keyboard.readInt(); } public int increment() { counter++; return counter; } Effect other than computing the return value Printing something Reading input Changing global variable

5 A LTERNATIVE TO C HANGING A G LOBAL V ARIABLE public int increment() { counter++; return counter; } public void incrementCounter() { counter++; } public int getCounter() { return counter; }

6 I F -E LSE VS. B OOLEAN E XPRESSIONS public static boolean hasFailed( int score) { if (score < PASS_CUTOFF) return true; else return false; }

7 I F -E LSE S TYLE public static boolean hasFailed( int score) { return score < PASS_CUTOFF; }

8 I F -E LSE : R EDUNDANT T EST public static char toLetterGrade ( int score) { if ((score >= A_CUTOFF == true) return 'A'; else if (score >= B_CUTOFF == true) return 'B'; else if (score >= C_CUTOFF == true) return 'C'; else if (score >= D_CUTOFF == true) return 'D'; else return 'F'; }

9 N ESTED I F -E LSE public static char toLetterGrade ( int score) if ((score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return 'F'; }

10 E LSE B RANCHING

11 T HEN B RANCHING

12 T HEN B RANCHING public static char toLetterGrade ( int score) if (score >= D_CUTOFF) if (score >= C_CUTOFF) if (score >= B_CUTOFF) if (score >= A_CUTOFF) return 'A‘; else return 'B'; else // score < B_CUTOFF return 'C‘; else // score < C_CUTOFF return 'D'; else // score < D_CUTOFF return 'F'; }

13 B ALANCED B RANCHING

14 B ALANCED B RANCHING public static char toLetterGrade ( int score) { if (score >= B_CUTOFF) if (score >= A_CUTOFF) return 'A'; else return 'B'; else // score < B_CUTOFF if (score < C_CUTOFF) if (score < D_CUTOFF) return 'F'; else return 'D'; else // score >= C_CUTOFF return 'C'; }

15 N ESTED I F -E LSE S TYLE Linear branching preferable to balanced branches Else branching preferable to then branching

16 N O N ESTING public static char toLetterGrade ( int score) { char result; if (score >= A_CUTOFF) result = 'A'; if ((score = B_CUTOFF)) result = 'B'; if ((score = C_CUTOFF)) result = 'C'; if ((score = D_CUTOFF)) result = 'D'; if (score < D_CUTOFF) result = F'; return result; }

17 W HEN E LSE B RANCH IS N EEDED public static char toLetterGrade ( int score) { char result; if (score >= A_CUTOFF) result = 'A'; else if (score >= B_CUTOFF) result = 'B'; else if (score >= C_CUTOFF) result = 'C'; else if (score >= D_CUTOFF) result = 'D'; else result = F'; return result; }

18 N O N ESTING WITH E ARLY R ETURNS public static char toLetterGrade ( int score) { char result; if (score >= A_CUTOFF) return 'A'; e lse if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; return F'; }

19 E QUIVALENT S OLUTION public static char toLetterGrade ( int score) { if (score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return F'; }

20 E ARLY R ETURNS IN P ROCEDURES public void printLetterGrade(int score) { if (score < 0) { System.out.println ("Illegal score"); return ; } System.out.println(“Letter Grade:” + toLetterGrade(score”)); }

21 W ITHOUT E ARLY R ETURN IN P ROCEDURES public void printLetterGrade(int score) { if (score < 0) System.out.println ("Illegal score"); else System.out.println ( "Letter Grade: ” + toLetterGrade(score)); }

22 D ANGLING E LSE if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); } Matching if?

23 D ANGLING E LSE : M ATCHING O UTERMOST I F if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad");

24 D ANGLING E LSE : M ATCHING I NNERMOST I F if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); Correct interpretation

25 N ESTED I F -E LSE S TYLE R EMINDER Linear Branching preferable to Balanced Branches Else Branching Preferable to Then Branching Do not have dangling-else ambiguity