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

Similar documents
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

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

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

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

Your First C++ Program. September 1, 2010

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

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

Chapter 2: Introduction to C++

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

CSc Introduction to Computing

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

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

CSI33 Data Structures

Objectives. In this chapter, you will:

LECTURE 02 INTRODUCTION TO C++

Lecture 2 Tao Wang 1

Fundamentals of Programming Session 4

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

Chapter 2 Basic Elements of C++

Introduction to Programming

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

2 nd Week Lecture Notes

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

UEE1302 (1102) F10: Introduction to Computers and Programming

Computer Programming : C++

Chapter 1 Introduction to Computers and C++ Programming

Step by step set of instructions to accomplish a task or solve a problem

BITG 1233: Introduction to C++

Chapter 2 C++ Fundamentals

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

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

C: How to Program. Week /Mar/05

CHAPTER 3 BASIC INSTRUCTION OF C++

A First Program - Greeting.cpp

PYTHON. Values and Variables

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

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

VARIABLES & ASSIGNMENTS

Chapter 2 - Introduction to C Programming

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

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

Getting started with C++ (Part 2)

Chapter 2: Overview of C++

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

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

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

The C++ Language. Arizona State University 1

Programming for Engineers Introduction to C

Programming with C++ as a Second Language

IT 374 C# and Applications/ IT695 C# Data Structures

Understanding main() function Input/Output Streams

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

6.096 Introduction to C++ January (IAP) 2009

BTE2313. Chapter 2: Introduction to C++ Programming

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

Introduction to C++ IT 1033: Fundamentals of Programming

Maciej Sobieraj. Lecture 1

C++ Support Classes (Data and Variables)

CS242 COMPUTER PROGRAMMING

Chapter 2. C++ Basics

Introduction to C# Applications

CHAPTER-6 GETTING STARTED WITH C++

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

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

Creating a C++ Program

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

Chapter 2, Part I Introduction to C Programming

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

4. Structure of a C++ program

Chapter 1 Introduction to Computers and Programming

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

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

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

Unit 3. Constants and Expressions

Chapter Two MULTIPLE CHOICE

CSCI 1061U Programming Workshop 2. C++ Basics

BASIC ELEMENTS OF A COMPUTER PROGRAM

Program Organization and Comments

UNIT- 3 Introduction to C++

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.

Fundamental of Programming (C)

Some Computer Preliminaries

Getting Started Values, Expressions, and Statements CS GMU

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

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

Chapter 4 - Notes Control Structures I (Selection)

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

UNIT-2 Introduction to C++

Problem Solving With C++ Ninth Edition

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

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Engineering Problem Solving with C++, Etter/Ingber

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

Transcription:

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

a description of data, defining a set of valid values and operations List of General Data Types: - Integer: numbers without a decimal point - Float: numbers with a decimal point - Character: a single character - String: a sequence of 0 or more characters

Q: What is meant by defines operations? A: a Data Type defines which operators may be used on values of the type and how they work. 2 + 3 result 5 definition of + on integers '2'+'3' result 101 definition of + on characters

The words and symbols found in a C++ program fall into the following categories: - Reserved Words (Keywords) - Identifiers - Literals - Comments - Compiler Directives

