CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Similar documents
9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

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

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CSI33 Data Structures

If Control Construct

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.

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

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CSCI 1061U Programming Workshop 2. C++ Basics

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

5. Selection: If and Switch Controls

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

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

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

This watermark does not appear in the registered version - Slide 1

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

UNIT- 3 Introduction to C++

LAB 4.1 Relational Operators and the if Statement

Computer Programming I - Unit 5 Lecture page 1 of 14

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Programming with C++ as a Second Language

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Exam I Review Questions Fall 2010

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Getting started with C++ (Part 2)

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSCE 206: Structured Programming in C++

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

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

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Introduction to Computer Science Midterm 3 Fall, Points

BASIC ELEMENTS OF A COMPUTER PROGRAM

Loops and Expression Types

do { statements } while (condition);

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

REPETITION CONTROL STRUCTURE LOGO

Maciej Sobieraj. Lecture 1

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Looping. Arizona State University 1

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

CS242 COMPUTER PROGRAMMING

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Homework #3 CS2255 Fall 2012

Review Questions I Spring 2010

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

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

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

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

Review for Test 1 (Chapter 1-5)

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

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

Integer Data Types. Data Type. Data Types. int, short int, long int

C++ Final Exam 2017/2018

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

VARIABLES & ASSIGNMENTS

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

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

Chapter 2: Basic Elements of C++

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

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

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

CS 201 (Intro. to Computing) Fall 2015 Sabancı University Sample Questions for Midterm 1

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

More Complex Versions of the if Statement. Class 13

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

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Lecture 3 Tao Wang 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer System and programming in C

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 8. Statement-Level Control Structures


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

CSCE 110 PROGRAMMING FUNDAMENTALS

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Boolean Algebra Boolean Algebra

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

Chapter 7. Additional Control Structures

1 Lexical Considerations

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Unit 7. 'while' Loops

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Transcription:

CS150 Intro to CS I Fall 2017 Fall 2017 CS150 - Intro to CS I 1

Chapter 4 Making Decisions Reading: Chapter 3 (3.5 pp. 101), Chapter 4 (4.4 pp. 166-168; 4.5 pp. 169-175; 4.6 pp.176-181; 4.8 pp. 182-189; 4.9 pp. 189-199; 4.14 pp. 202-210 Good Problems to Work: pp. 104 [3.13]; pp. 166 [4.14]; pp. 175 [4.15]; p. 180 [4.16]; p 190 [4.19, 4.20]; pp.209-210 [4.27, 4.29, 4.30] Fall 2017 CS150 - Intro to CS I 2

Explicit Type Conversion A type cast expression lets you manually change the data type of a value The syntax for type casting is static_cast<datatype>(value) Value is a variable or literal value If Value is a variable, the variable is NOT changed. DataType is the data type that you are converting Value into Fall 2017 CS150 - Intro to CS I 3

Example double number = 3.7; int val; val = static_cast<int>(number); What is saved into val? Fall 2017 CS150 - Intro to CS I 4

if Statement We may want to execute some code if an expression is true, and execute some other code when the expression is false. This can be done with two if statements if (value >= LIMIT) // do something if (value < LIMIT) // do something else Fall 2017 CS150 - Intro to CS I 5

Double-Alternative if C++ provides a shortcut to combine 2 if statements if (expression) // stmts if expression is true else // stmts if expression is false Fall 2017 CS150 - Intro to CS I 6

Problem int number; cout << Enter a number, I ll tell you ; cout << if it is odd or even: ; cin >> number; // write a double-alternative if here Fall 2017 CS150 - Intro to CS I 7

Problem Are these two code snippets equivalent? int x, y; cin >> x >> y; if(x > y) cout << x; if(x < y) cout << y; int x, y; cin >> x >> y; if(x > y) cout << x; else cout << y; Fall 2017 CS150 - Intro to CS I 8

Multiple-Alternative if cout << Enter two numbers: ; cin >> num1 >> num2; if (num1 > num2) cout << num1 << is greater << endl; else if (num2 > num1) cout << num2 << is greater << endl; else cout << Numbers are equal << endl; Fall 2017 CS150 - Intro to CS I 9

Problem Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1 int poscount = 0, negcount = 0, zerocount = 0; Fall 2017 CS150 - Intro to CS I 10

Logical Operators && And Or! Not Fall 2017 CS150 - Intro to CS I 11

Evaluating AND expr1 && expr2 For the complete expression to be true, both expr1 and expr2 must be true Example: (temp > HOT) && (humidity > STICKY) These are unbearable heat and humidity conditions Both must be true for the entire expression to be true Fall 2017 CS150 - Intro to CS I 12

Evaluating OR expr1 expr2 The complete expression is true, if either expr1 or expr2 is true Example: (salary < MIN_SALARY) (MARRIED == status) To qualify for financial aid, salary has to be less than some minimum salary OR you must be married Only one condition has to be true Fall 2017 CS150 - Intro to CS I 13

Evaluating NOT!expr If expr is true,!expr is false If expr is false,!expr is true Example:!(salary < MIN_SALARY) What makes this true? False? Fall 2017 CS150 - Intro to CS I 14

Operator Precedence (highest to lowest) Fall 2017 CS150 - Intro to CS I 15

Problem According to the operator precedence and associativity rules given on the previous slide, how will the following expressions be evaluated? x < min + max min <= x && x <= max!x == y + 2 x = a + b % 7 * 2 Fall 2017 CS150 - Intro to CS I 16

Problem Write a program segment that prints the message The number is valid if the variable speed is within the range 0-20 inclusive You must use logical operators Fall 2017 CS150 - Intro to CS I 17

Problem A bookstore s shipping policy is: 1. If the order is $30 or less, shipping is $5 2. If the order is over $30 but less than $50, shipping is $3 3. If the order is $50 or more then shipping is $2 Rewrite this program using logical operators Fall 2017 CS150 - Intro to CS I 18

switch statement Let s look at the following program segment: char choice; cout << "E)dit S)ave Q)uit"; cin >> choice; switch (choice) case 'E': cout << "Time to edit " << endl; break; case 'S': cout << "Time to save" << endl; break; default: cout << "Illegal command" << endl; Fall 2017 CS150 - Intro to CS I 19

switch format switch(ordinaldatatype) case constantexpression: // one or more stmts break; case constantexpression: // one or more stmts break; default : // one or more stmts What is an ordinal data type? (ordinaldatatype)can be a variable or expression constantexpression must be unique in each case default is optional break; resumes execution after the switch Fall 2017 CS150 - Intro to CS I 20

Problem 1. Modify slide 20 to allow E, e, S, or s 2. Rewrite the logic for 1. as an if statement Fall 2017 CS150 - Intro to CS I 21