Structured Program Development in C Dr Nouf Aljaffan











![Variable declaration The general format for a declaration is type variable-name [=value] ; char Variable declaration The general format for a declaration is type variable-name [=value] ; char](https://slidetodoc.com/presentation_image_h/031d83344bdb9d2e550b35d936f1b880/image-12.jpg)



































- Slides: 47

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

Outlines Variables & Data types Control structures Additional Operators Nouf Aljaffan (C) 2018

Variables and Data. Types Nouf Aljaffan (C) 2018

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

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

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

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

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 n. Total 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 = n. Total / 3. 0; When data is passed to or returned from functions. Nouf Aljaffan (C) 2018

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

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

Pop quiz (correct/incorrect): int money$owed; int total_count int score 2 int 2 ndscore int long Nouf Aljaffan (C) 2018
![Variable declaration The general format for a declaration is type variablename value char Variable declaration The general format for a declaration is type variable-name [=value] ; char](https://slidetodoc.com/presentation_image_h/031d83344bdb9d2e550b35d936f1b880/image-12.jpg)
Variable declaration The general format for a declaration is type variable-name [=value] ; char Nouf Aljaffan (C) 2018 x; /∗ uninitialized ∗/ x=’A’; /∗ intialized to ’A’∗/ x=’A’, y=’B’; /∗multiple variables initialized ∗/ x=y=’Z’; /∗multiple initializations ∗/

Declaration of answer and assigning it a value The general form of the assignment statement is: variable = expression; Nouf Aljaffan (C) 2018

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=3. 14159; Nouf Aljaffan (C) 2018

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

Branching/ Selections Nouf Aljaffan (C) 2018

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

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

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

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) ? value 1 : value 2 Nouf Aljaffan (C) 2018

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

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

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

switch statement switch( expression ) { case constant-expression 1: statements 1; [case constant-expression 2: statements 2; ] [case constant-expression 3: statements 3; ] [default : statements 4; ] } 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

switch( expression ) { switch statement } case constant-expression 1: statements 1; break; [case constant-expression 2: statements 2; break; ] [case constant-expression 3: statements 3; break; ] [default : statements 4; ] switch( Grade ) { case 'A' : printf( "Excellentn" ); break; case 'B' : printf( "Goodn" ); break; case 'C' : printf( "OKn" ); break; case 'D' : printf( "Mmmmm. . n" ); break; case 'F' : printf( "You must do better than thisn" ); break; default : printf( "What is your grade anyway? n" ); break; } Nouf Aljaffan (C) 2018

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

Operators Nouf Aljaffan (C) 2018

Assignment Operators Nouf Aljaffan (C) 2018

Increment and Decrement Operators Nouf Aljaffan (C) 2018

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

Nouf Aljaffan (C) 2018

Iteration & Looping Nouf Aljaffan (C) 2018

Means of iteration: Counter-Controlled Iteration Sentinel-controlled iteration definite iteration indefinite iteration It requires The precise number of iterations isn’t known in advance, and The loop includes statements that obtain data each time the loop is performed. 1. The name of a control variable (or loop counter). 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop. 4. The condition that tests for the final value of the control variable (i. e. , whether looping should continue). Nouf Aljaffan (C) 2018

Iteration & Looping Nouf Aljaffan (C) 2018

While Iteration Statements Nouf Aljaffan (C) 2018 Initialization While( condition){ statements }

While Iteration Statements Nouf Aljaffan (C) 2018

for Iteration Statement Nouf Aljaffan (C) 2018

for Iteration Statement Nouf Aljaffan (C) 2018

do…while Iteration Statement Nouf Aljaffan (C) 2018

break and continue Statements break and continue are used to alter the flow of control. Nouf Aljaffan (C) 2018

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

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

Confusing Equality (==) and Assignment (=) Operators Nouf Aljaffan (C) 2018

Logical Operator Logical AND (&&) Operator Logical OR (||) Operator Logical Negation (!) Operator Nouf Aljaffan (C) 2018

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

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

Questions? Nouf Aljaffan (C) 2018