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

Similar documents
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.

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

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

Chapter 4: Control Structures I (Selection)

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

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

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

Chapter 4 - Notes Control Structures I (Selection)

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

Add Subtract Multiply Divide

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

Checking Multiple Conditions

UEE1302(1102) F10: Introduction to Computers and Programming

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

Boolean Algebra Boolean Algebra

CHAPTER : 9 FLOW OF CONTROL

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

5. Selection: If and Switch Controls

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

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

Pointers, Dynamic Data, and Reference Types

CSI33 Data Structures

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.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

a data type is Types

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Introduction to Programming Using Java (98-388)

Control Structures: The IF statement!

2 nd Week Lecture Notes

CS1 Lecture 3 Jan. 18, 2019

REPETITION CONTROL STRUCTURE LOGO

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

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

CS1 Lecture 3 Jan. 22, 2018

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

C++ Final Exam 2017/2018

Why Is Repetition Needed?

WARM UP LESSONS BARE BASICS

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Increment and the While. Class 15

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Software Engineering using Formal Methods

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

Chapter 7. Additional Control Structures

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

Boolean evaluation and if statements. Making decisions in programs

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

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

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Relational Operators and if. Class 10

If Control Construct

Chapter 3 Objects, Types, and Values

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

1007 Imperative Programming Part II

A First Program - Greeting.cpp

Introduction to the C++ Programming Language

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

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

INTRODUCTION TO COMPUTER SCIENCE - LAB

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

Chapter 4 Computation

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Fundamentals of Programming CS-110. Lecture 2

Input And Output of C++

Introduction to Bioinformatics

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

Chapter 2. C++ Basics

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

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Formal Specification and Verification

Exercise: Inventing Language

Programming in C++ PART 2

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Software Engineering using Formal Methods

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Conditions, logical expressions, and selection. Introduction to control structures

Computer Science II Lecture 1 Introduction and Background

C++ Program Flow Control: Selection

C++ PROGRAMMING SKILLS Part 2 Programming Structures

CS313D: ADVANCED PROGRAMMING LANGUAGE

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CSCE 206: Structured Programming in C++

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

Programming, numerics and optimization

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Transcription:

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

Conditional Execution if is a reserved word The most basic syntax for if: if( condition ) statement The statement is executed if the condition evaluates to true The statement is bypassed if the condition evaluates to false 2

bool Data Type and Conditions A condition can be a bool variable The data type bool has logical (Boolean) values true and false bool, true, and false are reserved words The identifier true has the value 1 The identifier false has the value 0 3

int Data Type and Conditions Earlier versions of C++ did not provide built-in data types that had Boolean values Logical expressions evaluate to either 1 or 0 The value of a logical expression was stored in a variable of the data type int You can use the int data type as a condition 4

Logical Expressions General syntax for if: if( logical-expression ) statement A logical expression is any expression that evaluates to true or false A literal (anything but 0 is true) A variable (any built-in type) A function (should return bool or int) Any expression that evaluates to bool or int 5

Logical Expressions Arithmetic expressions Built with arithmetic operators Evaluate to numbers (integer or floating-point) 3 + 5 (7 / 2) * 4.0 Logical expressions Built with relational operators Evaluate to true or false 3 == 3 hello < goodbye 6

Relational Operators 7

Comparing Numbers Integer and floating-point types can be compared 8 < 15 evaluates to true 6!= 6 evaluates to false 2.5 > 5.8 evaluates to false 5.9 <= 7 evaluates to true 8

Comparing Characters 9

Comparing strings Relational operators can be applied to strings Strings are compared character by character, starting with the first character Comparison continues until either a mismatch is found or all characters are found equal If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string The shorter string is less than the larger string Note: this does not work for comparing 2 string literals! 10

Examples 11

Common Syntax Errors 13

Two-way Conditional Execution if can be paired with else if( logical-expression ) statement1 else statement2 If the condition is true, statement1 is executed If the condition is false, statement2 is executed 14

One-Way Selection 15

Two-Way Selection 16

Example 17

Common Syntax Errors 18

Block Statements A block (or compound) statement looks like: A block can be used anywhere a statement can be used 19

Conditional Block Statements if (age > 18) cout << "No longer a minor." << endl; else cout << "Still a minor." << endl; 20

Conditional Block Statements if (age > 18) { cout << "No longer a minor." << endl; } else { cout << "Still a minor." << endl; } 21

Conditional Block Statements if (age > 18) { cout << "No longer a minor." << endl; cout << "Eligible to vote." << endl; } else { cout << "Still a minor." << endl; cout << "Not eligible to vote." << endl; } 22

More Than 2 Choices Series of if statements: if( logical-expression1 ) { } statement1 if( logical-expression2 ) { } statement2 if( logical-expression3 ) { } statement3 Checks all three conditions Can t have a default else condition Used for statements that are not mutually exclusive

More Than 2 Choices For mutually exclusive conditions, use an if else tree Stops when a condition is true Can have a default else condition if( logical-expression1 ) { } statement1 else if( logical-expression2 ) { } statement2 else if( logical-expression3 ) { } else { } statement3 statement4

Input Example: Date Conversion Date in the form yyyy-mm-dd (e.g. 2009-09-24) Output Date in the form month day, year (e.g. September 24, 2009)

Example: Large Joe s Write a simple fast food drive-through ordering program for Large Joe s restaurant. The menu is: Triple Burger: $4.99 Fried Chicken $6.99 French Fries $2.29 Sample run (user input in bold): == Welcome to Large Joe's, can I take your order? == For a triple burger, press 1 For a heap of fried chicken, press 2 Your order: 1 Would you like fries with that? (y/n): y Your total is $7.28, please drive through.

Example: Large Joe s Large Joe s now has bacon! Adding bacon to your burger costs $0.99 Modify the program so that if the customer orders a burger, the program asks them: Would you like bacon on your burger? (y/n): y If they answer yes, add the cost to their order Don t ask about bacon if they didn t order a burger!