Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables & Data types Selection Statements Additional Operators

Similar presentations


Presentation on theme: "Variables & Data types Selection Statements Additional Operators"— Presentation transcript:

1 Variables & Data types Selection Statements Additional Operators
Outlines Variables & Data types Selection Statements Additional Operators Nouf Aljaffan (C) 2018

2 Variables and DataTypes
Nouf Aljaffan (C) 2018

3 Variables/Identifiers
Names that represent values in the program Similar to algebraic variables All variables have a type which must be declared E.g. int x; float y; Type determines : how arithmetic is preformed, how much memory space is required. Variables/Identifiers Nouf Aljaffan (C) 2018

4 Data types and sizes C has a small family of datatypes.
Numeric (int, float, double) Character (char) User defined (struct,union) Nouf Aljaffan (C) 2018

5 Basic Data Types The individual sizes are machine/compiler dependent.
However, the following is guaranteed: sizeof(char)<sizeof(short)<=sizeof(int)<=sizeof(long) sizeof(char)<sizeof(short)<=sizeof(float)<=sizeof(double) Nouf Aljaffan (C) 2018

6 Type Conversion Implicit Explicit
Data will get automatically converted from one type to another. Data may also be expressly converted, using the typecast operator Nouf Aljaffan (C) 2018

7 Implicit Type Conversion
1. When data is being stored in a variable, if the data being stored does not match the type of the variable. The data being stored will be converted to match the type of the storage variable. Input : 11.5 What will be printed ? Nouf Aljaffan (C) 2018

8 Implicit Type Conversion
2. When an operation is being performed on data of two different types. The "smaller" data type will be converted to match the "larger" type. For example, when an int is added to a double, the computer uses a double version of the int and the result is a double. The following example converts the value of nTotal to a double precision value before performing the division.  Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the result. (try it ) int nTotal ; double average ; average = nTotal / 3.0; 3. When data is passed to or returned from functions. Nouf Aljaffan (C) 2018

9 Explicit Type Conversion
The following example converts the value of nTotal to a double precision value before performing the division. ( nStudents will then be implicitly promoted, following the guidelines listed above. ) average = ( double ) nTotal / nStudents; Note that nTotal itself is unaffected by this conversion. Nouf Aljaffan (C) 2018

10 Variables and Variable Definitions
Naming rules: Variable names can contain letters,digits and _ Variable names should start with letters. Keywords (e.g., for,while etc.) cannot be used as variable names Variable names are case sensitive. int x; int X declares two different variables. Nouf Aljaffan (C) 2018

11 Pop quiz (correct/incorrect):
int money$owed; int total_count int score2 int 2ndscore int long Nouf Aljaffan (C) 2018

12 Variable declaration The general format for a declaration is
type variable-name [=value] ; char x; /∗ uninitialized ∗/ char x=’A’; /∗ intialized to ’A’∗/ char x=’A’,y=’B’; /∗multiple variables initialized ∗/ char x=y=’Z’; /∗multiple initializations ∗/ Nouf Aljaffan (C) 2018

13 Constants The const qualifier is used to tell C that the variable value can not change after initialisation. Constants are literal/fixed values assigned to variables or used directly in expressions. Examples: const char letter = ‘a’; const float pi= ; Nouf Aljaffan (C) 2018

14 Branching/ Selections
Nouf Aljaffan (C) 2018

15 Branching forms (1) If Statement (3) Nested If..else Statement
if (expression) {Block of statements; } if (expression) { Block of statements; } else if(expression) { Block of statements; } else (2) If ..else Statement if (expression) {Block of statements; } else Nouf Aljaffan (C) 2018

16 1. if Selection statement
Nouf Aljaffan (C) 2018

17 2. if…else Selection statement
Nouf Aljaffan (C) 2018

18 Conditional operator ?:
The( ? : ) operator is just like an if ... else statement except that because it is an operator you can use it within expressions. ? : is a ternary operator in that it takes three values, this is the only ternary operator C has. ? : takes the following form: if condition is true ? then X return value : otherwise Y value; Nouf Aljaffan (C) 2018

19 Conditional operator ?:
if condition is true ? then X return value : otherwise Y value; Nouf Aljaffan (C) 2018

20 If..else vs ?: max = x >= y ? x : y ; if ( x > = y ) max = x ;
max = y ; Nouf Aljaffan (C) 2018

21 Nested if...else Statements
Nouf Aljaffan (C) 2018

22 Nested if...else Statements
Nouf Aljaffan (C) 2018

23 Nested if...else Statements
Nouf Aljaffan (C) 2018

24 switch statement limited by switch( expression ) { }
case constant-expression1: statements1; break; [case constant-expression2: statements2; break;] [case constant-expression3: statements3; ;break;] [default : statements4;] } limited by   The expression can be integer or char and cannot be float or any other data type. No two case statement constants may be the same. Character constants are automatically converted to integer. Nouf Aljaffan (C) 2018

25 switch statement Nouf Aljaffan (C) 2018

26 Operators Nouf Aljaffan (C) 2018

27 Assignment Operators Nouf Aljaffan (C) 2018

28 Increment and Decrement Operators
Nouf Aljaffan (C) 2018

29 Increment and Decrement Operators
For Example :- int i, j = 2 ; i = ++ j ; /* prefix :- i has value 3, j has value 3 */ i = j++ ; /* postfix :- i has value 3, j has value 4 */ Nouf Aljaffan (C) 2018

30 Nouf Aljaffan (C) 2018


Download ppt "Variables & Data types Selection Statements Additional Operators"

Similar presentations


Ads by Google