the basics of a c++ program edp 4 / math 23 tth 5:45 – 7:15

Post on 04-Jan-2016

221 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

THE BASICS OF A C++ PROGRAM

EDP 4 / MATH 23TTH 5:45 –

7:15

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

in any language.

Three types Special symbols Word symbols Identifiers

TYPE

OF

TOKENS

SPECIAL SYMBOLS

+ - * /

. ; ? ,

<= != == >=

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

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.

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

//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!

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

store different types of data

C++ Data Types (Integral)

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

+ addition

- subtraction

* multiplication

/ division

% modulus (remainder)

ARITHMETIC OPERATORS

*, /, And % have higher level of precedence

than + and –

No precedence within the same level

Parentheses have highest precedence

ORDER OF PRECEDENCE

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>

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

• Expression will be evaluated before printing

• Manipulator is used to format the output

• Escape sequences (try \7)

Increment and Decrement Operators (Optional)

• ++ --

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

– count++; //post-increment

– ++count; //pre-increment

top related