C++ First Steps.

Slides:



Advertisements
Similar presentations
 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.
Advertisements

A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
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.
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
© by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
C++ Programming: Presentation 1
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Course websites CS201 page link at my website: Lecture slides
Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

C++ First Steps

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

defining a set of valid values and operations Data Type 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

Data Type 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

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

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

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

a data value with an implied data type Categories of Words Literals a data value with an implied data type Examples: 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.

Categories of Words 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.

Categories of Words 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

Categories of Words 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)

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

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

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

Categories of Words 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.

Categories of Words 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”.

Categories of Words 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.

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

Syntax Semantics Style The Three S’s When learning new statements in C++, we will discuss three major aspects: Syntax Semantics Style

Rules defined by the language for writing statements The Three S’s Syntax 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.

Semantics The meaning of a statement: what it does. The Three S’s Semantics The meaning of a statement: what it does.

Violation of these rules has no effect on the program. The Three S’s Style Rules for writing statements that make the code easier for a programmer to understand Violation of these rules has no effect on the program.

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

Warning to the Snakes! The Three S’s (Python programmers) Indentation, spacing, and end-of-line affect the Syntax and Semantics of statements in Python In C++ THEY DO NOT!**

Define Before Use 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()) Violation of this rule results in the syntax error “identifier undefined”

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

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

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

First C++ Program // my first program #include <iostream> 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.

First C++ Program // my first program #include <iostream> using namespace std; 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".

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

First C++ Program // 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 int main() line, or on a line by itself.

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

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

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

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

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

First C++ Program // my first program #include <iostream> using namespace std; int main() { cout << "Hello world!\n"; system("pause"); return 0; } 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 Conclusion - Data Type - Categories of Words - The Three S’s - Define Before Use - End of Statement - My First Program

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

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