Instructor: Sasa Junuzovic

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
C OMP 110 C ONDITIONALS Instructor: Jason Carter.
Control Flow C and Data Structures Baojian Hua
Comp 114 Foundations of Programming Instructor: Prasun Dewan.
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.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
More on Conditionals Miscellaneous (Side effects) Style issues Early returns.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)
Selection in C.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
COMP Loop Statements Yi Hong May 21, 2015.
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.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
SWE 510: Object Oriented Programming in Java
Introduction to programming in java
Flow of Control.
Chapter 5: Control Structures II
הרצאה 3 אלמנטים בסיסיים בשפה
Instructor: Prasun Dewan
null, true, and false are also reserved.
SELECTION STATEMENTS (2)
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Building Java Programs
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Self study.
ASSIGNMENT OBJECTIVES
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
2/24/2019.
CS2011 Introduction to Programming I Selections (I)
Lecture Notes – Week 2 Lecture-2
PROGRAM FLOWCHART Selection Statements.
Names of variables, functions, classes
Question 1a) What is printed by the following Java program? int s;
Control Structure.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Instructor: Sasa Junuzovic Comp 110 Conditionals Instructor: Sasa Junuzovic

Prerequisite Conditionals Loops .

More on Conditionals Miscellaneous (Side effects) Style issues Early returns

If-Else vs. Boolean Expressions public static boolean hasFailed(int score) { if (score < PASS_CUTOFF) return true; else return false; }

If-Else Style public static boolean hasFailed(int score) { return score < PASS_CUTOFF; }

If-Else: Redundant Test 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'; }

Nested If-Else 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'; }

Else Branching

Then Branching

Then Branching 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'; }

Balanced Branching

Balanced Branching 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'; return 'D'; else // score >= C_CUTOFF return 'C'; }

Nested If-Else Style Linear branching preferable to balanced branches Else branching preferable to then branching

No Nesting public static char toLetterGrade (int score) { char result = ‘I’; if (score >= A_CUTOFF) result = 'A'; if ((score < A_CUTOFF) && (score >= B_CUTOFF)) result = 'B'; if ((score < B_CUTOFF) && (score >= C_CUTOFF)) result = 'C'; if ((score < C_CUTOFF) && (score >= D_CUTOFF)) result = 'D'; if (score < D_CUTOFF) result = ‘F'; return result; }

When Else Branch is Needed public static char toLetterGrade (int score) { char result = ‘I’; 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; }

No Nesting with Early Returns 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'; return F'; }

Equivalent Solution public static char toLetterGrade (int score) { if (score >= A_CUTOFF) return 'A'; if (score >= B_CUTOFF) return 'B'; if (score >= C_CUTOFF) return 'C'; if (score >= D_CUTOFF) return 'D'; return ‘F'; }

Early Returns in Procedures public void printLetterGrade(int score) { if (score < 0) { System.out.println ("Illegal score"); return; } System.out.println(“Letter Grade:” + toLetterGrade(score”));

Without Early Return in Procedures public void printLetterGrade(int score) { if (score < 0) System.out.println ("Illegal score"); else System.out.println ( "Letter Grade: ” + toLetterGrade(score)); }

Dangling Else Matching if? if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); } Matching if?

Dangling Else: Matching Outermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad");

Dangling Else: Matching Innermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); Correct interpretation

Nested If-Else Style Reminder Linear Branching preferable to Balanced Branches Else Branching Preferable to Then Branching Do not have dangling-else ambiguity