Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Similar documents
Making Decisions Chp. 5

C++ Programming: From Problem Analysis to Program Design, Third Edition

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Repeating Instructions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Lecture 5 Tao Wang 1

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Decision Making in C

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

204111: Computer and Programming

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Chapter 4: Control Structures I (Selection)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Chapter 2: Using Data

Introduction to C# Applications

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Flow Control. CSC215 Lecture

Chapter 4 - Notes Control Structures I (Selection)

LECTURE 04 MAKING DECISIONS

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

CIS 3260 Intro to Programming in C#

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Following is the general form of a typical decision making structure found in most of the programming languages:

Language Reference Manual

BASIC ELEMENTS OF A COMPUTER PROGRAM

5. Selection: If and Switch Controls

Chapter 4: Control Structures I

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Conditional Statement

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Handout 4 Conditionals. Boolean Expressions.

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Java Bytecode (binary file)

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Software Design & Programming I

Boolean evaluation and if statements. Making decisions in programs

Introduction to Bioinformatics

Introduction to Object-Oriented Programming

Control Structures in Java if-else and switch

Selection Statements. Pseudocode

IT 374 C# and Applications/ IT695 C# Data Structures

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Oct Decision Structures cont d

Control Structures in Java if-else and switch

Computer Programming : C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

BRANCHING if-else statements

The Warhol Language Reference Manual

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

egrapher Language Reference Manual

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

In this lab, you will learn more about selection statements. You will get familiar to

Typescript on LLVM Language Reference Manual

Chapter Two MULTIPLE CHOICE

1 Lexical Considerations

Chapter 3. Selections

UEE1302(1102) F10: Introduction to Computers and Programming

The SPL Programming Language Reference Manual

Language Proposal: tail

CS 106 Introduction to Computer Science I

Advanced Computer Programming

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 2: Functions and Control Structures

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Selections. CSE 114, Computer Science 1 Stony Brook University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3 Selection Statements

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Transcription:

5 ec Making Decisions s C# Programming: From Problem Analysis to Program Design 2nd Edition David McDonald, Ph.D. Director of Emerging Technologies Chapter Objectives Learn about conditional expressions that return Boolean results and those that use the bool data type Examine equality, relational, and logical operators used with conditional expressions Write if selection type statements to include one-way, two-way, and nested forms 1

Chapter Objectives (continued) Learn about and write switch statements Learn how to use the ternary operator to write selection statements Revisit operator precedence and explore the order of operations Basic Programming Constructs Simple sequence Selection statement If statement Switch Iteration Looping 2

Making Decisions Central to both selection and iteration constructs Enables deviation from sequential path in program Involves conditional expression The test Produces Boolean result Boolean Results and Bool Data Types Boolean flags Declare Boolean variable bool identifier; Initialize to true or false Use to determine which statement(s) to perform Example bool moredata = true; : // Other statement(s) that might change the : // value of moredata to false. if (moredata) // Execute statement(s) following the if // when moredata is true 3

Conditional Expressions Appear inside parentheses Expression may be a simple Boolean identifier if (moredata) Two operands required when equality or relational symbols are used Equality operator two equal symbols (==) Inequality operator NOT equal (!=) Relational operator (<, >, <=, >=) Equality and Relational Operators 4

Conditional Expression Examples int avalue = 100, bvalue = 1000; string svalue = CS158 ; decimal money = 50.22m; double dvalue = 50.22; int avalue = 100; char cvalue = A ; decimal money = 50.22m; double dvalue = 50.22; 5

Logical Operators Short-Circuit Evaluation Short-circuiting logical operators && and OR ( ) expressions if the first evaluates as true, no need to evaluate the second operand AND (&&) expressions if the first evaluates as false, no need to evaluate second operand C# also includes the & and operators logical does not perform short-circuit evaluation 6

if...else Selection Statements Classified as one-way, two-way, or nested Alternate paths based on result of conditional expression Expression must be enclosed in parentheses Produce a Boolean result One-way When expression evaluates to false, statement following expression is skipped or bypassed No special statement(s) is included for the false result One-way Selection Statement if (expression) statement; } No semicolon placed at end of expression Null statement Curly braces required with multiple statements Figure 5-1 One-way if statement 7