Reserved Words (Keywords) Words that already have a defined meaning in C++ including operators and symbols Ex: + * / < <= << >> ( [

Identifiers Words that are created and defined by the programmer. Rules: - Must start with a letter - The remainder may be letters, digits or the underscore (_) - Note: that means no spaces - Identifiers are Case Sensitive

Examples: Literals a data value with an implied data type 5 integer literal: digits with no decimal point 5.0 float literal: digits with a decimal point '1' character literal: exactly one character between single quotes. "a 12.0" string literal: 0 or more characters between double quotes. Must be on one line.

Escape Characters The ASCII Table defines the binary codes for 256 possible characters. Some characters are not found on a keyboard but are needed in some programming situations. C++ provides Escape Characters for specifying such character literals.

Escape Characters Inside quotation marks (literals), Escape Characters are identified by a backslash \ followed by a single character The single character indicates which special character Together these two characters are interpreted as one character

Escape Characters \n Newline (go down to the next line) \t Tab \ Single Quote (needed inside a char literal) \ Double Quote (needed inside a string literal) \\ Backslash (there are more)

Escape Characters cout << "Hello\t\"World\"\n\n\\BS\\\n"; cout << "Hello\t\"World\"\n\n\\BS \n"; Prints:

Comments Two forms: Block Comment: anything between /* and */ May include multiple lines. Line Comments: anything after // to the end of the line

/* this is a block comment */ Comments /* This is also a block comment */ /* * traditionally more like this */ // this is a line comment

Compiler Directives Purpose: to direct the compiler to do something with the Source code before translation begins. Start with # followed by a pre-defined word (no space), usually followed by something more.

Compiler Directives Example: #include <iostream> #include instructs the compiler to copy/paste a file into the Source code. It is followed by the name of the file. When the file name is between < arrows >, this means look in the C++ standard library.

Compiler Directives Example: #include <iostream> iostream is a C++ library that defines cout, cin, the system() function and many other things. It must be included in any source file that uses those items defined in it.

Compiler Directives Compiler Directives are usually placed at the top of a source file (not considering comments).

When learning new statements in C++, we will discuss three major aspects:

Rules defined by the language for writing statements Violation of these rules causes a Syntax Error and the compiler is unable to translate the code.

The meaning of a statement: what it does.

Rules for writing statements that make the code easier for a programmer to understand Violation of these rules has no effect on the program.

Examples: - Writing meaningful comments - Creating descriptive identifier names - Indentation and spacing - Breaking a statement up over multiple lines (or not).

(Python programmers) Indentation, spacing, and end-of-line affect the Syntax and Semantics of statements in Python In C++ THEY DO NOT!**

Violation of this rule results in the syntax error identifier undefined When compiling a source file, C++ begins with the first line and proceeds down, left to right. Any identifier must be defined before it can be used, including those defined in standard libraries. Example: #include<iostream> must be above the use of any identifiers it defines (ex: cout, system())

In C++ the semicolon is used to indicate End of Statement

// my first program #include <iostream> using namespace std; int main() { cout << "Hello world!\n"; system("pause"); return 0; }

#include <iostream> using namespace std; int main() { cout << "Hello world!\n"; system("pause"); return 0; } Comment - has no effect on the program

// my first program using namespace std; int main() { } cout << "Hello world!\n"; system("pause"); return 0; Compiler Directive - this program uses the iostream library in C++. It is needed to define the word cout.

// my first program #include <iostream> int main() { } cout << "Hello world!\n"; system("pause"); return 0; Instructs the compiler to use the "standard namespace". Without this, cout and other words would be "undefined".

// my first program #include <iostream> using namespace std; } { cout << "Hello world!\n"; system("pause"); return 0; main() - beginning of the program instructions. Execution always starts here.

// my first program #include <iostream> using namespace std; int main() cout << "Hello world!\n"; system("pause"); return 0; Begin and End of main()'s block. Note the may be on the end of the line, or on a line by itself.

// my first program #include <iostream> using namespace std; int main() { } << "Hello world!\n"; system("pause"); return 0; cout is used to print on the screen in a Console Application

// my first program #include <iostream> using namespace std; int main() { } cout system("pause"); return 0; "Hello world!\n"; The operator is used to separate individual pieces of data to be printed. Here, only one string literal is printed.

// my first program #include <iostream> using namespace std; int main() { } cout << ; system("pause"); return 0; A String Literal begins and ends with a double quote. This is literally what is printed on the screen.

// my first program #include <iostream> using namespace std; int main() { cout << "Hello world! "; system("pause"); return 0; } The makes the cursor go down to the next line after printing the

// my first program #include <iostream> using namespace std; int main() { cout << "Hello world!\n"; } return 0; This is a request to the OS (Windows) to execute the pause command, which prints " " then waits for the user to respond.

// my first program #include <iostream> using namespace std; int main() { cout << "Hello world!\n"; system("pause"); } return 0; causes the program to exit main(), and so exits the program. It should always be the last line in main().

- Data Type - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program

Term Data Type Integer Float Character String Reserved Word Keyword Identifier Literal Comment Compiler Directive Definition A description of data defining valid values and operations A number without a decimal point A number with a decimal point A single character (letter, numeral, symbol) A sequence of 0 or more characters Word (or symbol) that has a meaning already defined in the language (same as Reserved Word) Word created and defined by the programmer Word (or symbol) that is a data value with an implied type Programmer notes in the source that are not translated Word(s) beginning with # that instruct the compiler to manipulate the source code before translating.

Term Syntax Semantics Style Definition Rules for writing programs as defined by the language; violation causes a Syntax Error and the program cannot be compiled (translated) Meaning of a statement; what it does Rules for writing statements that make a program clearer to a programmer; violation has no effect on the translation or execution of the program