CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

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.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
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 (!)
true (any other value but zero) false (zero) expression Statement 2
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.
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.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Computer Science Selection Structures.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Basic Of Computer Science
Flow of Control Part 1: Selection
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
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 Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
If/Else Statements.
Java Programming Fifth Edition
More on the Selection Structure
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Boolean Expressions and If
Control Structures.
Selection CSCE 121 J. Michael Moore.
CSC215 Lecture Flow Control.
Chapter#3 Structured Program Development in C++
CSC215 Lecture Control Flow.
Boolean Expressions to Make Comparisons
Structured Program Development in C++
CSC215 Lecture Control Flow.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Controlling Program Flow
Presentation transcript:

CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V

Selection Structures Conditions – result in true or false if if... else nested if switch conditional operator

Conditions something that a program checks to decide which code to run 3 parts to a "conditional expression" something being tested (operand) conditional operator (I.e. ==,!==) what it is being compared to (operand) example: (age >= 18)

Conditions - 2 conditional operators ==, !=,, >= Java always puts conditions in parentheses (weight > 10)

Combining Conditions multiple comparisons can be combined in 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 color.equals("red“) && price < 10000) both need to be true (if color.equals("red“) || price < 10000) only 1 needs to be true

Truth Tables T && T = T T && F = F F && T = F F && F = F T || T = T T || F = T F || T = T F || F = F

Practice write conditions for the following (need to make up 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, some 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 they will run if condition evaluates to true

Simple if code runs if true, skipped if false one-line if statement if (a==2) x = x + 1; two-line statement if (a==2) x = x + 1; // note ; end both statements

Simple if - 2 “block” if statement with curly braces if (a==3) { x = x + 1; // note ; y = y * 2; // note ; } //[statement(s) runs if true]

Practice write an if structure to print Good Customer if a person buys over $50,000 If (purchase >= 50000) displayResult(“Good Customer”);

if... else if (a==4) x = x * 5; else x = x * 2; // note only 1 semicolon, at // very end

if... else - 2 if (a==6) { x = x * 3; y = y * 3;} else { x = x + 1; y = y + 1;} // ; after each statement

Practice write an if structure to print Good Customer if a person 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) {x = 1; //1 st statement if b==2 y = 2; //2 nd statement } 3 possibilities: A is false, A true & B false, or both A & B are true

Limitations of if if can run 1 block of code if... else can run 2 blocks of code nested ifs can run multiple blocks of code however it gets awkward after about 3 or 4 blocks

Multiple Nested ifs if (a==1) x=1; else if (a==2) x = 4; else if (a==3) Suggest use of blocks to make it easier to read if (a==1) x=1; else if (a==2) x = 4; else if (a==3)

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 is better than if for multiple tests makes code easier to read makes code easier to maintain uses a test variable, which is compared to multiple values matching value triggers appropriate code default can be used when no value matches (optional)

switch - 2 switch (vegetableChar)//selector { case "b”: // label for broccoli caloryCount = 50; break; case "c”: // label for corn caloryCount = 111; break; default: caloryCount = 375; }// note switch selector must be of type int,boolean, or char

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

Conditional Operator shortcut for if... else bigger = (a>b) ? a : b; if condition true, variable on left gets 1 st value after ? mark if condition false, variable on left gets 2 nd value after ? mark Dr V does not like shortcuts!!

Conditional Operator - 2 if statement if (age<18) school = "high"; else school ="college"; using conditional statement instead school = (age<18)?"high":"college";