1 / 41

Introduction to computer & programming

Introduction to computer & programming. MS SADIA EJAZ CS DEPARTMENT. Programming Language C++. 2. Constants. It is a quantity that cannot be changed during program execution. Two types of constants. Literal constant Symbolic constant. Literal Constant.

kevork
Download Presentation

Introduction to computer & programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to computer & programming MS SADIA EJAZ CS DEPARTMENT MS Sadia Ejaz CIIT ATTOCK

  2. Programming Language C++ MS Sadia Ejaz CIIT ATTOCK 2

  3. Constants • It is a quantity that cannot be changed during program execution. • Two types of constants. • Literal constant • Symbolic constant MS Sadia Ejaz CIIT ATTOCK

  4. Literal Constant • It is a value that is typed directly in a program. • For example • int age = 19 ; • Types of Literal Constants • Integer constant e.g. 87 • Floating point constant e.g. 10.22F • Character constant e.g. ‘A’ • String constant e.g. “Pakistan” MS Sadia Ejaz CIIT ATTOCK

  5. Symbolic Constants • It is a name given to values that cannot be changed. • It can be declared in two ways. • const Qualifier • const data_type identifier = value ; • e.g const int N = 100 ; • Define Directive • # define identifier value ; • e.g # define Pl 3.141593 ; MS Sadia Ejaz CIIT ATTOCK

  6. Expression Operator Operands • It is a statement that evaluates to a value. • It consists of operators and operands. • e.g A + B ; MS Sadia Ejaz CIIT ATTOCK

  7. Operators • There are the symbols that are used to perform certain operations on data. • These include : • Arithmetic operators • Relational operators • Logical operators • Bitwise operators , etc • The operators can be categorized as follows: • Unary Operators • Binary Operators MS Sadia Ejaz CIIT ATTOCK

  8. Unary Operators It is a type of operator that works with one operand. - , ++ , -- e.g – a ; MS Sadia Ejaz CIIT ATTOCK

  9. Binary Operators It is a type of operator that works with two operands. + , - , * , /, % e.g x / y ; MS Sadia Ejaz CIIT ATTOCK

  10. Arithmetic Operators • It is a symbol that performs mathematical operation on data. • Addition + • Subtraction - • Multiplication * • Division / • Modulus % MS Sadia Ejaz CIIT ATTOCK

  11. Lvalue and Rvalue • Lvalue • It is an operand that can be written on the left side of assignment operator =. • Rvalue • It is an operand that can be written on the right side of assignment operator =. MS Sadia Ejaz CIIT ATTOCK

  12. Compound Assignment Statement It is an assignment statement that assigns a value to many variables. e.g. A = B = 10 ; MS Sadia Ejaz CIIT ATTOCK

  13. Compound Assignment Operators They combine assignment operator with arithmetic operators. Variable operator = expression; e.g . N + = 10 ; (N +=10;) MS Sadia Ejaz CIIT ATTOCK

  14. Increment Operator • It is used to increase the value of a variable by 1. • ++ • It is a unary operator and works with single variable . • e.g. A ++ • It can be used in two forms. • prefix form  The increment operator is written before the variable. • postfix form  The increment operator is written after the variable. • ++y ;  prefix form • y++ ;  postfix form MS Sadia Ejaz CIIT ATTOCK

  15. Decrement Operator • It is used to increase the value of a variable by 1. • -- • It is a unary operator and works with single variable . • e.g. A -- • It can be used in two forms. • prefix form  The decrement operator is written before the variable. • postfix form  The decrement operator is written after the variable. • --y ;  prefix form • y-- ;  postfix form MS Sadia Ejaz CIIT ATTOCK

  16. Operator Precedence • The order in which different types of operators in an expression are evaluated is known as operator precedence. It is also known as hierarchy of operators. • Each operator has its own precedence level. If an expression contains different types of operators, the operators with higher precedence are evaluated before the operators wit lower precedence. MS Sadia Ejaz CIIT ATTOCK

  17. Operator Precedence (contd.) • The order of precedence in C ++ language is as follows: • Any expression given in parenthesis is evaluated first. • Then multiplication * and division / operators are evaluated. • Then plus + and minus – operators are evaluated. • In case of parenthesis within parenthesis, the expression of the inner parenthesis will be evaluated first. MS Sadia Ejaz CIIT ATTOCK

  18. Operator Precedence (contd.) • Example: 10 * (24 / (5 - 2) ) + 13 10 * ( 24 / 3 ) + 13 10 * 8 + 13 80 + 13 93 MS Sadia Ejaz CIIT ATTOCK

  19. Operator Associativity • The order in which operators od same precedence are evaluated is known as operator associativity. • If an expression contains some operators that have same precedence level, the expression is evaluated from left-to-right or right-to-left. MS Sadia Ejaz CIIT ATTOCK

  20. Operator Associativity (contd.) • Operator associativity in C ++ language is as follows: MS Sadia Ejaz CIIT ATTOCK

  21. Type Casting • The process of converting the data type of a value during execution is known as type casting. • It can be performed in two ways: • Implicit Type Casting • Explicit Type Casting MS Sadia Ejaz CIIT ATTOCK

  22. Implicit Type Casting Highest data type long double double float long int char It is performed automatically by C++ compiler. e.g. char + float  float MS Sadia Ejaz CIIT ATTOCK Lowest data type

  23. Explicit Type Casting It is performed automatically by the programmer. (type) expression; e.g. (int) a% (int) b ; MS Sadia Ejaz CIIT ATTOCK

  24. The “sizeof” Operator It is used to find the size of any data value. It gives the number of bytes occupied by that value. sizeof(operand); e.g. sizeof (10); MS Sadia Ejaz CIIT ATTOCK

  25. Comments • These are the lines of program that are not executed. • They explain the purpose of the code. • The can be added anywhere in programs in two ways: • Sing-line Comments  “//” • Multi-line Comments  /* ---*/ MS Sadia Ejaz CIIT ATTOCK

  26. Input and Output • Input • The process of giving something to computer is known as input. • Standard Input • This term refers to the input via keyboard. • Output • The process of getting something from computer is known as output. • Standard Output • This terms refers to the output displayed on monitor. • cout<<variable/constant/expression; MS Sadia Ejaz CIIT ATTOCK

  27. Escape Sequences • These are special characters used in control string to modify the format of output. • Different escape sequences are as follows: Escape Sequence Purpose \ a Alarm \ b Backspace \ f Form feed \ n Carriage return \ t Tab \ ’ Single quote \ ” Double quote MS Sadia Ejaz CIIT ATTOCK

  28. C++ Manipulators • These are used to format the output in different styles. • Some important manipulators are as follows: • endl  end of line • setw  set width • setprecision  set the number of digits to be displayed • setfill  replaces the leading or trailing blanks in output • showpoint  displays the decimal part • fixed  controls the output of floating-point numbers MS Sadia Ejaz CIIT ATTOCK

  29. Standard Input It refers to the input given via keyboard. cin>> var ; MS Sadia Ejaz CIIT ATTOCK

  30. LAB WORK MS Sadia Ejaz CIIT ATTOCK

  31. Program .1 #include <iostream.h> #incldue<conio.h> void main() { clrscr(); cout << “Hello World” << endl; getch(); } MS Sadia Ejaz CIIT ATTOCK

  32. Output • Hello World MS Sadia Ejaz CIIT ATTOCK

  33. Program .2 • Write a program which will display your name. MS Sadia Ejaz CIIT ATTOCK

  34. Program .3 #include <iostream.h> void main() { char ch1, ch2, sum; ch1 = ‘2’ ; ch2 = ‘6’ ; sum = ch1 + ch2 ; cout<<“Sum =“<<sum; } MS Sadia Ejaz CIIT ATTOCK

  35. Output • 104 • Because ASCII values of ‘2’ and’6’ are 50 and 54 MS Sadia Ejaz CIIT ATTOCK

  36. Program .4 #include <iostream.h> #incldue<conio.h> void main() { clrscr(); short testVar = 32767; cout << testVar << endl; testVar = testVar +1 ; cout << testVar << endl; testVar = testVar - 1 ; cout << testVar << endl; getch(); } MS Sadia Ejaz CIIT ATTOCK

  37. Output • 32767 • - 32768 • 32767 • Because range of short is -32768 to 32767. MS Sadia Ejaz CIIT ATTOCK

  38. Program . 5 #include <iostream.h> #incldue<conio.h> #define PI 3.141 void main() { float r, area; clrscr(); cout << “Enter radius:”; cin>> r; area = 2.0 * PI * r; cout << “Area=“ << area; getch(); } MS Sadia Ejaz CIIT ATTOCK

  39. Output • User will give input, then Area will be displayed on the screen. MS Sadia Ejaz CIIT ATTOCK

  40. Program. 6 #include <iostream.h> #incldue<conio.h> void main() { clrscr(); int a,b; a = 10; b = 5; cout << “a+b =“<< a+b << endl; cout << “a-b =“<< a-b << endl; cout << “a*b =“<< a*b << endl; cout << “a/b =“<< a/b << endl; cout << “a%b =“<< a%b << endl; getch(); } MS Sadia Ejaz CIIT ATTOCK

  41. Output • a+b =15 • a-b =5 • a*b =50 • a/b =2 • a%b =0 MS Sadia Ejaz CIIT ATTOCK

More Related