Nothing Special   »   [go: up one dir, main page]

Introduction To C++ Programming

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Chapter 2 Introduction to C++ programming What is program? What use of compiler? What syntax mean?

Tell the d/ce b/n Pseudocode and flow chart

2.1 Introduction Probably the best way to start learning a programming language is with a program

// my first program in C++ (source code) #include <iostream.h> int main () { cout << My first program in C++!"; return 0; }
The result of the program once compiled and executed is My first program in C++!

General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

// my first program in C++ This is a comment line. All the lines beginning with two slash signs (//) are considered comments and do not have any effect in the behavior of the program #include <iostream.h> Sentences that begin with a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler int main () This line corresponds to the beginning of the main function declaration. The main function is the point by where all C++ programs begin their execution

cout << "My first program in C++!"; This instruction does the most important thing in this program. cout is the standard output stream in C++. cout is declared in the iostream.h header file, so in order to be able to use it that file must be included Notice that the sentence ends with a semicolon character (;). return 0; The return instruction makes the main() function to finish and return the code that the instruction is followed by, in this case 0

Comments Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code

C++ supports two ways to insert comments: // line comment /* block comment */
If you include comments within the source code of your programs without using the comment characters combinations //, /* nor */, the compiler will take them as if they were C++ instructions

#include <iostream.h> int main () { cout << "Hello Class! "; // says Hello Class! cout << "I'm a C++ program"; // says I'm a C++ program return 0; } The result of the program once compiled and executed is Hello Class! I'm a C++ program

2.2 Variables, Data types, Constants Identifiers :-A valid identifier is a sequence of one or more letters, digits or underline symbols ( _ ). Only letters, digits and underline characters are valid variable identifiers should always begin with a letter when inventing your own identifiers is that they cannot match with any language's key word Eg :- bool, break, case, char, class, const Very important: The C++ language is "case sensitive", that means that a same identifier written in capital letters is not equivalent to another Eg:- RESULT is not the same one that the variable result nor variable
Result

Data types Our computer's memory is organized in bytes. A byte is the minimum amount of memory which we can manage

Declaration of variables In order to use a variable in C++, we must first declare it specifying which of the data types above we want it to be. The syntax to declare a new variable is to write the data type specifier that we want (like int, short, float...) followed by a valid variable identifier Eg int a; float mynumber; If you need to declare several variables of the same type Eg int a, b, c;

Integer data types (char, short, long and int ) can be signed or unsigned. Thus to specify an integer data type we do it by putting the keyword signed or unsigned before the data type itself Eg:- unsigned short NumberOfSons; signed int MyAccountBalance By default, if we do not specify signed or unsigned it will be assumed that the type is signed

// operating with variables #include <iostream.h> int main () { // declaring variables: int a, b; int result; // process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0;}

Data Representation Constants: Literals A constant is any expression that has a fixed value. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings Integer Numbers they are numerical constants that identify integer decimal numbers. Notice that to express a numerical constant we do not need to write quotes (") nor any special character (233,8349,092) Floating Point Numbers They express numbers with decimals and/or exponent. They can include a decimal point, an e character (that expresses "by ten at the Xth height", where X is the following integer value) or both.

Characters and strings There also exist non-numerical constants, like: 'z' 'p' "Hello world" "How do you do? Notice that to represent a single character we enclose it between single quotes (') and to express a string of more than one character we enclose them between double quotes (") ;

Operators Once we know of the existence of variables and constants we can begin to operate with them Assignation (=) The assignation operator serves to assign a value to a variable. a = 5; the assignation operation always takes place from right to left and never at the inverse a = b = c = 5;

Arithmetic operators ( +, -, *, /, % ) Module is the operation that gives the rest of a division of two integer values For example, if we write a = 11 % 3;, the variable a will contain 2 as result since 2 is the rest from dividing 11 between 3 Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) which allow to modify the value of a variable with one of the basic operators value += increase; is equivalent to value = value + increase

Increase and decrease. Another example of saving when writing code are the increase operator (++) and the decrease operator (--). They increase or reduce by 1 the value stored in a variable. They are equivalent to +=1 and to -=1, respectively a++; a+=1; a=a+1; In case that the increase operator is used as a prefix (++a) the value is increased before the expression is evaluated in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated
B=3; A=++B;// A is 4, B is 4 B=3; A=B++; // A is 3, B is 4

Relational operators ( ==, !=, >, <, >=, <= ) In order to evaluate a comparison between two expressions we can use the Relational operators == Equal != Different > Greater than < Less than >= Greater or equal than <= Less or equal than

Logic operators ( !, &&, || ) Operator ! is equivalent to boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to invert the value of it, producing false if its operand is true and true if its operand is false Logic operators && and || are used when evaluating two expressions to obtain a single result. They correspond with boolean logic operations AND and OR respectively

You might also like