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

Similar documents
Software Design & Programming I

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++

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

BITG 1233: Introduction to C++

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Chapter 5: Loops and Files

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

UNIT- 3 Introduction to C++

Week 3: File I/O and Formatting 3.7 Formatting Output

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

Objectives. In this chapter, you will:

Chapter 3 - Notes Input/Output

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Introduction to C++

Chapter 2 Basic Elements of C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CHAPTER 3 Expressions, Functions, Output

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

The C++ Language. Arizona State University 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

C++ Quick Guide. Advertisements

CSCI 1061U Programming Workshop 2. C++ Basics

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

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

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

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

2 nd Week Lecture Notes

Programming. C++ Basics

Expressions, Input, Output and Data Type Conversions

Lecture 4 Tao Wang 1

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Looping. Arizona State University 1

Chapter 7. Additional Control Structures

I/O Streams and Standard I/O Devices (cont d.)

C++ Input/Output: Streams

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Chapter 2 - Control Structures

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

BASIC ELEMENTS OF A COMPUTER PROGRAM

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CS201 Some Important Definitions

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

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

Chapter 2 - Control 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.

Window s Visual Studio Output

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

CS3157: Advanced Programming. Outline

Chapter 2 - Control Structures

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Chapter 1 Introduction to Computers and C++ Programming

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

Reserved Words and Identifiers

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Lecture 2 Tao Wang 1

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

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Chapter 2. Outline. Simple C++ Programs

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

EEE145 Computer Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2. Lexical Elements & Operators

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.

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Engineering Problem Solving with C++, Etter/Ingber

ANSI C Programming Simple Programs

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

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

1 Lexical Considerations

CSC Web Programming. Introduction to JavaScript

Study recommendations for the Mid-Term Exam - Chapters 1-5

Getting started with C++ (Part 2)

CS102: Variables and Expressions

Typescript on LLVM Language Reference Manual

Fundamental of Programming (C)

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Topic 2: C++ Programming fundamentals

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Full file at C How to Program, 6/e Multiple Choice Test Bank

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Creating a C++ Program

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

CHAPTER 3 BASIC INSTRUCTION OF C++

Transcription:

Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object code Linker takes object code and produces executable code. Language Elements: keywords, identifiers, operators, punctuation, syntax Literals character (char/string) or numeric (integer/floating point). c (char), c (char string), 10 (integer), 12. & 16.0 (floating point [double]) A numeric literal without a decimal point is an int. A numeric literal with a decimal point is a double. true and false are reserved literals of the bool data type Variable Declarations ( char, int, float, double, bool ) <data type> <name> [ = value ] ; //initialization is optional Examples: int x; int y = 10; double length = 5.0, double width; The three primary activities of a program are input, processing, and output Programming Paradigms: Procedural, Object-oriented, Event-driven ( using an OOP language ) Chapter 2 Preprocessor Directives: #include All preprocessor directives begin with a # preprocessor directives are NOT C++ statements Function main template: int main ( ) <statements> return 0; Punctuation Characters: // /* */ # <> ( ) ; cout object << (stream insertion operator) separate statements / cascaded statement Common Escape Sequences: \n (newline) \t (tab) variables and literals: X X X 5 5.0 c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Rules for Identifiers: legal variable names first character upper or lower case character a-z or A-Z remaining characters, a-z, A-Z, 0-9, _ (underscore) practical limit is 31 characters Integer data types: short, int, long ( modifier unsigned may be used with integer data types ) Characters: A 65 (ascii code) character (char) is a subtype of integer (int) the ordinal value of a character in the ASCII character set is stored as an integer Character string Null character \0 Floating-point data types: float, double, long double bool data type: true false sizeof( ) operator sizeof ( int ) sizeof ( GrossPay ) Variable declaration / assignment Arithmetic operators: + - * / % unassigned garbage value Distinguish between integer and floating point division: 3/5 3.0/5 Remember that the % operator returns the integer remainder of a division

Chapter 3 cin object >> (stream extraction operator) Using a single statement / cascaded operators Whitespace: spaces, tabs, newlines Reading strings: cin versus cin.get( ) versus cin.getline( ) Char Arrays: char Array[21] size / room for Null character \0 Array size declarator must be an integer named constant or integer literal Array bounds - cin can accept more characters than the array can hold! Array bounds are not checked Mathematical expressions operator precedence ( ) * / % in order from left to right + in order from left to right Type conversion: mixed data types in an expression promote to highest type found Type Casting: C style casting: ( int ) x C++ typecast: int ( x ) ANSI standard C++: static_cast<int>(x) Constants: const keyword: const double PI = 3.14159; preprocessor directive: #define PI 3.14159 Combined Assignment Operators: +=, -=, *=, /=, %= Output formatting: #include<iomanip> stream manipulators fixed, showpoint, setprecision( ), setw( ), left, right cin member functions cin.get( ), cin.getline( ), cin.ignore( ) Output mode: automatic / fixed / scientific default output mode is automatic default precision is 6 automatic mode (significant digits ---> integer and decimal positions) fixed mode (number of decimal positions) setw( ) stream manipulator width of output field, right-justifies setprecision( ) set number of significant digits or decimal digits ( if used with fixed ) fixed change mode to fixed ( change meaning of setprecision to decimal digits ) showpoint left / right Formatted input: cin >> setw( 8 ) >> X; cin.getline( array, size ) Math library functions: #include <cmath> Math library constants M_PI and M_E pseudorandom numbers: rand( ), srand( ) seed value --> srand( time( NULL ) ) rand( ) % x --> returns numbers in the range 0.. x-1 1 + rand( ) % x --> returns numbers in the range 1.. x

