Your First C++ Program. September 1, 2010

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

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

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Introduction to C++

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

4. Structure of a C++ program

Understanding main() function Input/Output Streams

Introduction to Programming

Chapter 2: Overview of C++

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

Computer Programming : C++

Chapter 1 Introduction to Computers and C++ Programming

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

2 nd Week Lecture Notes

Chapter 1 Introduction to Computers and Programming

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

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

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

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

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

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

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

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

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

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to C++ IT 1033: Fundamentals of Programming

Chapter 2, Part I Introduction to C Programming

download instant at Introduction to C++

Creating a C++ Program

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

The C++ Language. Arizona State University 1

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

Programming with C++ as a Second Language

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

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

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

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Practice test for midterm 1

Objectives. In this chapter, you will:

Chapter 1: Why Program? Computers and Programming. Why Program?

Tutorial 2: Compiling and Running C++ Source Code

6.096 Introduction to C++ January (IAP) 2009

Chapter 1 INTRODUCTION

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter Two MULTIPLE CHOICE

A First Program - Greeting.cpp

Chapter 4 - Notes Control Structures I (Selection)

Fundamentals of Programming CS-110. Lecture 2

Your first C++ program

Chapter 2. C++ Basics

UEE1302 (1102) F10: Introduction to Computers and Programming

CSCI 1061U Programming Workshop 2. C++ Basics

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Chapter 2. Lexical Elements & Operators

Getting started with C++ (Part 2)

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

Tutorial-2a: First steps with C++ programming

C++ Support Classes (Data and Variables)

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Lecture 2 Tao Wang 1

Chapter 2 Basic Elements of C++

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Overview of C++ Chapter Objectives

Program Organization and Comments

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

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Ch 1. Algorithms and Basic C++ Programming

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

CSI33 Data Structures

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

C++ For Science and Engineering Lecture 2

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

A SHORT COURSE ON C++

Programming with C++ Language

VARIABLES & ASSIGNMENTS

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

UNIT- 3 Introduction to C++

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

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

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

Chapter 2 C++ Fundamentals

Getting Started with Visual Studio

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

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

Transcription:

Your First C++ Program September 1, 2010

Your First C++ Program //*********************************************************** // File name: hello.cpp // Author: Bob Smith // Date: 09/01/2010 // Purpose: This program displays a welcome message to // the user //*********************************************************** #include <iostream> #include <string> using namespace std; int main() { string name; cout << "Type your name, then press enter " << endl; cin >> name; cout << "Hello " << name << "! " << endl; } return 0; Program Output: Type your name, then press enter Doug Hello Shereen! 2

Language Elements Key Words o Have special meaning in C++ o using namespace int Programmer-Defined Identifiers o Names made up by the programmer o name Operators o Perform operations o * = Punctuation o Used to mark the beginning and end of the program o ; 3

Syntax (Grammar) Rules that must be followed when constructing a program Controls the use of key words, programmerdefined identifiers, operators, and punctuation 4

Program Components The C++ program on the previous slide consists of the following elements: o Comments o Preprocessor directives o Standard namespace o main function o Declaration statements o Executable statements 5

Program Skeleton All programs in C++ should have the following skeleton //*********************************************************** // File name: filename.cpp // Author: Your Name // Date: 09/01/2010 // Purpose: Description about what the program does //*********************************************************** #include <iostream> #include <string> using namespace std; int main() { // declaration statements // executable statements } return 0; 6

Comments Comments are o How you explain in English what the different parts of your program do o Ignored by the compiler o Very important for you and me The editor in Visual Studio will color code your comments. They will be green 7

Comments There are two ways to write comments o // I am a comment Anything after // to the end of the line will be a comment o /* I am another comment */ You must start the comment with /* and end it with */ in this style of comment 8

Preprocessor directives #include <iostream> #include <string> These need to appear at the beginning of every program that you write Includes C++ libraries into your program 9

Namespace std using namespace std; The statement appears in all our programs 10

main Function int main() { // program statements return 0; } Where the start of your program execution begins return 0; ends the main function and indicates that the program terminated successfully Everything within the double braces {} should be indented o Style! 11

Program Statements There are two types of statements that you can write inside the main (or any other) function o Declaration statements Specify the data that is needed by the program o Executable statements Perform operations All statements must end with a semicolon; 12

Program Statements Declaration statements string name; Executable statements cout << Type your name, then press enter << endl; cin >> name; cout << Hello << name <<! << endl; 13

Executable Statements cout Object 14

cout Object cout object is the standard output object The monitor is the standard output device cout is a stream object and works with streams of data o Streams of characters o Streams? 15

cout Object Output operator (insertion operator): << Standard output (monitor screen): cout cout << Hello out there! ; o right operand string literal variable 16

cout Object What is the output? cout << Type your name, then press enter << endl; endl will move the cursor to a new line Statement must end in a semicolon 17

cout Object Other ways of outputting the same message cout << Type your name, << then press enter << endl; cout << Type your name, ; cout << "then press enter << endl; Everything will output to the same line unless you specify otherwise 18

cout Object Separate components with << int kms; kms = 4; cout << Enter the distance in miles << endl; cout << The distance in kilometers is << kms << endl; Don t break string literals across a line cout << Type your name, then press enter << endl; o Compiler error! 19

Problem What is the output? cout << My name is: ; cout << Doe, Jane. << endl; cout << I live in ; cout << Ann Arbor, MI ; cout << and my zip code is << 48109 <<. << endl; 20

Escape Characters These are special characters that can be output o part of a string literal They are always preceded by a backslash \ Examples of escape characters include: o \n: new line: equivalent to endl o \r: moves the cursor to the beginning of the current line o \t: moves the cursor to the next tab stop o \\: displays the backslash o \ : outputs the double quotes o \a: outputs a beep! 21

Examples What is the output? cout << This is a C++ program\n ; cout << This is a \nc++ program ; cout << \ This is a C++ program\ ; cout << This is a\tc++\tprogram ; 22

Summary Today we covered The basic components of a program Program skeleton cout Object Next time Data types Identifiers Completed sections 2.1-2.3, 2.14 23