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

Similar documents
BRANCHING if-else statements

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

Control Structures: The IF statement!

5. Selection: If and Switch Controls

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

Program Control Flow

Program Control Flow

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

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

CIS 110: Introduction to Computer Programming

Conditional Expressions

Conditional Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators & Expressions

Logic Design: Part 2

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

Review. Relational Operators. The if Statement. CS 151 Review #4

Making Decisions Chp. 5

Chapter 2: Using Data

If Control Construct

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Introduction to C# Applications

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

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

Chapter 2. C++ Basics

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

JAVA OPERATORS GENERAL

Information Science 1

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Chapter 6 Primitive types

Topics. Chapter 5. Equality Operators

conditional statements

b. Suppose you enter input from the console, when you run the program. What is the output?

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

TOPIC 8 MORE ON WHILE LOOPS

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

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

CA4003 Compiler Construction Assignment Language Definition

CMSC201 Computer Science I for Majors

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

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Lecture 1. Types, Expressions, & Variables

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

QUIZ: What value is stored in a after this

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Algorithms and Conditionals

Flow Control. So Far: Writing simple statements that get executed one after another.

1.3b Type Conversion

Conditionals and Loops

Name: -1 and and and and

Outline. Data and Operations. Data Types. Integral Types

Logical operators 20/09/2018. The unit pulse. Background

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

cs1114 REVIEW of details test closed laptop period

CS1150 Principles of Computer Science Boolean, Selection Statements

Visual C# Instructor s Manual Table of Contents

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

10/9/2012. Comparison and Logical Operators The if Statement The if else Statement Nested if Statements The switch case. switch case Statement

C/C++ Programming Lecture 7 Name:

CS 135 Lab Assignments Week 1

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

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.

Programming Logic and Design Sixth Edition

CS112 Lecture: Making Choices

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CS313D: ADVANCED PROGRAMMING LANGUAGE

CPS122 Lecture: From Python to Java

Logic Informatics 1 Functional Programming: Tutorial 6

ECEN 468 Advanced Logic Design

Lecture 2: Variables & Assignments

Lecture (03) Binary Codes Registers and Logic Gates

Course Outline. Introduction to java

Boolean Algebra Boolean Algebra

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

Boolean Logic & Branching Lab Conditional Tests

Lesson 38: Conditionals #2 (W11D3)

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Lecture 3: More SQL Basics

Chapter 3: Operators, Expressions and Type Conversion

Lecture 3: More SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases. Single-Criterion WHERE Conditions. SQL Conditions.

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

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

Variables and Operators 2/20/01 Lecture #

Lecture 7. Log into Linux New documents posted to course webpage

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

CSC 121 Spring 2017 Howard Rosenthal

Transcription:

Lecture 9 Reminder: Programming Assignment 3 is due Wednesday by 4:30pm. Exam 1 is on Friday. Exactly like Prog. Assign. 2; no collaboration or help from the instructor. Log into Windows/ACENET. Start MSVS and open "HelloWorldProgram", then open MSVS again and create a new console project called "QuadraticRootsProgram" and rename "Program.cs" (and the class) to "QuadraticRoots.cs" Questions? Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Outline Flow of control Simple sequence Selection Conditions (boolean expressions) Equality, relational, logical operators Short-circuit evaluation If-statements Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 2

Flow of Control Flow of control determines the order in which lines of code are executed. So far, every line of code in a method is executed in the order they are written. This order is called simple sequence. Sometimes, we do not want to execute every statement. This requires a choice to be made based on a test of some values. This flow order is called selection. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 3

Conditions Tests to determine which choice to be made are called conditions and are boolean expressions that evaluate to true or false (i.e., a value of bool type). There are three kinds of operators that evaluate to boolean values: equality, relational, and logical operators. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 4

Equality Operators The equality operators correspond to the English phrases "is equal to" and "is not equal to". Their operator symbols are == and!=, respectively. == evaluates to true when the values of both operands have the same value; false otherwise.!= evaluates to true when the values of the operands are not the same value; false otherwise. The types of the operands must be the same Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 5

Equality Operator Examples int avalue = 100, bvalue = 1000; char cvalue = 'A'; string svalue = "CS 205"; bool testresult = false; testresult = avalue == 100; testresult = avalue!= bvalue; testresult = cvalue!= 'A'; testresult = svalue == "cs 205"; Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 6

