This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.

Slides:



Advertisements
Similar presentations
Chapter 3 Objects, types, and values Bjarne Stroustrup
Advertisements

Current Assignments Homework 1 due in 4 days (June 16 th ) Variables, mathematical and logical operators, input/output, and the “ if ” operator. Project.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Primitive Variables.
Chapter 3, Part 2 s Input: cin s Example programs.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Chapter 05 (Part III) Control Statements: Part II.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Basic Elements Skill Area 313 Part B. Lecture Overview Basic Input/Output Statements Whitespaces Expression Operators IF Statements Logical Operators.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Fundamentals of Characters and Strings
getline() function with companion ignore()
Structured Programming (Top Down Step Refinement)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 3: Input/Output
Introduction to C++ Programming
Lexical Elements & Operators
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
getline() function with companion ignore()
Programming Fundamental-1
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer from the user and prints “even” if the number is even and “odd” otherwise

Whitespace Whitespace consists of spaces, newlines, and tabs. C++ treats all whitespace as if it were just one space –e.g. int x is the same as int x You do need white space to separate names and keywords in C++ –e.g. int x is not the same as intx You do not need whitespace between a variable name or keyword and an operator –e.g. x + y, x + y, and x+y are all correct –e.g. cout<<x; is the same as cout << x;

Input/Output, revisited Think of input and output as two streams of data. One stream flows out of your program to the screen One stream flow into your program from the keyboard “cout” is the name of the output stream “cin” is the name of the input stream

Input/Output, revisited In order to put things into the output stream so that they are displayed we used the stream insertion operator –e.g cout << x; –Think of the stream insertion operator as pointing x towards the “cout” output stream.

Input/Output, revisited To get values out of the input stream we use the stream extraction operator –e.g. cin >> x –Think of >> as pointing from the input stream “cin” towards the variable. You can have as many insertion and extraction operators as you want for each cout or cin. –So we can write cout << x << y << z; –Or cin >> x >> y >> z;

Input/Output, revisited We can also put text directly into the output stream with String Constants: –A string constant is a sequence of characters enclosed in double quotes –e.g. cout << “This is a string constant”; –There are special characters that we can include in our string constant that you can’t type on the keyboard. –These characters are represented special codes called escape sequences.

Input/Output, revisited If you want a newline for example: –cout << “Prints on line 1\nPrints on line 2”; Produces the output: Prints on line 1 Prints on line 2 cout << “Prints on line 1 Prints on line 2”; is incorrect.

Input/Output, revisited We can also insert commands into the stream –e.g. cout << flush; tells the stream to add a display everything in the stream right away without adding a newline. –e.g. cout << endl; is the same as flush but it adds a newline as well. –There are many other commands that we can insert into the output stream, they generally determine the format of the output.

Current Assignments Homework 1 due in 5 days (June 16 th ) Variables, mathematical and logical operators, input/output, and the “if” operator. (After today’s class you should be able to do all the problems on Homework 1) Project 1 Due in 12 days (June 23 rd ) Write a binomial root solver using the quadratic equation. (After today’s class you should be able to write this program)

How to allocate memory for variables of different types. How to name those variables. The primitive mathematical operators we can apply to those variables. The order in which operators are applied. How to print the value of a variable. How to take a value from the keyboard and put it in a variable. Last Time

The Programming Cycle There are two kinds of programming language: –Some languages such as Lisp and Prolog have an interpreter that performs a statement as soon as you type it in. With these languages you get instant feedback on whether the statement you just wrote makes any sense or not. –Other languages such as Fortran and C++ are called compiled languages. They separate the process of writing the program, compiling it into machine code, and running the program into three separate steps.

The Programming Cycle The programming cycle for C++ consists of four main phases. 1)Design. Here the algorithm we want the program to execute is developed. There are lots of different tools we can use in the design phase: flow charts, pseudocode, UML being a few choices of how we can represent the algorithm or “logic flow” of the program. 2)“Coding” translating the algorithm into the particular syntax of the language we want to use to create the program.

