MELJUN CORTES Introduction to C Programming


Published on

MELJUN CORTES Introduction to C Programming

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Introduction to C Programming

  2. 2. OBJECTIVES 1. Understand the basic components of a C++ program. 2. Classify identifiers, keywords, constants, and variables. 3. Enumerate and apply the basic data types of C++. 4. Identify the different operators to be used. 5. Know the precedence of operators.
  3. 3. A Simple C++ Program /* This is a simple C++ program */ /* declaration of header file */ #include <iostream.h>   /* start of main program */ void main(void) { cout << "Welcome to C++!!!n"; }
  4. 4. Basic Components of a C++Code HEADER FILES Functions Statements The { } Symbols The /* */ symbols
  5. 5. Identifiers, Keywords,Constants, and VariablesIDENTIFIERS (NAMING CONVENTIONS) - Names are made up of letters and digits. - The first character must be a letter or an underscore (_). However, an underscore is not recommended to be used as the first character in a name since some of C++’s internal identifiers begin with underscores. - C++ is case-sensitive, i.e., the lower-case letter ‘a’ is not the same as the uppercase letter ‘A’. - At least the first 31 characters of a name are significant.
  6. 6. Identifiers, Keywords,Constants, and VariablesKEYWORDS
  7. 7. Identifiers, Keywords,Constants, and VariablesCONSTANTS Constants are entities whose value does not change. A constant can either be numeric constant or a literal constant. In C/C++, a numeric constant can be an integer or floating point number, while a literal constant can be a single character or a string, i.e. a constant with more than one character. A single character is written such that it is enclosed in a pair of single quotes. A string is written enclosed in a pair of double quotes
  8. 8. Identifiers, Keywords,Constants, and VariablesVARIABLES A program is made of data and instructions to manipulate those data. Note that data have to be stored somewhere, and thus will need some memory space in the Random Access Memory (RAM). In a C++ program, a variable is the entity that holds data. A variable, as the name suggests, is a varying entity depending on the actual data it holds. Without variables, it would be impossible to store data.
  9. 9. Identifiers, Keywords,Constants, and VariablesA variable has the following characteristics: 1. a symbolic name; 2. an associated physical memory space (portion in a RAM) 3. a data type 4. a value that depends on the data type 5. a scope 6. a lifetime
  10. 10. Basic Data Types in C++ Data Type  specifies the kind of values can be assumed by a variable of that type  the range of values that can be assumed by a variable of that type  and the amount of memory (in bytes) needed by a variable to store a value of that type.
  11. 11. Basic Data Types in C++ Five basic data types in C++  char – the character data type. The char data type is used to represent/store/manipulate character data values.  int – the integer data type. The int data type is used to represent/store/manipulate signed whole numbers. The range of values that can be assumed by an int value is from -2147483648 to 2147483647.   float – the single precision floating point data type. The float data type is used to store single precision signed real numbers. The appropriate range of values that can be assumed by a float value is from 3.4 X 10 -38 to 3.4 X 1038.  double – the double precision floating point data type. The double data type is used to store double precision signed real numbers. The appropriate range of values that can be assumed by a double value is from 1.7 X 10-308 to 1.7 X 10308.  bool – the Boolean data type. The bool data type is used to represent Boolean values true or false.
  12. 12. Variable Declaration A variable declaration is an “action” by which a variable is “introduced” to a program/function. All variables in a C++ program must be declared. If you forgot to do so, the compiler will report a syntax error. The syntax for declaring a variable is as follows: <data type> <variable name>; A semicolon signifies the end of a declaration.
  13. 13. Examples: char ch; bool b; int i; float f; double d;
  14. 14. More Examples: char ch1, ch2; bool b1, b2; int x, y, z; float degree_celsius, degree_fahrenheit, degree_kelvin; double numerator, denominator;
  15. 15. Operators Operators are symbols representing operations that can be performed on constants and variables. There are four basic operations available in C++ language. 1. Assignment Operation 2. Arithmetic Operation 3. Relational Operation 4. Logical Operation
  16. 16. The assignment operator is denoted by theequal symbol (=). It is used to store (i.e.,assign) a value to a variable. The syntax ofan assignment operation is: <variable name> = <expression>;In the syntax above, variable name can beany valid identifier in C++, while expressioncan be a constant, variable or a validexpression.
  17. 17. THE ASSIGNMENT OPERATOR The assignment operation is also a statement, thus a semicolon should terminate it.  ch1 = ‘Z’;  ch2 = ch1;  x = 5;  y = x; If in some cases we assign a value whose data type is different from the data type of the receiving variable, the data type of the value will be converted to the data type of the variable (either demoted or promoted).
  19. 19. There are some important notes to remember in theuse of C++ operators: Assigning a variable to a constant is syntax error. For example: 12345 = x; Assigning the value of a variable to a variable of the same name is syntactically correct, but practically useless and does not make any sense. For example: a = 12345; a = a; // a get the value of a, which is 12345 It is usual in programming to assign a value of an expression to a variable, wherein the old value of the variable is also used in the expression. For example: a = 50; a = a + 1; The statement a = 50; means the value 50 is assigned to a. The next statement adds 1 to this value resulting to 51, which is then assigned as the new value of a.
  21. 21. THE RELATIONAL OPERATORS (cont.)The relational operators can be performed onall the basic data types.In C++, the result of a relational operation iseither a 0 (zero) or a 1 (one). A zero meansthat the relation is FALSE and a one meansthat it is TRUE. All the relational/comparisonoperators are binary operators.It is important to remember that the test forequality uses two equal symbols. Forgettingone of the equal sign is a very commonlogical error.
  22. 22. THE LOGICAL OPERATORS The logical operators are normally used in conjunction with relational operators to test for multiple conditions. The logical NOT is a unary operator, it is used with only one operand to its right. The remaining operators are binary operators. The logical NOT operation is performed before logical AND, before logical OR.