CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 4 - Control Statements
The if-else Statements
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-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 Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Computer Science Selection Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Control statements Mostafa Abdallah
Application development with Java Lecture 6 Rina Zviel-Girshin.
Decision Statements, Short- Circuit Evaluation, Errors.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Lecture 3 Selection Statements
If/Else Statements.
Selection (also known as Branching) Jumail Bin Taliba by
Lecture 3- Decision Structures
Boolean Expressions and If
Selection CSCE 121 J. Michael Moore.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
The System.exit() Method
Boolean Expressions to Make Comparisons
Chapter8: Statement-Level Control Structures April 9, 2019
Additional control structures
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010

Selection Control Structures all selection control structures use conditions (tests) types of selection control structures if if... else nested if switch conditional (ternary) operator

Condition something that a program checks or tests to decide which code to run 3 parts to a conditional or "boolean" expression something being tested (operand) relational (conditional) operator what it is being compared to (operand) age >= 18 //operands, operator?

Condition - 2 relational (conditional) operators ==, !=,, >= many languages use = instead of == in Java, must always put the whole conditional expression in parentheses (weight > 10)

boolean Expression Results boolean (conditional) expressions must always evaluate to either true or false int a, b; boolean r; a = 4; b = 3; r = (a==b); // value of result (r)? // = ? r = (a>b); // value of result? r = a / b; // is this syntax legal? template code for testing examples

Combining Conditions boolean operators can combine multiple comparisons into one conditional expression AND – requires both comparisons to be true && is used for AND in Java OR – only 1 comparison has to be true || is used for OR in Java

Combining Conditions - 2 if (weight > 50 && height > 48) // inches getsOnTheRide = "yes"; both need to be true to get on the ride at Magic Mountain if (GPA>=3.0 || activeClubMember=true) numberOfInterviews = 3; people who either have higher GPAs or are active club members get more job interviews

NOT Operator can use NOT operator (!) to reverse a boolean value boolean ok = true; boolean noGood = ! ok;

Practice if (GPA>=3.0 || activeClubMember=true) numberOfInterviews = 3; change the above code so it tests to see if both are true estimate how many interviews change code again to apply to students for whom neither condition is true # of interviews?

Truth Tables T && T == T F && F == F T && F == F F && T == F only T && T is true T || T == T F || F == F T || F == T F || T == T only F || F is false

Practice write conditions to test for the following (need to name variables for values) people 33 years old people over 40 years old with 2 children pets that are brown dogs paper that is any size except 8.5" x 11" some members of a team are over 5 foot tall, but they weigh less than 120 pounds

Selection (Branching) - if always based on a condition (test) condition must evaluate to true or false (a == b) (a > b && c < d) 1 or more statements immediately follow the condition need curly braces if there is more than 1 statement statement(s) will run only if condition evaluates to true

Simple if code runs when true, otherwise skipped one-line if structure (two statements) if (a == 2) x = x + 1; two lines, two statements if (a == 2) x++; //same as x = x + 1 //note that ; ends statements, not lines

Simple if - 2 “block” if statement with curly braces if (a==3) { x = x + 1; // note semicolon y = y * 2; // note semicolon } // both statements run if a equals 3

Practice write an if control structure to print "Good Customer" for persons who buy over $50,000 write an if control structure to print "Kid discount" and also add one to the count of these admissions for children who are under 6 years old

if... else if (a == 4) // word if used once by itself System.out.println("x is 4"); else if (b==2) // 0 to many else ifs System.out.println("b is 2, x isnt 4"); else // 0 or 1 else without if System.out.println("b isnt 2, x isnt 4"); // note semicolons after each print statement

if... else With Block(s) if (a == 6) { x = x * 3; //curly braces for block y = y * 3;} //end of 1 st block else { x = x + 1;//start of 2 nd block y = y + 1;} //end of 2 nd block // semicolons after each statement

Practice write an if structure to print "Good Customer" for a person who buys over $50,000, and prints "OK Customer" for other customers whose sales are over $10,000

"Nested" if can put if (if... else) inside another if if (a == 1) // 1 st if statement {x = 1; if b == 2 // 2 nd if statement y = 2; // end of 2 nd if } // end of 1 st if a=b=1;a=b=2;a=1,b=2? x,y for each?

Limitations of if structures if can run 1 block of code if... else (or if … else if) can run 2 blocks of code can run multiple blocks of code with multiple else ifs and/or nested ifs however it gets awkward after about 3 or 4 additional blocks

Layouts: Multiple if Structures if (a==1) x=1; else if (a==2) x = 4; else if (a==3) x = 9; else // default x = 16; if (a==1) x=1; else if (a==2) x = 4; else if (a==3) x = 9; else // default x = 16; // same, in less lines

Practice write an if structure to print "Good Customer" if a person buys over $50,000 prints "OK Customer" for other customers whose sales are over $10,000 prints "Needs Follow Up" for the rest of the customers

Selection (Branching) - switch switch better than if for multiple tests makes code easier to read (and write) makes code easier to maintain compares integer test value (number, variable, or expression) to multiple values matching value triggers appropriate code default can be used when no value matches (this is optional)

switch Example switch (number) { case 0: comment = "You are nada … … zip!"; break; case 1: comment = "You are the only one!"; break; default: comment = "You are something else!"; break;} // break optional here

break in a switch Statement break statement stops execution of switch after a true condition is found case 0: comment = "You are nada, zip!"; break; put most likely conditions at top to reduce number of tests (optimize code) break after default could make code safer to maintain e.g., adding more conditions

switch … break - 2 can omit some break statements to handle multiple conditions code "falls through": runs all statements after true condition before the break "12 Days of Christmas" (video) (karaoke) "12 Days of Christmas" videokaraoke could also put if code inside a switch see days in month example (SwitchDemo2 halfway down page)example

Practice write a switch structure to print "Outstanding Customer" if a person buys over $100,000, "Good Customer" if a person buys over $50,000, "OK Customer" for other customers whose sales are over $10,000, and "Needs Follow Up" for the rest of the customers

Conditional Operator question mark (?) and colon (:) used in a one-line if … else statement that assigns a value also known as the ternary operator result = condition ? value1 : value2; boolean x = 3>2; int larger = x ? 3 : 2;

Review Name 3 different types of selection structures What is a condition? Give an example of a condition What are relational operators? Give examples of relational operators

Review - 2 What 2 values can boolean variables hold? What are the boolean operators for combining conditions? How many conditions can be combined by boolean operators? (trick question?)

Review - 3 What is a truth table? What are the boolean value of the following conditions? T || F T && F ! T

Review - 4 Provide sample code for: Simple if if … else if … else if … else How many else ifs can be in a control structure? How many elses can be in a control structure?