BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Similar documents
C++ DATA TYPES BASIC CONTROL FLOW

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

WELCOME TO CS 16! Problem Solving with Computers-I

C-String Library Functions

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Common Misunderstandings from Exam 1 Material

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS2141 Software Development using C/C++ C++ Basics

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

From Pseudcode Algorithms directly to C++ programs

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

Add Subtract Multiply Divide

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

If Control Construct

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

primitive arrays v. vectors (1)

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

CSCE 121 ENGR 112 List of Topics for Exam 1

The following program computes a Calculus value, the "trapezoidal approximation of

Computer Programming : C++

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

Increment and the While. Class 15

Program Organization and Comments

CP122 CS I. Iteration

Programming in C++ PART 2

Introduction to C++: I

First C or C++ Lab Paycheck-V1.0 Using Microsoft Visual Studio

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

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

C++ basics Getting started with, and Data Types.

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

Outline. Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Maximum of a sequence. Maximum of a sequence

Relational Operators and if. Class 10

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

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

C:\Temp\Templates. Download This PDF From The Web Site

2 2

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

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

LAB 4.1 Relational Operators and the if Statement

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Communication With the Outside World

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

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.

Introduction to Programming using C++

Lab Instructor : Jean Lai

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

Announcements. HW 0 due tonight (check that it compiles on CSE labs) (make sure you submit the code part)

Linked List using a Sentinel

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

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

Strings and Stream I/O

More Complex Versions of the if Statement. Class 13

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

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

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

Boolean Algebra Boolean Algebra

Shell Programming (Part 2)

CSI33 Data Structures

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

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

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

Name Section: M/W T/TH Number Definition Matching (8 Points)

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

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

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

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

Lecture 2, September 4

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

Computer Science II Lecture 1 Introduction and Background

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

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

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

Recitation 2/18/2012

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Connecting with Computer Science, 2e. Chapter 15 Programming II

Control Structures of C++ Programming (2)

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

CS 0449 Sample Midterm

Announcements. Lab 1 this week! Homework posted Wednesday (late)

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

Streams in C++ Stream concept. Reference information. Stream type declarations

Review Questions I Spring 2010

The Design Recipe Fall 2017

Lab # 02. Basic Elements of C++ _ Part1

CS 222: More File I/O, Bits, Masks, Finite State Problems

CSC 1300 Exam 4 Comprehensive-ish and Structs

Do not turn to the next page until the start of the exam.

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

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

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

C++ Lab 03 - C++ Functions

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

Transcription:

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT Problem Solving with Computers-I

Announcements HW02: Complete (individually)using dark pencil or pen, turn in during lab section next Wednesday Please use Piazza to ask questions instead of email If you must email me, include [CS16] in the subject line

What is git? Git is a version control system (VCS). A VCS allows you to keep track of changes in a file (or groups of files) over time Git allows you to store code on different computers and keep all these different copies in sync

Git Concepts repo (short for repository): a place where all your code and its history is stored Remote repo: A repo that exists on the web (in our case github.com)

In class demo - creating a repo on github.com - adding collaborators to the repo - adding files to the repo - Updating files in a remote repo using a web browser - Viewing the version history

Boolean Expressions An expression that evaluates to either true or false. You can build Boolean expressions with relational operators: == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the right value <= // true if left value is less than OR EQUAL to the right value > // true if left value is greater than the right value >= // true if left value is greater than OR EQUAL to the right value

Boolean Expressions Integer values can be used as boolean values C++ will treat the number 0 as false and any non-zero number as true. bool x = 5 == 1; // x = 0 bool x = 3!= 2; // x = 1 Combine boolean expressions using Logical Operators! // inverts true to false or false to true && // boolean AND // boolean OR Example bool x = true; bool y = true; x =!x; // x = false x = x && y // x = false x = x y // x = true

Control flow: if statement The condition is a Boolean expression These can use relational operators if ( Boolean expression) { // statement 1; // statement 2; } In C++ 0 evaluates to a false Everything else evaluates to true

Examples of if statements The condition is a Boolean expression These can use relational operators if ( 1 < 2 ) { cout<< foo ; } if ( 2 == 3) { cout<< foo ; } Use the curly braces even if you have a single statement in your if

Fill in the if condition to detect numbers divisible by 3 A. x/3 == 0 B.!(x%3) C. x%3 == 0 D. Either B or C E. None of the above if ( ) cout<< x << is divisible by 3 \n ; }

11 Control Flow: if-else if (x > 0){ pet = dog; count++; } else { pet = cat; count++; } Can you write this code in a more compact way?

12 Control Flow: Multiway if-else if (x > 100){ pet = dog; count++; } else if (x > 90){ pet = cat; count++; } else { pet = owl; count++; } Can you write this code in a more compact way?

13 Input from user (using cin) Getting input from stdin (standard input) int x; cout<< Enter a number <<endl; cin>>x;

14 Let s code Fizzbuzz -1.0 $ Enter a number: 1 1 $ Enter a number: 2 2 $ Enter a number: 3 fizz $ Enter a number: 4 4 $Enter a number: 5 5 $Enter a number: 6 fizz $Enter a number: 7 7 $Enter a number: 15 fizz

15 Input from user (via the command line) We can pass information into a C++ program through the command line when executing the program. The main function will need to have the following: int main(int argc, char *argv[]) `int argc` is the number of "arguments" the program has, including the executable name. `char* argv[]` is the "list" of arguments passed into the program. argv[0]: name of the program argv[1]: 1 st argument, remember this is a C-string Use atoi to convert a C-string to a number atoi(argv[1])

16 Let s code Fizzbuzz -2.0 (taking arguments from main) $./fizzbuzz 1 1 $./fizzbuzz 9 Fizz $./fizzbuzz 15 Fizzbuzz

Next time Loops