Lecture 5 Tao Wang 1

Similar documents
Lecture 7 Tao Wang 1

Lecture 3 Tao Wang 1

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

Decision Making in C

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

LECTURE 5 Control Structures Part 2

Microsoft Visual Basic 2005: Reloaded

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

Boolean evaluation and if statements. Making decisions in programs

Introduction to C Programming

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018

LECTURE 04 MAKING DECISIONS

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Software Design & Programming I

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

Lecture 18 Tao Wang 1

The following expression causes a divide by zero error:

Flow Control. CSC215 Lecture

Programming for Engineers Iteration

Lecture 6. Statements

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Computational Expression

Control Structures in Java if-else and switch

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Control Structures in Java if-else and switch

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

Pace University. Fundamental Concepts of CS121 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

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.

Exceptions, Case Study-Exception handling in C++.

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

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

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

Fundamental of Programming (C)

Chapter 2: Functions and Control Structures

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

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

CHAPTER : 9 FLOW OF CONTROL

The SPL Programming Language Reference Manual

INTRODUCTION TO COMPUTER SCIENCE - LAB

Computer Programming : C++

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 4: Making Decisions

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction to Programming Using Java (98-388)

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Introduction. C provides two styles of flow control:

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

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

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

STUDENT LESSON A12 Iterations

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

SELECTION. (Chapter 2)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Information Science 1

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

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

CSc Introduction to Computing

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

CT 229 Java Syntax Continued

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

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

Chapter 17. Fundamental Concepts Expressed in JavaScript

Lecture 2 Tao Wang 1

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

The PCAT Programming Language Reference Manual

Handout 4 Conditionals. Boolean Expressions.

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

BRANCHING if-else statements

Fundamentals of Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Loops / Repetition Statements

Chapter 4 - Notes Control Structures I (Selection)

ARG! Language Reference Manual

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

Introduction to Java & Fundamental Data Types

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

CS110D: PROGRAMMING LANGUAGE I

DECISION MAKING STATEMENTS

Chapter 3: Decision Structures

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Conditional Statement

REPETITION CONTROL STRUCTURE LOGO

Chapter 2 Control in Programming. lecture 2 1

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

In this chapter you will learn:

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Transcription:

Lecture 5 Tao Wang 1

Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists, Third Edition 2

Selection Criteria if-else statement: Implements a decision structure for two alternatives Syntax: if (condition) statement executed if condition is true; else statement executed if condition is false; C++ for Engineers and Scientists, Third Edition 3

Selection Criteria (continued) The condition is evaluated to its numerical value: A non-zero value is considered to be true A zero value is considered to be false The else portion is optional; it is executed only if the condition is false The condition may be any valid C++ expression C++ for Engineers and Scientists, Third Edition 4

Relational Operators Relational expression: Compares two operands or expressions using relational operators Table 4.1 C++ s Relational Operators C++ for Engineers and Scientists, Third Edition 5

Relational Operators (continued) Relational expressions are evaluated to a numerical value of 1 or 0 only: If the value is 1, the expression is true If the value is 0, the expression is false char values are automatically coerced to int values for comparison purposes Strings are compared on a character by character basis The string with the first lower character is considered smaller C++ for Engineers and Scientists, Third Edition 6

Relational Operators (continued) Examples of string comparisons C++ for Engineers and Scientists, Third Edition 7

Logical Operators AND (&&): Condition is true only if both expressions are true OR ( ): Condition is true if either one or both of the expressions is true NOT (!): Changes an expression to its opposite state; true becomes false, false becomes true AND T F T T F F F F OR T F T T T F T F Examples: (5<6) && 5 is even (-2 is positive) or 2 is even A is not a number and -1!= 1 C++ for Engineers and Scientists, Third Edition 8

Logical Operators (continued) Table 4.2 Operator Precedence and Associativity C++ for Engineers and Scientists, Third Edition 9

A Numerical Accuracy Problem Comparing single and double precision values for equality (==) can lead to errors because values are stored in binary Instead, test that the absolute value of the difference is within an acceptable range Example: abs(operandone operandtwo) < 0.000001 C++ for Engineers and Scientists, Third Edition 10

The if-else Statement if-else performs instructions based on the result of a comparison Place statements on separate lines for readability Syntax: C++ for Engineers and Scientists, Third Edition 11

The if-else Statement (continued) Figure 4.2 The if-else flowchart C++ for Engineers and Scientists, Third Edition 12

The if-else Statement (continued) C++ for Engineers and Scientists, Third Edition 13

Compound Statements Compound statement: A sequence of single statements contained between braces Creates a block of statements Block of statements can be used anywhere that a single statement is legal Any variable declared within a block is usable only within that block Scope: The area within a program where a variable can be used A variable s scope is based on where the variable is declared C++ for Engineers and Scientists, Third Edition 14

Block Scope Statements contained in compound statement are a single block of code Scope of the variable: Area in a program where a variable can be used C++ for Engineers and Scientists, Third Edition 15

Block Scope (continued) C++ for Engineers and Scientists, Third Edition 16

One-Way Selection One-way selection: An if statement without the optional else portion Figure 4.3 A one-way selection if statement C++ for Engineers and Scientists, Third Edition 17

Problems Associated with the ifelse Statement Common problems with if-else statements: Misunderstanding what an expression is Using the assignment operator (=) instead of the relational operator (==) ERRORS!!! C++ for Engineers and Scientists, Third Edition 18

Nested if Statements if-else statement can contain any valid C++ statement, including another if-else Nested if statement: an if-else statement completely contained within another if-else Use braces to block code, especially when inner if statement does not have its own else C++ for Engineers and Scientists, Third Edition 19

Nested if Statements (continued) Figure 4.4a Nested within the if part C++ for Engineers and Scientists, Third Edition 20

The if-else Chain if-else chain: A nested if statement occurring in the else clause of the outer if-else If any condition is true, the corresponding statement is executed and the chain terminates Final else is only executed if no conditions were true Serves as a catch-all case if-else chain provides one selection from many possible alternatives C++ for Engineers and Scientists, Third Edition 21

The if-else Chain (continued) Figure 4.4b Nested within the else part C++ for Engineers and Scientists, Third Edition 22

The if-else Chain (continued) General form of an if-else chain C++ for Engineers and Scientists, Third Edition 23

If-else chain example The student s number grade corresponds to a letter grade as following: A: 90 and 90 above B: 80 89 C: 70 79 D: 60 69 F: below 60 Fundamentals of Information Systems, Fifth Edition 24

Common Programming Errors Using the assignment operator (=) instead of the relational operator (==) for an equality test Placing a semicolon immediately after the condition Assuming a structural problem with an if-else causes the error instead of focusing on the data value being tested Using nested if statements without braces to define the structure C++ for Engineers and Scientists, Third Edition 25