1 / 44

Lecture 1

Lecture 1. Variable, Operators and Expressions. Variable. In C++ a variable is a place to store information. A variable is a location in your computer memory in which you can store a value and from with you can later retrieve the value. Every variable has a name, type and value.

Download Presentation

Lecture 1

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. Lecture 1 Variable, Operators and Expressions

  2. Variable • In C++ a variable is a place to store information. • A variable is a location in your computer memory in which you can store a value and from with you can later retrieve the value. • Every variable has a name, type and value

  3. When a variable in C++ is defined, you must tell the compiler what kind of variable is it. (integer, character etc) • This information tells the compiler how much room to set aside and what kind of value you want to store in your variable.

  4. Basic Data Types • Following are the five Basic Data Types: Size • Int • char • float • double • void

  5. TYPE int • Stores only numeric data • It cannot store decimal numbers with fractional part. E.g 1.4 and 14.0 • (-) sign must be written when using negative number but with positive number (+) sign is not mandatory. • Leading zeros and commas cannot be used when writing integers.

  6. TYPE int(cont..) • The storage space occupied by int data type depends on the operating system, compiler and microprocessor. (4bytes or 2bytes) int sample values 4578 -4578 0

  7. TYPE char • Stores character data, which includes letters, digits, and special symbols. • Char data occupies 1byte of memory space • A character variable can store only one character at a time. • Char must be enclose in single quotation marks. char sample values ‘B’ ‘d’ ‘4’ ‘?’ ‘*’

  8. TYPE float and double • To store decimal numbers, float data type is used. • Occupies 4 bytes. • Floating value are precise up to 5-6 digits where as double provides 10 digits precision.

  9. Naming Variables (identifier) • A variable name is combination of alphabet, digits, or underscore. • The first character in the variable name must be an alphabet. • No commas or blanks are allowed within a variable name. • No special symbols other than underscore can be used in a variable name.

  10. Correct or Incorrect Name • X • Abc124 • My variable • 124abc • My ^ Variable • My_Variable • MyVariable • My Varibale!

  11. What Does a Variable Declaration Do? int ageOfDog; A declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location. 4 bytes for ageOfDog

  12. Syntax for Declarations Variable Declaration Datatype (variable) TypeName Identifier , Identifier . . ;

  13. Example1Variable Declaration and Initialization int main( ) { int x=5; int y; y=5; cout<< “The value of x and y”<<x<<y; } What is initialization :

  14. Example 2Receiving keyboard input and screen output //include iostream + conio header files int main( ) { int myage; cout<<“ Enter your age”; cin>>myage; cout<<myage; }

  15. Constants • A constant is the quantity that does not change. This quantity can be stored at a location in the memory. • A variable can be considered as the name given to the location in memory where constant is stored. • Key words : Reserve words for C

  16. Memory Depiction float y = 12.5;

  17. Memory Depiction float y = 12.5; int Temperature = 32;

  18. Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c';

  19. Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c'; int Number;

  20. Constants Keyword #include <stdio.h> const float PI = 3.14159; int main() { printf("Hello, world\n %d", XYZ); getch(); return 0; }

  21. OPERATORS

  22. In C++ you can combine variables and/or numbers using the arithmetic operators.

  23. Arithmetic in C C operation Algebraic C Addition(+) f+7 f+7 Subtraction (-) p-c p-c Multiplication(*) bm b*m Division(/) x/y, x ,x y x/y Modulus(%) r mod s r%s Power (Exponent) xy x ^ y y

  24. Assignment Statement • The assignment statement takes the form: • variable = expression; • Expression is evaluated and its value is assigned to the variable on the left side • In C++ = is called the assignment operator. • Integer division truncates remainder • Variable = 7 / 5 ; • 7 / 5 evaluates to 1 • Modulus operator returns the remainder • 7 % 5 evaluates to 2 • SomeVar = 7 % 5;

  25. Definition int NewStudents = 6;

  26. Definition int NewStudents = 6; int OldStudents = 21;

  27. Definition int NewStudents = 6; int OldStudents = 21; int TotalStudents;

  28. Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

  29. Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

  30. Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

  31. Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

  32. Assignment Statement sum = 5 + 3; sum= num1 + num2; sum= 2 + num1;

  33. More Assignment Statement • There is a shorthand notion that combines the assignment operator (=) and an arithmetic operator so that a given variable can have its value changed by adding, subtracting, multiplying, or dividing by a special value. • Variable Operator = Expression • Same as • Variable = Variable Operator (Expression)

  34. Examples • count += 2; // count = count + 2; • count -= 2; // count = count - 2; • count *= 2; • count *= val1 + val2; // count = count*val1 + val2 • count =+ 2; // count = 2+ count;

  35. Precedence order • Highest to lowest • () • *, /, % • +, -

  36. Example Algebra: z = pr%q+w/x-y C: z = p * r % q + w / x – y ; Precedence: 1 2 4 3 5

  37. Evaluate the Expression 7 * 10 - 5 % 3 * 4 + 9

  38. Evaluate the Expression 7 * 10 - 5 % 3 * 4 + 9 means (7 * 10) - 5 % 3 * 4 + 9 70 - 5 % 3 * 4 + 9 70 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - (2 * 4) + 9 70 - 8 + 9 (70 - 8) + 9 62 + 9 71 What would be the precedence of parenthesis: Evaluate (4+5)/5-10%4

  39. EXAMPLE :

  40. Increment & Decrement Operators • Increment operator: increment variable by 1 • Decrement operator: decrement variable by 1 • Pre-increment: ++variable • Post-increment: variable++ • Pre-decrement: --variable • Post-decrement: variable— • Int x; : ++x; : cout<<x++; x++; : x = x + 1; : cout<<++x;x--;

  41. Decision Making • Checking falsity or truth of a statement • Equality operators have lower precedence than relational operators • Relational operators have same precedence • Both associate from left to right

  42. Decision Making • Equality operators • == • != • Relational operators • < • > • <= • >=

  43. Summary of precedence order Operator Associativity () left to right * / % left to right + - left to right < <= > >= left to right == != left to right = left to right

More Related