The Programming Cycle 3)Compilation. Here the source file we wrote in the coding phase is checked for obvious errors by the compiler and then translated into machine code. 4)Debugging. There are two types of debugging: a.First we resolve any compilation errors. Compilation errors are typically the result of typos and careless mistakes with syntax. b.Once the program compiles and we run it we analyze its behavior to make sure that it does what we expected.

Boolean Operators Whereas the mathematical operators we saw before act on integers and floating point numbers boolean operators act on true/false values. All boolean operators return a value of the type bool which can only take on the values “true” or “false.”

Boolean Operators, Comparitors The primitive boolean operators are: > greater than, e.g. 5 > 4 returns true < less than, e.g. 5 < 4 returns false >=, greater than or equal, e.g. 5 >= 5 returns true <=, less than or equal e.g. 5 <= 2 returns false

Boolean Operators, Equality ==, equality e.g.5 == 5 returns true 5.5 == 4.3 returns false !=, not equal e.g.5 != 5 returns false 5.5 != 4.3 returns true

Boolean Operators, And &&, logical “and” true && false returns false true && true returns true false && true returns false false && false returns false

Boolean Operators, Or and Not ||, logical “or” true || false returns true true || true returns true false || true returns true false || false returns false !, logical “not”, logical negation !true returns false !false, returns true

Boolean Operators, Precedence ! (not) has the highest precedence of any operator Comparators have lower precedence than +, and –, * and / != and == have lower precedence than (other) comparators && has lower precedence than != and == || has lower precedence than && but higher precedence than unary operators like +=

Boolean Operators, Precedence Expressions within parentheses are always evaluated first. If there are nested sets of parentheses the inner most parentheses are evaluated first. You can avoid all precedence issues in your own code by always using parentheses to force the order of evaluation. You have to know the order when reading other people’s code.

Boolean Operators, Example 1 int main { bool x = true, y = false, result = true; result = x && y || y; cout << result << endl; return 0; } 0

Boolean Operators, Example 3 int main { int x = 6, y = 8, z = -1, w = 14, bool result = false; result = (x + z != w) && !(x > 6) cout << result << endl; return 0; } 1

Boolean Operators, Example 4 int main { int x = 6, y = 8, z = -1, w = 14, bool result = false; result = (z == w) || (x <= 6) cout << result << endl; return 0; } 1

The “if” Control Structure We would like our programs to be more than just calculators we want them to make decisions. Decision making in programming is called branching. The program goes down one branch if some condition is true and down another if that condition is false. Statements that make decisions about what branch of instructions to execute next are called control structures. The most common control structure is the “if” statement.

The “if” Control Structure The syntax of the if control structure is: if ( boolean_expression ) { statements… } If boolean_expression returns “true” then the statements inside the braces are executed. If the expression is false then those statements are skipped.

The “if” Control Structure, Example 1 #include int main() { int x = 6, y = 12; if ( x > 5 ) { cout << “y = “ << y << endl; } cout << “x = “ << x << endl; return 0; }

The “if” Control Structure, Example 2 #include int main() { int x = 6, y = 12; if ( x < 5 ) { cout << “y = “ << y << endl; } cout << “x = “ << x << endl; return 0; }

The “if” Control Structure, Example 2 #include int main() { int x = 6, y = 12; if ( x < 5 || y != 15) { cout << “y = “ << y << endl; } cout << “x = “ << y << endl; return 0; }

Lab – Parity Program 1)Login to UNIX with SSH 2)Start xwin32 3)Start emacs 4)Open a new file called parity.cpp 5)Write a program that takes an integer as input and displays “even” if the integer is even and “odd” otherwise 6)This program will require two “if” statements, one for even and one for odd 7)x % 2 returns 0 if x is even and 1 if x is odd. 8)Compile, debug, and run your program.