17
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

Embed Size (px)

Citation preview

Page 1: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

THE BASICS OF A C++ PROGRAM

EDP 4 / MATH 23TTH 5:45 –

7:15

Page 2: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

C++ TOKENS The smallest individual unit of a program written

in any language.

Three types Special symbols Word symbols Identifiers

Page 3: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

TYPE

OF

TOKENS

SPECIAL SYMBOLS

+ - * /

. ; ? ,

<= != == >=

Page 4: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

TYPE

OF

TOKENS

RESERVED WORDS (keywords)Keyword Meaning

include Specify the header file to be included by the preprocessor

using Specify the namespace in the header file where the identifiers are declared namespace

int Integer data type

return Return a value to where the current function is called

Page 5: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

TYPE

OF

TOKENS

IDENTIFIERS

Name Meaning

std Where cin and cout are declared in iostream header file

main Every C++ program must have a main()

cout A stream output object that can be used to display something on screen

These are all predefined identifiers from the standard library. But you can define your own identifiers.

Page 6: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

If a token is made of more than two characters, no spaces are allowed among these characters.

You are not allowed to use keywords for other purposes, e.g. naming your own variable int

A C++ identifiers can have letters, digits, and underscore (_) and cannot begin with a digit.

C++ is case-sensitive!!!!!!!!!!!!!

Use meaningful identifiers

C++ Tokens

Page 7: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

//basics.cpp//This is to show some basic elements of C++//The following two lines are preprocessor directives#include <iostream>using namespace std;

/*Here begins the main() function Every C++ program has a main() function*/int main() {

cout << "Hello world!\n\n"; //Every C++ statement ends //with a semicolonreturn 0;

} //The main() finishes here with this right curly bracket

SAMPLE PROGRAM

OUTPUT

Hello world!

Page 8: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

C++ Data Types• Computer uses different # of digits to

store different types of data

Page 9: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

C++ Data Types (Integral)

Page 10: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

C++ Data Types (Floating point)

Memory Range Significant digits

Precision

float 4 Bytes -3.4E+38 and 3.4E+38

6 or 7 Single

double 8 Bytes -1.7E+308 and 1.7E+308

15 Double

Page 11: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

+ addition

- subtraction

* multiplication

/ division

% modulus (remainder)

ARITHMETIC OPERATORS

Page 12: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

*, /, And % have higher level of precedence

than + and –

No precedence within the same level

Parentheses have highest precedence

ORDER OF PRECEDENCE

Page 13: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

INPUT (cin) STATEMENT

• Put data into variables from the standard input devices (keyboard in most cases)

• Syntaxcin >> variableOne >> variableTwo …;

• Variable must be declared first• #include <iostream>

Page 14: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

OUTPUT• Syntaxcout << expression or manipulator << …;

• Expression will be evaluated before printing

• Manipulator is used to format the output

• Escape sequences (try \7)

Page 15: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15

Increment and Decrement Operators (Optional)

• ++ --

• These three statements are equivalent– count = count + 1;

– count++; //post-increment

– ++count; //pre-increment

Page 16: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15
Page 17: THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15