Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Program Development in C

Similar presentations


Presentation on theme: "Structured Program Development in C"— Presentation transcript:

1 Structured Program Development in C
Dr. Nouf Aljaffan Nouf Aljaffan (C) 2018

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

3 Variables and DataTypes
Nouf Aljaffan (C) 2018

4 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

5 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

6 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(doubl e) Nouf Aljaffan (C) 2018

7 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

8 Implicit Type Conversion
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. 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 reuslt. average = nTotal / 3.0; 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 if (expression) { Branching forms (1) If Statement
Block of statements; } (1) If Statement if (expression) { Block of statements; } else { Block of statements; } (2) If ..else Statement if (expression) { Block of statements; } else if(expression) { } else { Block of statements; } (3) Nested If..else Statement Nouf Aljaffan (C) 2018

16 if Selection statement
if (expression) { Block of statements; } if Selection statement Nouf Aljaffan (C) 2018

17 if…else Selection statement
if (expression) { Block of statements; } else { Block of statements; } 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 switch statement switch( expression ) { } limited by
case constant-expression1: statements1; [case constant-expression2: statements2;] [case constant-expression3: statements3;] [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

23 switch statement switch( expression ) { } switch( Grade ) {
case constant-expression1: statements1; break; [case constant-expression2: statements2; break;] [case constant-expression3: statements3; ;break;] [default : statements4;] } switch statement switch( Grade ) { case 'A' : printf( "Excellent\n" ); case 'B' : printf( "Good\n" ); case 'C' : printf( "OK\n" ); case 'D' : printf( "Mmmmm....\n" ); case 'F' : printf( "You must do better than this\n" ); default : printf( "What is your grade anyway?\n" ); } Nouf Aljaffan (C) 2018

24 Operators Nouf Aljaffan (C) 2018

25 Assignment Operators Nouf Aljaffan (C) 2018

26 Increment and Decrement Operators
Nouf Aljaffan (C) 2018

27 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

28 Nouf Aljaffan (C) 2018

29 Conclusion Nouf Aljaffan (C) 2018

30 Questions? Nouf Aljaffan (C) 2018


Download ppt "Structured Program Development in C"

Similar presentations


Ads by Google