Chapter 4 Relational operators: < <= > >= = =!= Evaluate to true ( 1 ) or false ( 0 ) what is true? Any expression that evaluates to non-zero is considered "true". if ( -5 ) expression 5 is considered true (its non-zero) if statement: if ( expression ) if ( expression ) statement; statements if/else statement: if ( expression-1 ) // don t forget the ; here! else statement-2; if/else if statement ( case structure ): if ( expression-1 ) if ( expression-1 ) else or else if (expression-2) if (expression-2) statement-2; statement-2; else if // two words! else if else if. Trailing else --- default case. Statement executed when all of the conditions are false. Nested if statement: if ( expression-1 ) if (expression-2) statement-2; Logical operators:! (not) && (and) (or) Connect two or more relational expressions if ( weight > 0 && weight <= 20 ) correct-weight-statement; if ( weight <= 0 weight > 20 ) weight-error-statement; // test to see if a value is within a specific range // test to see if a value is outside a specific range Precedence is 1:! (not) 2: && (and) 3: (or) Variable declarations and scope block The scope of a variable is the block in which it is declared. Variables with the same name ( nested blocks ) conditional operator: test-expression? t-expression : f-expression; x < 0? y = 10 : z = 20; if ( x < 0 ) y = 10; else z = 20; Note: << has higher precedence than?: (conditional) operator put conditional expression in ( ) cout << ( (num % 2 == 0)? Even\n : Odd\n ); char data type a single character enclosed in single quotes: a, b, \n, \t [escape sequences are a single character] string class #include<string> string description; string name = Hector supported operators: [ ], relational, comparison, = (assignment)

switch statement: switch ( integer-expression ) case constant-integer-expression : statement(s) ; break (optional) ; default (optional) : statements; Checking for file open errors if (! infile ) if ( infail.fail( ) ) //integer or character literal stream extraction operator returns a value of true or false if ( inputfile >> number ) can be used to control a loop while ( inputfile >> number ) // returns true as long as you are not at the end of the file Note: can be used with cin object [ while ( cin >> number ) ] // returns true while not EOF ( Ctrl-D or Ctrl-Z ) increment and decrement operators: ++, prefix and postfix modes / using in expressions ++val (1. increment 2. use in expression), val (1. decrement 2. use in expression) Chapter 5 val++ (1. use in expression, 2. increment) val (1. use in expression, 2. decrement A loop is a block of code that is repeated while some condition is true or until some condition is true. For any loop, there are three types of expressions which may be necessary initialization-expression(s) used to initialize a loop control variable and any other variables that need to be initialized before executing a loop text-expression used to determine whether or not the body of the loop should be executed update-expression(s) used to update a loop control variable ( if present ) while and do...while loops only require a test-expression. A For loop does not require any of these expressions. while loop [ pre-test loop ] while (expression) statement; executes while expression is non-zero body of loop executed 0 or more times while (expression) statement-n; counters ( 228), accumulators, running totals (231) sentinel a special value that marks the end of a list of values. Used to satisfy the condition necessary to terminate a loop. A sentinel value is outside the range of normal expected values. while ( Emp_no!= -1 ) loop body

do-while loop [ post-test loop ] do statement; while (expression); do-while loops are often used with menus body of loop executed at least once do statement-n; while (expression); for loop [ pre-test loop ] for ( initialization ; test ; update ) statement; for ( initialization ; test ; update) statement-2; statement-n; only thing required in the parentheses is the semicolons ( ; ; ) initialization is performed once test is performed every iteration of the loop update is performed every interation of the loop after the body of the loop has been executed pre-test loop - executed 0 or more times may declare local variable(s) in the for loop header may have multiple expressions separated by commas in the for loop header for ( int x=2, y=3 ; x*y < =100 ; x++, y++ ) cout << setw(5) << x << setw(5) << y << endl; Nested loops for ( int j=0 ; j < 10 ; j++ ) for ( int k=0 ; k<10 ; k++) cout << j << \t << k << \t << j+k << endl; break keyword used to terminate a loop early while (1) if ( x = = 10 ) break; continue keyword - causes a loop to stop its current iteration and begin the next one while (1) if ( x = = 10 ) continue; using a loop for input validation cout << Enter a number in the range of 1 100: ; cin >> Number; while ( Number < 1 Number > 100 ) cout << Please enter a number in the range of 1 100: ; cin >> Number;

File Input and Output: 3-step process: 1. open the file 2. process the file (read/write) 3. close the file use of file stream objects requires the fstream header file #include<fstream> File Stream Classes: ifstream file used for input (read only) ofstream file used for output (write only) fstream file used for input, output or both Declaration of a File Stream Object: ifstream Infile; ofstream outfile; Use of the Open( ) Member Function: infile.open( customer.dat ); outfile.open( info.dat ); use of a backslash in a path for a file specification OutputFile.open( a:\\files\\invtry.dat ); // requires two backslashes closing a file OutputFile.close( ); writing to a file using the stream insertion operator << outfile << I love C++ programming ; using the stream extraction operator to read information from a file >> InputFile >> Emp_No; InputFile >> FirstName; remember the stream extraction operator stops reading when it encounters any whitespace character ( space, tab, newline )