C++ Program Flow Control: Selection

Similar documents
Chapter 4 - Notes Control Structures I (Selection)

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

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

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

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

Chapter 4: Control Structures I (Selection)

Checking Multiple Conditions

Chapter 4: Making Decisions

Chapter 4: Making Decisions

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

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

C++ Programming: make

LECTURE 04 MAKING DECISIONS

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

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

1 Lexical Considerations

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

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

C++ Final Exam 2017/2018

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

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

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

UEE1302(1102) F10: Introduction to Computers and Programming

Introduction to Programming

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Java Notes. 10th ICSE. Saravanan Ganesh

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

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

CSI33 Data Structures

Lexical Considerations

C++ Programming: Functions

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

If Control Construct

Lexical Considerations

Relational Operators and if. Class 10

IT 1003 Introduction to Programming (New)

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

5. Selection: If and Switch Controls

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Control Structures in Java if-else and switch

Add Subtract Multiply Divide

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

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

Boolean Algebra Boolean Algebra

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)

Absolute C++ Walter Savitch

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

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

5. Control Statements

Control Structures in Java if-else and switch

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

COP 1220 Introduction to Programming in C++ Course Justification

CSCE 206: Structured Programming in C++

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

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

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

Programming Logic and Design Sixth Edition

Review of the C Programming Language for Principles of Operating Systems

Programming. C++ Basics

Logical Operators and if/else statement. If Statement. If/Else (4.3)

CS242 COMPUTER PROGRAMMING

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

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

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

CSCI 1061U Programming Workshop 2. C++ Basics

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Programming for Engineers Iteration

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

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

Programming, numerics and optimization

BITG 1233: Introduction to C++

CS Spring 05 - MidTerm

CS201- Introduction to Programming Current Quizzes

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Operators in java Operator operands.

Type Conversion. and. Statements

Chapter-8 DATA TYPES. Introduction. Variable:

Increment and the While. Class 15

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.

Conditional Statement

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

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Creating a C++ Program

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

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

IS 0020 Program Design and Software Tools

LECTURE 02 INTRODUCTION TO C++

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

REVIEW. The C++ Programming Language. CS 151 Review #2

Introduction to Programming Using Java (98-388)

Transcription:

C++ Program Flow Control: Selection Domingos Begalli Saddleback College, Computer Science CS1B, Spring 2018 1 / Domingos Begalli CS1B Spring 2018 C++ Introduction 1/19 19

Control program flow control structures selection if, if-else, switch -case repetition for, while, do-while 2 / Domingos Begalli CS1B Spring 2018 C++ Introduction 2/19 19

Control logical expressions logical expressions are also called boolean expressions expressions that evaluate to or conditional statements evaluate them using relational operators 3 / Domingos Begalli CS1B Spring 2018 C++ Introduction 3/19 19

Control relational operators operator description == equal to!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to 4 / Domingos Begalli CS1B Spring 2018 C++ Introduction 4/19 19

Relational Operators comparing integers and real numbers usage result 8 == 8 8!= 8 8 < 8 8 > 8.0 8.0 <= 8 8.0 >= 8.0 5 / Domingos Begalli CS1B Spring 2018 C++ Introduction 5/19 19

Relational Operators comparing characters - CLI is your friend: man ascii [partial] ascii character set in decimal 32 sp 33! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44, 45-46. 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 125 } 126 ~ 127 del 6 / Domingos Begalli CS1B Spring 2018 C++ Introduction 6/19 19

Selection Flow one-way selection if (expression) process enter if (expression) process exit 7 / Domingos Begalli CS1B Spring 2018 C++ Introduction 7/19 19

Selection Flow two-way selection if (expression) process else process enter if (expression) process process exit 8 / Domingos Begalli CS1B Spring 2018 C++ Introduction 8/19 19

Logical Expressions bool type and boolean expressions old c++ compilers relied on 1 for & 0 for int age = 15; int legalage; legalage = ( age >= 21) // legalage i s now 0 new c++ compilers support boolean values & int age = 15; bool legalage; legalage = ( age >= 21) // legalage i s now f a l s e ( a l s o 0) 9 / Domingos Begalli CS1B Spring 2018 C++ Introduction 9/19 19

Logical Expressions boolean operators operator description! not && and or 10 / Domingos Begalli CS1B Spring 2018 C++ Introduction 10/19 19

Logical Expressions boolean operators - the! (not) operator expression value 11 / Domingos Begalli CS1B Spring 2018 C++ Introduction 11/19 19

Logical Expressions boolean operators - the & & (and) operator expr 1 expr 2 value 12 / Domingos Begalli CS1B Spring 2018 C++ Introduction 12/19 19

Logical Expressions boolean operators - the (or) operator expr 1 expr 2 value 13 / Domingos Begalli CS1B Spring 2018 C++ Introduction 13/19 19

Operators order of precedence value expression first unary! + - second * / % third + - fourth < <= >= > fifth ==!= sixth && seventh eighth = 14 / Domingos Begalli CS1B Spring 2018 C++ Introduction 14/19 19

Conditional Operator if can be replaced by the ternary operator?: expr_1? expr_2 : expr_3; for example, rather than: if ( a > b ) { max = a; } else { max = b; } the conditional version can be used: max = (a >= b)? a : b; // or : a > b? max = a : max = b; 15 / Domingos Begalli CS1B Spring 2018 C++ Introduction 15/19 19

Boolean Semantics short-circuit evaluation - for sake of optimization... compilers ignore second expression if x is greater than 5: (x > 5) (y == 3) similarly if x is less than 5: (x > 5) && (y == 3) however... x = 6; if (0 <= x <= 5) evaluates to since 0 < 6 evaluates to 1, which is less than 5 16 / Domingos Begalli CS1B Spring 2018 C++ Introduction 16/19 19

Floating-point Numbers Comparison other problems double x = 1.0; double y = 2.0/7.0 + 3.0/7.0 + 2.0/7.0; if (x == y) { cout << " x and y are the same\n"; } else { cout << "NOT the same: "; cout << "x = " << x << ", and y = " << y << endl; } // p r i n t s NOT the same : x = 1, and y = 1 17 / Domingos Begalli CS1B Spring 2018 C++ Introduction 17/19 19

Multiple Selections nested ifs enter if (expr_1) { block_1 } else if (expr_2) { block_2.... if (expr_1) elseif (expr_2) elseif (expr_n) process block_1 process block_2 process block_n } else if (expr_n) { block_n } else { default_block } else default_block exit 18 / Domingos Begalli CS1B Spring 2018 C++ Introduction 18/19 19

Multiple Selections switch case instead of nested ifs switch (selector) switch (selector) { case const_1: block_1 break; case const_2: block_2 break;.... case const_n: block_n break; default: default_block } case (const_1) block_1 break case (const_2) block_2 break case (const_n) block_n break default default block exit 19 / Domingos Begalli CS1B Spring 2018 C++ Introduction 19/19 19