Introduction to C++ Chapter

Similar documents
Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Understanding main() function Input/Output Streams

4. Structure of a C++ program

Chapter 2: Introduction to C++

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

CHAPTER 3 BASIC INSTRUCTION OF C++

LECTURE 02 INTRODUCTION TO C++

Your First C++ Program. September 1, 2010

Introduction to C++ IT 1033: Fundamentals of Programming

Chapter 2 Basic Elements of C++

Streams. Ali Malik

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

C++ Support Classes (Data and Variables)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

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

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

VARIABLES & ASSIGNMENTS

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Computer Programming. Dr. Fahad Computer Science Deptt.

2 nd Week Lecture Notes

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Tutorial 2: Compiling and Running C++ Source Code

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

Computer Programming : C++

Chapter 1 INTRODUCTION

St. Edmund Preparatory High School Brooklyn, NY

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

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

Lesson 1: Hello, world! Line by line explanation

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.

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

UNIT- 3 Introduction to C++

Practice test for midterm 1

Types, Values, Variables & Assignment. EECS 211 Winter 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++

Topics. Functions. Functions

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

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Dr. Md. Humayun Kabir CSE Department, BUET

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C++ For Science and Engineering Lecture 2

Chapter 17 - Notes Recursion

A First Program - Greeting.cpp

download instant at Introduction to C++

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Computer Programming

Chapter 1 - What s in a program?

Increment and the While. Class 15

Programming with C++ as a Second Language

WARM UP LESSONS BARE BASICS

Security Coding Module - Buffer Overflow Data Gone Wild CS1

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

BITG 1233: Introduction to C++

Objectives. In this chapter, you will:

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Computer Science II Lecture 1 Introduction and Background

CSCI 1061U Programming Workshop 2. C++ Basics

Introduction to C++ (Extensions to C)

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

Getting started with C++ (Part 2)

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 4 - Notes Control Structures I (Selection)

Chapter 2: Overview of C++

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

Exception with arguments

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Your first C++ program

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

PIC 10A Objects/Classes

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

Programming. C++ Basics

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

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

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

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

do { statements } while (condition);

CSE 303: Concepts and Tools for Software Development

Programming Language. Functions. Eng. Anis Nazer First Semester

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

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Scientific Computing

UEE1302 (1102) F10: Introduction to Computers and Programming

Fundamentals of Programming CS-110. Lecture 2

1 Unit 8 'for' Loops

Exercise 1.1 Hello world

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

C++ Programming for Non-C Programmers. Supplement

Introduction to Programming using C++

Transcription:

Topics Introduction to C++ Chapter 2.1-2.2 1) What does a simple C++ program look like? 2) How can we output text to the screen? 3) What kind of errors will we see? CMPT 125 / 128 Brian Fraser 07/05/11 1 07/05/11 2 Hello World! A simple C++ program. Output 07/05/11 3 07/05/11 4

Comments: All text on a line after a // is a comment. These are notes to the programmer; #include: Tells the compiler that we may use the keyboard or the screen in our program. This is called a The file iostream is included for our use. 07/05/11 5 07/05/11 6 using namespace: All identifiers (such as variable and function names) are inside a namespace. int main() {... : Creates a the main() function. The main() function is Basically, this states that we want to use identifiers in the std namespace. Functions are named collection of statements. Note: C++ is case sensitive! main() is different than Main() or MAIN()! 07/05/11 7 07/05/11 8

int is the return type. In this case, an integer. It is the type of information the program "returns" to the OS. main is the name of our function. Each program we create must The () indicates this is... The {... indicates a block. In this case, a block of statements associated with the main() function. cout: Think of << as sending the string to cout. (cout = character out). Completed statements end with a semicolon. We will later get a better feel for this. For now, just concentrate on the parts of the program. 07/05/11 9 07/05/11 10 return: The return statement in the main() function returns a value to the operating system. Returning 0 to the OS indicates success (by convention). Output 07/05/11 11 07/05/11 12

Review Visual C++ IDE 1) What C++ statement prints "I love programming" to the screen? 2) What is the name of the function which runs when a program starts? 3) Is C++ case sensitive? 07/05/11 13 07/05/11 14 cout cout: The cout Object Think of it as character out, or console out. cout is a stream object: It operates on a stream (sequence) of characters. << is the stream-insertion operator: Use it to push text into cout cout << "Wow! Programming is fun!"; Think of << as an arrow point to the left: cout "Wow! Programming is fun!" 07/05/11 15 07/05/11 16

Multiple Strings Common Problem You can send multiple different strings to cout: What is the problem with the following? // Displaying multiple strings. cout << "Programming is " << "great fun "; cout << "all the time!"; Notice all the strings are run together, even though they are from separate statements. // Demonstrate a common problem cout << "My favourite numbers are: "; cout << "0"; cout << "42"; cout << "73"; 07/05/11 17 07/05/11 18 Line Feeds Special Characters Can put line feeds in with either: End Line Stream Manipulator: endl cout << "First line." << endl; cout << "Second." << endl << "Third."; New Line Character: "\n" cout << "First line.\n"; cout << "Second.\n" << "Third."; Escape Sequences: New line: "One \n on \n top" Tabs (line up): "Age: \t" A \ character: "Up \\ down" A ' character: "I\'m lovin\' programming!" A " character: "I said, \"Yes!\" too" Note that the escape sequence must be inside a string, whereas endl must not be in the string. 07/05/11 19 07/05/11 20

Escape Sequence Example // Demonstrate escape sequences and endl cout << "Movie Lineup\n"; cout << "7:30\tSpace Balls" << endl; cout << "10:40\tIt\'s a Wonderful Life" << endl; cout << "12:30\tGone with the Wind"<<endl<<endl; cout << "He'll say, \"They\'re great!\"\n"; Spot the Mistakes // Show some easy mistakes. // Spot the mistakes: cout << "C++ is fun! endl"; cout << "Computers are awesome!" << \n; cout << "Amazing stuff!/n"; cout << "I say "Yeah!"" << endl; 07/05/11 21 07/05/11 22 Review 1) Write one or more C++ statements which output the following (including tabs, and line-feeds): Name: Fav-Colour: "Brian" Green Errors 07/05/11 23 07/05/11 24

Errors Errors To err is human, but to really foul things up you need a computer. Paul Ehrlich 1982: Bug in software controlling Soviet pipeline causes largest manmade non-nuclear explosion in history. Compile Error Syntax errors, such as forgetting a ; Semantic errors, such as invalid type casting. Run-time Error Errors causing... such as an un-checked divide by zero (exceptions). Logical Error Caused by programmer error (bug). 07/05/11 25 07/05/11 26 Summary Simple program: "Hello world!" Output to the console with cout. cout<<"one "<<"Two"; cout<<"with 2 line feeds\n"<<endl; Escape Sequence: \n, \t, \\, \', \" 3 types of errors: Compile, run-time, logical. 07/05/11 27