Download presentation
Presentation is loading. Please wait.
Published byJesse Jacobs Modified over 9 years ago
1
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15
2
C++ TOKENS The smallest individual unit of a program written in any language. Three types Special symbols Word symbols Identifiers
3
TYPEOFTOKENSTYPEOFTOKENS SPECIAL SYMBOLS +-*/.;?, <=!===>=
4
TYPEOFTOKENSTYPEOFTOKENS RESERVED WORDS (keywords) KeywordMeaning 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
5
TYPEOFTOKENSTYPEOFTOKENS IDENTIFIERS NameMeaningstd 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.
6
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
7
//basics.cpp //This is to show some basic elements of C++ //The following two lines are preprocessor directives #include 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 semicolon return 0; } //The main() finishes here with this right curly bracket //basics.cpp //This is to show some basic elements of C++ //The following two lines are preprocessor directives #include 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 semicolon return 0; } //The main() finishes here with this right curly bracket SAMPLE PROGRAM OUTPUT Hello world!
8
C++ Data Types Computer uses different # of digits to store different types of data
9
C++ Data Types (Integral)
10
C++ Data Types (Floating point)MemoryRange Significant digits Precisionfloat 4 Bytes -3.4E+38 and 3.4E+38 6 or 7 Single double 8 Bytes -1.7E+308 and 1.7E+308 15Double
11
+ addition - subtraction * multiplication / division % modulus (remainder) + addition - subtraction * multiplication / division % modulus (remainder) ARITHMETIC OPERATORS
12
*, /, And % have higher level of precedence than + and – No precedence within the same level Parentheses have highest precedence *, /, And % have higher level of precedence than + and – No precedence within the same level Parentheses have highest precedence ORDER OF PRECEDENCE
13
INPUT (cin) STATEMENT Put data into variables from the standard input devices (keyboard in most cases) Syntax cin >> variableOne >> variableTwo …; Variable must be declared first #include Put data into variables from the standard input devices (keyboard in most cases) Syntax cin >> variableOne >> variableTwo …; Variable must be declared first #include
14
OUTPUT Syntax cout << expression or manipulator << …; Expression will be evaluated before printing Manipulator is used to format the output Escape sequences (try \7 )
15
Increment and Decrement Operators (Optional) ++ -- These three statements are equivalent –count = count + 1; –count++; //post-increment –++count; //pre-increment ++ -- These three statements are equivalent –count = count + 1; –count++; //post-increment –++count; //pre-increment
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.