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