using namespace std; int main () { cout << "Hello world!\n"; return 0; } C++ - style comments. Everything between the // and the end of line is ignored by the compiler."> using namespace std; int main () { cout << "Hello world!\n"; return 0; } C++ - style comments. Everything between the // and the end of line is ignored by the compiler.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout <<

Similar presentations


Presentation on theme: "A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout <<"— Presentation transcript:

1 A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; }

2 A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } C++ - style comments. Everything between the // and the end of line is ignored by the compiler.

3 A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } #include is a preprocessor directive. Preprocessing occurs before compilation. This directive instructs the preprocessor to include the file iostream at that point. iostream is a library containing routines that perform I/O

4 A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } Informally, a namespace is a set of all the names that are declared at some point. We will talk about namespaces much later. For now, just remember to add this line after the #include directives.

5 A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } Program execution always begins in the ‘main’ function. You may invoke other functions from within main() The body of main()

6 Indentation It helps make the code readable.
The compiler ignores white space. int main() { int i; for(i=0; i<10; i++) { cout << "1TBS" << endl; } return 0; int main() { int i; for(i=0; i<10; i++) { cout << "Allman" << endl; } return 0; Choose ONE style and STICK with it!

7 Variables The syntax for declaring a variable is:
data_type variable_name ; or, for several variables of the same type: data_type variable_name1, variable_name2 ; Variables may be declared at any point in the program, but they must always be declared before they are used. The name of a variable is a sequence of letters, digits and underscores that does not begin with a digit. C++ is case sensitive

8 Readability Pick meaningful variable names that describe the entity they refer to. Things to avoid: the letter l (el) which can be confused with the digit 1 (one) single-letter variables (there's one exception) variable names starting with one or two underscores (many internal C++ names follow this convention) extremely long names names similar to C++ reserved words names similar to one another

9 Basic types int for integers (4 bytes*)
float and double for single- and double-precision floating point numbers (4 and 8 bytes respectively*) char for characters (1 byte*) * for our architecture

10 Literals integer literal double literal string literals
#include<iostream> using namespace std; int main () { cout << 10 << "USD = " << 8.2 << "EUR\n"; return 0; } integer literal double literal string literals

11 Literals Major disadvantages: Good idea: Look ma, no semicolon!
a literal cannot be reused (you have to type it in every time) it is easy to make a typo if you want to change it, you have to make sure you change all of its occurrences inside the program. Good idea: Use #define (C++ preprocessor directive) to define a literal Look ma, no semicolon! #define NUM_DOLLARS 10 #include<iostream> using namespace std; int main () { cout << NUM_DOLLARS; return 0; }

12 #defined constants Location: with other directives (e.g. #include)
Syntax: #define UNIQUE_NAME some_value Before compiling, pre-processor does 'find & replace' on your file: every occurrence of UNIQUE_NAME is replaced by some_value. Convention: UNIQUE_NAME is always in UPPERCASE. Major advantages: One single, central location for fixed values prevents scattered, hard-to-find errors in literals makes it easy to modify the value Works for ANY repeated text, not just numbers string literals, even executable text statements…

13 named constants Declared like variables BUT
preceded with the keyword const initialized at declaration their value CANNOT change afterwards. #include<iostream> using namespace std; int main () { const int num_dollars = 10; cout << num_dollars; return 0; }

14 Operators Assignment operator: = Arithmetic operators: +, , *, /, %
Relational operators: <, >, <=, >=, ==, != Logical operators: &&, ||, !

15 Operators ( ) ! Unary – * / % higher precedence + –
* / % + – < <= >= > = = != && || = higher precedence lower precedence Associativity: left-to-right (except = and unary operators)


Download ppt "A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout <<"

Similar presentations


Ads by Google