Presentation is loading. Please wait.

Presentation is loading. Please wait.

1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.

Similar presentations


Presentation on theme: "1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value."— Presentation transcript:

1 1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value into the variable: scanf (“%d”, &age);

2 2 Examples:  Declare a variable to store the Watts for a light bulb:  Prompt the user to enter a value (ex: 75):  Read the entered value into the variable:  Display this variable with a message to the screen, ex: This bulb is ….. Watts

3 3 Cont…  Declare 3 variables to store 3 resistor values. float r1, r2, r3; …  Prompt user to enter 3 values for resistors:  Scan the entered values into these variables.  Display these variables to the standard output in 3 lines:

4 4 Arithmetic Operators in C:  Operator meaningExample + addition 3 + 6  9 - subtraction 4 – 7  -3 * Multiplication 2 * 13  26 / division 7 / 2  3 7 / 2.0  3.5 % remainder 7 % 2  1 3 % 10  3 10 % 3 -> 1

5 5 Dividing 2 whole numbers:  Results in a whole number, ex: 12 / 5  2 1 / 2  0  How to perform a real division? a) Make one of the values float, ex: 11.0 / 2  5.5 or 11 / 2.0  5.5

6 6 Arithmetic expression:  Meaningful combination of numeric variables, constants, and operators: Examples: 3 + 8 12 + 6 * 3  30 (12 – 10) * 3 / 2  3

7 7 Precedence Rule:  Which operator takes precedence?  Highest: ( ) - negation *, /, % +, -  Lowest: = Rule: Arithmetic expression gets evaluated: From left to right, Respecting the precedence rules.

8 8 Ex: Evaluate the following exp: Assume: int a = 3; int b = 7; Evaluate: 1. a + b 2. 10 + 6 – b 3. a + b * 3 4. b * (12 – a) / 2  b * 9 / 2

9 9 Cont..  Given: int a, b, c; a = 3; b = 7;  Evaluate: c = a + b; // ------ is stored in c Careful: a + b = c; is illegal!! The target should be on the LHS (left hand side) of =

10 10 Cont…  Assume: a = 3; b = 7;  What will be stored in a and b after: b = a;  Careful: b = a; is not the same as : a = b;

11 11 More examples: Given: int a, b, c; a = 3; b = 7; Write a statement to store sum of a and 10 in c: Assign a to c: Increment a by 1. Divide a by 2 times b, and store the result in c.

12 12 Translate math formulas to C:  Translate following algebra formulas to c: a + 4b ab a + b + c c = ----------------- 2a

13 13 Type Casting: You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15; float half; half = a / 2; half = (float) a / 2;

14 14 Increment / Decrement Operators:  ++ Increment operator: int a = 5; a = a + 1; can be written as: a++; or ++a;  -- Decrement Operator: a = a -1; can be written as: a--; or --a;

15 15 What is a difference? ++a; a++;  Makes no difference if they stand alone!  Makes a difference if used in an expression!  Ex: Given int a, b; a = b = 5; a = 3 * b++; Is equivalent to: a = 3 * b; b++; Outcome: /

16 16 Cont…  Ex2: a = b = 5; a = 3 * ++b;  Is equivalent to: b++; a = 3 * b; Outcome:

17 17 Compound Assignment Operators:  Assume: int a = 6;  Increment a by 2: a = a + 2; Using combined assignment operator: +=: a += 2;  Multiply a by 6: a = a * 6; Using combined assignment operator: *=: a *= 6;

18 18 Cont…  -= a -= b; means: a = a – b;  *= a *= 5; means: a = a * 5;  /= b /= a; means: b = b / a;  %= a %= 2; means: a = a % 2;  a *= (b + c); means: a = a * (b + c);


Download ppt "1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value."

Similar presentations


Ads by Google