Relational Operators The relational operators and their English equivalents are as follows: < "is less than" <= "is less than or equal to" > "is greater than" >= "is greater than or equal to" The types of the operands must be the same. These operators are not defined on the string type. The characters are ordered based on the underlying encoding. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 7

Relational Operator Examples testresult = avalue > 10; testresult = avalue <= bvalue; testresult = cvalue < 'Z'; testresult = cvalue >= 'a'; Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 8

Logical Operators Logical operators have boolean expressions as operands. They are logical negation ("not", symbol!), conditional logical conjunction ("and", symbol &&), and conditional logical disjunction ("or", symbol ). The evaluation of these operators can be represented using a truth table: p q!p p && q p q true true false true true true false false true false true true false true false false false false Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 9

Logical Operator Examples // test if avalue is between 1 and 100, inclusive testresult = (1 <= avalue) && (avalue <= 10); // test if avalue is between 1 and 100, exclusive testresult = (1 < avalue) && (avalue < 100); // test if cvalue is an uppercase letter testresult = ('A' <= cvalue) && (cvalue <= 'Z'); // test if cvalue is a 'y' or a 'Y' testresult = (cvalue == 'y') (cvalue == 'Y'); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 10

Operator Precedence Just like the arithmetic operators, the equality, relational, and logical operators have precedence. Use ( )'s to override, or for clarity. Highest precedence: unary +, unary -,! (unary) *, / % (multiplicative) binary +, binary - (additive) <, <=, >, >= (relational) ==,!= (equality) && (conditional AND) (conditional OR) Lowest precedence: = (assignment) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 11

Short-Circuit Evaluation Both && and have short-circuit evaluation. For &&, if either of the operands is false, the entire expression is false, so when the first operand evaluates to false, the second one is not evaluated. This is often used to prevent an invalid computation. For example, if we want to make sure not to divide by 0: (n > 0) && (totalscores/n > 70) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 12

Short-Circuit Evaluation For, an expression is true if at least one of the operands is true, so if the first operand evaluates to true, the second one is not evaluated. This is often used when there a simple condition and a complex condition (perhaps encapsulated in a method call). For example, (n == 1) IsComplexCondition(x, y) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 13

If-Statements Now that we have conditions, we can use them to make choices for flow of control. For example, suppose we want to compare two numbers, a and b, and determine the larger one. In English, we might say "If a is greater than b, then a is the larger number; otherwise b is the larger number". In C# code, we use an if-statement, to implement this. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 14

If-Statements An if-statement has: A condition test A body to execute when the test is true An optional body to execute when the test is false The syntax of an if-statement is: if (<condition>) { <body to execute when condition is true> } else // this part is optional { <body to execute when condition is false> } Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 15

If-Statement Example The C# code for our example of determining the larger of values a and b is: double larger; if (a > b) condition test { larger = a; executed when condition is true } else { larger = b; executed when condition is false } Console.WriteLine ("{0:F} is the larger number", larger); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 16

If-Statement Example The "else" part of an if-statement is optional. Without the else part, the flow of control is to execute the if-body or skip it. For example, a wage earner is paid an hourly rate for up to 40 hours a week and an overtime rate that is 1.5 times the hourly rate for hours over 40. grosspay = hours * hourlyrate; // compute base pay if (hours > 40) // skip if condition is false { // compute and add overtime pay overtimepay = (hours 40) * hourlyrate * 1.5; grosspay = grosspay + overtimepay; } Console.WriteLine("Gross pay is {0:F2}", grosspay); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 17

In-Class Exercise For a quadratic equation, ax 2 bxc=0 the formula for finding the roots of the equation is: x= b± b 2 4 ac 2 a The quantity b 2 4 ac is called the discriminant. When the discriminant is greater than 0, the equation has 2 real roots. When it is 0, there is only 1 real root. And when it is less than 0, there are no real roots. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 18

In-Class Exercise In the Main method of the QuadraticsRoot project, write code to do the following: Prompt and input the three coefficients for a quadratic equation. Compute the discriminant for the equation. If the discriminant is greater than 0, compute and display the two real roots If the discriminant is 0, display the one real root. If the discriminant is less than 0, display a message saying there are no real roots. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 19

In-Class Exercise Three example runs: Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 3 The two real roots are 1.00 and 3.00 Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 1 The one real root is 1.00 Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 2 This equation has no real roots Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 20