Download presentation
Presentation is loading. Please wait.
1
Structured Program Development in C
Dr. Nouf Aljaffan Nouf Aljaffan (C) 2018
2
Variables & Data types Control structures Additional Operators
Outlines Variables & Data types Control structures 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 result. 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
Declaration of answer and assigning it a value
The general form of the assignment statement is: variable = expression; Nouf Aljaffan (C) 2018
14
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
15
Exercises Define the variables x, y and z to be of type int where x= 9, y=2, z=0 Prompt the user to enter a float and store it in z. Define the variable result, compute the product of the numbers in the variables x, y and z, and use that product to initialize the variable result. Print "The product is" followed by the value of the integer variable result. Nouf Aljaffan (C) 2018
16
Branching/ Selections
Nouf Aljaffan (C) 2018
17
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
18
if Selection statement
if (expression) { Block of statements; } if Selection statement Nouf Aljaffan (C) 2018
19
if…else Selection statement
if (expression) { Block of statements; } else { Block of statements; } if…else Selection statement Nouf Aljaffan (C) 2018
20
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; (expression) ? value1 : value2 Nouf Aljaffan (C) 2018
21
Conditional operator ?:
if condition is true ? then X return value : otherwise Y value; Nouf Aljaffan (C) 2018
22
If..else vs ?: max = x >= y ? x : y ; if ( x > = y ) max = x ;
max = y ; Nouf Aljaffan (C) 2018
23
Nested if...else Statements
Nouf Aljaffan (C) 2018
24
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
25
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" ); break; case 'B' : printf( "Good\n" ); break; case 'C' : printf( "OK\n" ); break; case 'D' : printf( "Mmmmm....\n" ); break; case 'F' : printf( "You must do better than this\n" ); break; default : printf( "What is your grade anyway?\n" ); break; } Nouf Aljaffan (C) 2018
26
Exercise Test whether the value of the variable count is greater than 10. If it is, print “Count is greater than 10.” Nouf Aljaffan (C) 2018
27
Operators Nouf Aljaffan (C) 2018
28
Assignment Operators Nouf Aljaffan (C) 2018
29
Increment and Decrement Operators
Nouf Aljaffan (C) 2018
30
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
31
Nouf Aljaffan (C) 2018
32
Iteration & Looping Nouf Aljaffan (C) 2018
33
Means of iteration: Counter-Controlled Iteration
Sentinel-controlled iteration definite iteration It requires The name of a control variable (or loop counter). The initial value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop. The condition that tests for the final value of the control variable (i.e., whether looping should continue). indefinite iteration The precise number of iterations isn’t known in advance, and The loop includes statements that obtain data each time the loop is performed. Nouf Aljaffan (C) 2018
34
Iteration & Looping Nouf Aljaffan (C) 2018
35
While Iteration Statements
Initialization While( condition){ statements } Nouf Aljaffan (C) 2018
36
While Iteration Statements
Nouf Aljaffan (C) 2018
37
for Iteration Statement
Nouf Aljaffan (C) 2018
38
for Iteration Statement
Nouf Aljaffan (C) 2018
39
do…while Iteration Statement
Nouf Aljaffan (C) 2018
40
break and continue Statements
break and continue are used to alter the flow of control. Nouf Aljaffan (C) 2018
41
break Statement break can be used in a while, for, do…while or switch statement causes an immediate exit from that statement. Program execution continues with the next statement after that while, for, do…while or switch. Nouf Aljaffan (C) 2018
42
continue Statement continue can be used in a while, for or do…while
Cause skipping the remaining statements in that control statement’s body and performs the next iteration of the loop. Nouf Aljaffan (C) 2018
43
Confusing Equality (==) and Assignment (=) Operators
Nouf Aljaffan (C) 2018
44
Logical Operator Logical AND (&&) Operator Logical OR (||) Operator
Logical Negation (!) Operator Nouf Aljaffan (C) 2018
45
Exercises Sum the odd integers between 1 and 99 using a for statement. Use the unsigned integer variables sum and count. Print the integers from 1 to 20 using a while loop and the counter variable x. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character, otherwise print a tab character.] Nouf Aljaffan (C) 2018
46
Conclusion Variables Data Types
Expression represent a single data item such as a number or character Statement caused the computer to carry out some action Branching If statement If..Else statement Nested if statement Switch statement Looping For statement While statement Do while statement Nouf Aljaffan (C) 2018
47
Questions? Nouf Aljaffan (C) 2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.