One-way if Selection Statement Example /* BonusCalculator.cs Author: McDonald */ using System; namespace BonusApp class BonusCalculator static void Main( ) string invalue; decimal salesforyear, bonusamount = 0M; Console.WriteLine("Do you get a bonus this year?"); Console.WriteLine( ); Console.WriteLine("To determine if you are due one, "); Console.Write("enter your gross sales figure: "); invalue = Console.ReadLine(); salesforyear = Convert.ToDecimal(inValue); if (salesforyear > 500000.00M) Console.WriteLine( ); Console.WriteLine( YES...you get a bonus! ); bonusamount = 1000.00M; } Console.WriteLine( Bonus for the year: 0:C}, bonusamount); Console.ReadLine( (); } // end of Main( ) method } // end of class BonusCalculator } // end of BonusApp namespace 8

Output from BonusCalculator Figure 5-2 BonusApp with salesforyear equal to 600,000.00 000 Figure 5-3 BonusApp with salesforyear equal to 500,000.00 Two-way Selection Statement Either the true statement(s) executed or the false statement(s), but not both No need to repeat expression else portion Figure 5-5 Two-way if statement 9

Two-way Selection Statement (continued) if (expression) } else } statement; statement; Two-way if else Selection Statement Example if (hoursworked > 40) payamount = (hoursworked 40) * payrate * 1.5 + payrate * 40; Console.WriteLine( You worked 0} hours overtime., hoursworked 40); } else payamount = hoursworked * payrate; Console.WriteLine( Displayed, whether the expression evaluates + true or false ); 10

Nested if else Statement Acceptable to write an if within an if When block is completed, all remaining conditional expressions are skipped or bypassed Syntax for nested if else follows that of twoway Difference: With a nested if else, the statement may be another if statement t t No restrictions on the depth of nesting Limitation comes in the form of whether you and others can read and follow your code Nested if else Selection Statement Example bool hourlyemployee; double hours, bonus; int yearsemployed; if (hourlyemployee) if (hours > 40) bonus = 500; else bonus = 100; else if (yearsemployed > 10) bonus = 300; else bonus = 200; Bonus is assigned 100 when hourlyemployee == true AND hours is less than or equal to 40 11

Matching up Else and If Clauses if (avalue > 10) // Line 1 if (bvalue == 0) // Line 2 amount = 5; // Line 3else goes with the closest else // Line previous 4 if that does not if (cvalue > 100) // Line 5have its own else if (dvalue > 100) // Line 6 amount = 10; //Line 7 else // Line 8 amount = 15; // Line 9 else // Line 10 amount = 20; // Line 11 else // Line 12 if (evalue == 0) // Line 13 amount = 25; // Line 14 Switch Selection Statements Multiple selection structure Also called case statement Works for tests of equality only Single variable or expression tested Must evaluate to an integral or string value Requires the break for any case No fall-through available 24 12

Switch Statements General Form switch (expression) Selector } case value1: statement(s);... break; case valuen: statement(s); [default: break; statement(s); Optional break;] Value must be a of the same type as selector Switch Statement Example /* StatePicker.cs Author: McDonald */ using System; namespace StatePicker class StatePicker static void Main( ) string stateabbrev; Console.WriteLine( Enter the state abbreviation. ); Console.WriteLine( Its full name will be displayed ); Console.WriteLine( ); stateabbrev = Console.ReadLine( ); 13

switch(stateabbrev) case "AL": Console.WriteLine( Alabama ); break; case "FL": Console.WriteLine( Florida ); break; : // More states included case "TX": Console.WriteLine( Texas ); break; default: Console.WriteLine( No match ); break; } // End switch } // End Main( ) } // End class } // End namespace Switch Statements Associate same executable with more than one case Example (creates a logical OR) case "AL": case "al": case "Al": case "al": Console.WriteLine( Alabama ); break; Cannot test for a range of values 14

Switch Statements (continued) Case value must be a constant literal Cannot be a variable int score, high = 90; switch (score) case high : // Syntax error. Case value must be a constant // Can write case 90: but not case high: Value must be a compatible type char value enclosed in single quote string value enclosed in double quotes Ternary Operator? : Also called conditional operator General form expression1? expression2 : expression3; When expression1 evaluates to true, expression2 is executed When expression1 evaluates to false, expression3 is executed Example grade = examscore > 89? A : C ; 15

Order of Operations Order of Operations (continued) Precedence of the operators Associativity Left-associative All binary operators except assignment operators Right-associative Assignment operators and the conditional operator? Operations are performed from right to left Order changed through use of parentheses 16