Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 1 Algorithmization and Programming Languages.

Similar presentations


Presentation on theme: "Week 1 Algorithmization and Programming Languages."— Presentation transcript:

1 Week 1 Algorithmization and Programming Languages

2 Introduction About C++ Facilitates a disciplined approach to program design C++ programs consist of pieces called classes and functions Provides C++ Standard Library

3 First Program in C++: Printing a Line of Text

4 Comments - document program - improve program readability - ignored by compiler - single-line comment starts with // (it terminates at the end of the current line) - multiple-line comments starts with /* and ends with */

5 Preprocessor directive - begins with a hash sign ( # ) - processed before the program is compiled - includes in the program the contents of the input/output stream header file - preprocessor directives (like #include ) do not end with a semicolon

6 int main () - a part of every C++ program - is a program building block called a function - exactly one function in every program must be main - C++ programs begin executing at function main, independently of its location within the source code - the keyword* int that main "returns" an integer *A keyword is a word in code that is reserved by C++ for a specific use

7 BODY - the left brace, {, must begin the body of every function - the right brace, }, must end each function's body - the entire line is called a statement - instructs the computer to perform an action - C++ statement must end with a semicolon( ; ) - output and input are accomplished with streams of characters.

8 INPUT/OUTPUT cin - Standard input stream - Normally keyboard cout - Standard output stream - Normally computer screen cerr - Standard error stream - Display error messages

9 Standard output stream object std::cout - “Connected” to screen - Stream insertion operator (<<) - Value to right (right operand) inserted into output stream Namespace std:: specifies using name that belongs to “namespace” std std:: removed through use of using statements Escape characters ( \ ) - Indicates “special” character output

10 return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

11 Escape sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. \a Alert. Sound the system bell. \\ Backslash. Used to print a backslash character. \' Single quote. Use to print a single quote character. \'' Double quote. Used to print a double quote character. Escape sequences

12  1 // Fig. 1.4: fig01_04.cpp  2 // Printing a line with multiple statements.  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome ";  9 std::cout << "to C++!\n";  10  11 return 0; // indicate that program ended successfully  12  13 } // end function main  1 // Fig. 1.4: fig01_04.cpp  2 // Printing a line with multiple statements.  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome ";  9 std::cout << "to C++!\n";  10  11 return 0; // indicate that program ended successfully  12  13 } // end function main Welcome to C++! Multiple stream insertion statements produce one line of output. Comments Preprocessor directive BODY

13  1 // Fig. 1.5: fig01_05.cpp  2 // Printing multiple lines with a single statement  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome\nto\n\nC++!\n";  9  10 return 0; // indicate that program ended successfully  11  12 } // end function main  1 // Fig. 1.5: fig01_05.cpp  2 // Printing multiple lines with a single statement  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome\nto\n\nC++!\n";  9  10 return 0; // indicate that program ended successfully  11  12 } // end function main Welcome to C++! Welcome to C++! Using newline characters to print on multiple lines.

14 Adding Integers Output:

15 Standard input stream object: std::cin - obtain values typed at the keyboard - stream extraction operator ( >> ) Example: int number1; int number2; cin>>number1>>number2;

16 Variables - Location in memory where value can be stored - Common data types int – integer numbers (0, 7, 876, 1998) char – characters ('a', 'G', '#') double – floating point numbers (0.2, 3.667, 8.65) Declare variables with name and data type before use int integer1; int integer2; int sum; Can declare several variables of same type in one declaration Comma-separated list int integer1, integer2, sum;

17 Variables names Valid identifier - Series of characters (letters, digits, underscores) that begin with a letter or underscore - Neither spaces nor punctuation marks or symbols can be part of an identifier - Cannot match any keyword of the C++ Example: char abc, _abc, abc3, a3bc, a_3b_c; - Cannot begin with digit - Case sensitive

18 Initialization of variables When declaring a regular local variable, its value is by default undetermined. There are two ways to initialize variables in C++: 1. c-like initialization: type identifier = initial_value ; Example: int a = 0; 2. constructor initialization: type identifier (initial_value); Example: int a(0); Both ways of initializing variables are valid and equivalent in C++

19 6 6

20 Memory Concepts - Variable names correspond to locations in the computer's memory. - Every variable has a name, a type, a size and a value - When new value placed into variable, overwrites previous value - Reading variables from memory nondestructive - Placing a new value into a memory location is said to be destructive

21 Memory Concepts std::cin >> integer1; Assume user entered 45 std::cin >> integer2; Assume user entered 72 sum = integer1 + integer2;

22 Fundamental data types When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++

23 Specifiers Specifiers modify the meanings of the basic built-in types and expand them to a much larger set. There are four specifiers: Long Short Signed Unsigned modify the maximum and minimum values that a data type will hold. tell the compiler how to use the sign bit with integral types and characters (floating- point numbers always contain a sign).

24 Fundamental data types in C++ * The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems.

25 Arithmetic operators

26 Integer division - yields an integer quotient - fractional part is discarded (i.e., truncated) no rounding occurs Example: int a = 7, b = 4; cout<<a/b; Modulus operator ( % ) - yields the remainder after integer division - can be used only with integer operands * Example: int a = 7, b = 4; cout<<a%b; *Attempting to use the modulus operator (%) with noninteger operands is a compilation error. 1 3

27 Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right

28

29 REFERENCES: 1. C++ How to Program, By H. M. Deitel Chapter 1. Introduction to Computers, the Internet and World Wide Web Chapter 2. Introduction to C++ Programming 2. http://www.cplusplus.com

30 THANK YOU FOR YOUR ATTENTION


Download ppt "Week 1 Algorithmization and Programming Languages."

Similar presentations


Ads by Google