ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications Lecture 5 Structured Programming in C Paul Lin Professor of Electrical & Computer Engineering Techno...
Author: Magdalen Cooper
9 downloads 2 Views 326KB Size
ECET 264 C Programming Language with Applications Lecture 5 Structured Programming in C Paul Lin Professor of Electrical & Computer Engineering Technology

http://www.etcs.ipfw.edu/~lin

Lecture 5 - Paul Lin

1

Structured Programming in C 

   

Algorithms, Pseudo Code, Control Structures The If Selection Statement The If .. else Selection Statement The while Repetition Statement More Operators of the C Language • Arithmetic, Relational, Logical, Assignment, Increment and Decrement Operators Lecture 5 - Paul Lin

2

1

Structured Programming in C 

Examples • Example 1: F to C Temperature Conversion • Example 2: Using Increment and Decrement Operators • Example 3: Using Relational Operators

Lecture 5 - Paul Lin

3

Structured Programming in C 

Algorithms1 • A procedure for solving a problem in terms of





The actions to be executed, and



The order in which these actions are to be executed

Pseudo Code1 • An artificial and informal language that help programmers to develop algorithms 1

Deitel & Deitel, C How to Program, 5th Ed, Prentice Hall, 2007 Lecture 5 - Paul Lin

4

2

Structured C Programming 



Control Structures are used to specify what the next statement is executed (either in sequence or by transfer of control). Three Control Structures • Sequence structure – one after the other in the order • Selection structure – if , if .. else, switch • Repetition structure – while, do .. while, for

Lecture 5 - Paul Lin

5

Structured Programming in C 

Structured Program Development in C • Control Structures  

Sequence Structure Selection Structures if and if… else, switch • if Statements • if …else Statements • nested if …else Statements

 

Compound Statements While Repetition Structure

• Formulating Algorithms 

Example: Reading Student Grades and Calculating Class Average Lecture 5 - Paul Lin

6

3

Structured Programming in C 

Flowchart • A graphical representation of an algorithm or a portion of an algorithm • Symbols 

Oval – Begin, End



Rectangle – Action



Flow lines

Lecture 5 - Paul Lin

7

Sequence Structure Statements in a program are executed one after the other in the order in which they are written Sequence Flowchart Add grade to total

Add 1 to counter

Lecture 5 - Paul Lin

8

4

Selection Structures Selection structure:  if  if .. else  switch

9

Lecture 5 - Paul Lin

if Statements 

if Statement • Single selection statement – a single-entry and single-exit structure • Either performs an action when the condition is true; OR skip/ignore the action when the condition is false • Syntax: if(exp1) s1; True • Example: if (grade >= 60) printf(“Passed \n”); Lecture 5 - Paul Lin

Grade >=60 ? False

Print “Passed”

10

5

if … else Statements if … else Statement Double selection statement It selects between two different actions. Syntax: if(exp) s1; else s2; True Example: If (grade >= 60) printf(“Passed \n”); else printf(“Failed \n”);

Grade >=60 ? False

Print “Passed”

Print “Failed”

Lecture 5 - Paul Lin

11

Nested if … else Statements 

Nested if … else Statement • Multiple selection statements • It takes an action when evaluating each selection statement in sequence until the condition is first met. • the properties of the else-if:  We use each else-if to select one from among the alternatives.  When any of the else-if conditions is satisfied, all the remaining else-if's are skipped.  The last else, the one without if is the default case. Lecture 5 - Paul Lin

12

6

Nested if … else Statements

(continue)

nested if … else Statements 



Syntax: if(exp1) s1; else if(exp2) s2; else s3; True Example: Print Grade >=90 “A” if (grade >= 90) printf (“A \n”); ? else False if ((grade = 80)) printf(“B \n”); Grade < 90 Print Print else And “B” True Grade >= 80 False “C” printf(“C \n”); ?

Lecture 5 - Paul Lin

13

Compound Statements 

Compound Statements • More than one statements are in the body of an if • Require a pair of brace { } to enclose the set of statements

Example: if (grade >= 60) True Print Grade >=60 “Passed” printf(“Passed \n”); ? else { False A pair of printf(“Failed \n”); braces printf(“Take the course again \n”); Print “Failed” “Take the …” } Lecture 5 - Paul Lin

14

7

while Statements 

while Statement • The "while" loop is a generalized looping structure that employs a variable or expression for testing the ending condition. • A repetition statement that allows an action to be repeated while some conditions remains true. • Variables used in the loop control testing must be initialized • Testing for the termination condition is done at the top of the while() loop

15

Lecture 5 - Paul Lin

While Statements

(continue)

• The body of while statement can be a single statement or compound statements Syntax: Form 1: Simple Statement while (condition) statement; Form 2: Compound Statements while (condition) { s1; s2; s3; …..} Lecture 5 - Paul Lin

16

8

while Statements 

Simple Statement : item = 2; while (item < 100) item = 2 * item;



Compound statement: item = 2; while (item < 100) { item = 2 * item; item = item +1; }

(continue)

True item< 100 ? False

17

Lecture 5 - Paul Lin

while Statements 

 

Item=2*item Item=item+1

(continue)

Relational and logical operators are used in the while statement for evaluating the true or false value of the ending condition zero is used to represent false and a value of nonzero means true in C Other examples of using these operators are listed below: while(a > b) /* a greater than b */ while(a , =, b Assignment Operator: =, +=, -=, *=, … • Add and assign: i = i + 2 or i += 2 Increment (++) and Decrement (--) Operator • n++, post increment n by 1 • --num, pre-decrement num by 1 Logical Operators: !, &&, ||, … • For constructing complex statement











Lecture 5 - Paul Lin

19

Arithmetic Operators 

Arithmetic Operators: +, -, *, /, % Calculate data: x = y + z; a = 3 * b;

Operator + * /

Meaning Example add z = x + y; sub z = x - y; multiply x = y * 20; divide x = 10 / 3; x = 3; (integer division) x = 10.0 / 3.0; (floating point number division x = 3.333333 ) % remainder x = 10 % 3; (remainder is 1) Lecture 5 - Paul Lin

20

10

Relational Operators Operator < >= == != Example:

Meaning

Example

less than x < max; less than or equal to x min; greater than or equal to x >= min; equal to x == 10; not equal to min != max; if (x < max) printf(“x is smaller\n”); else printf(“max is smaller”);

Value returned from a relational expression may be 0 false, or 1 true (nonzero) A value 0 is treated as false, any other nonzero value is true 21

Lecture 5 - Paul Lin

Assignment Operators Operator

Meaning

Example

= += -= *= /=

assignment add and assign sub and assign multiply and assign divide and assign

%=

remainder and assign

&=

bit-wise AND and assign

|=

bit-wise OR and assign

^=

bit-wise EX-OR and assign

a = 10; a += 2; a = a + 2; a - = 1; a = a - 1; a *= 2; a = a * 2; a /= 2; a = a / 2; a %= 2; a = a % 2; b &= 0001; b = b & 0001; b |= 1110; b = b | 1110; b ^= a; b=b^ a;

Lecture 5 - Paul Lin

22

11

Example 1 F to C Temperature Conversion /* ftoc.c - Define float variables for temperature conversion, then calculate the expression c = (5/9)*(f – 32) for f = 0 and 212 F. */ #include void main() { float c, f; f =32.0; /* assign float constant */ c = (5.0/9.0) * (f – 32.0); printf(“%f Degree F = %f Degree C\n\n”, f, c); f =212.0; c = (5.0/9.0) * (f – 32.0); printf(“%f Degree F = %f Degree C\n\n”, f, c); } Lecture 5 - Paul Lin

23

Example 1 (continue) F to C Temperature Conversion

Output 32.000000 Degree F = 0.000000 Degree C 212.000000 Degree F = 100.000000 Degree C

Lecture 5 - Paul Lin

24

12

Increment and Decrement Operators 



Increment and Decrement Operators (for integer only) ++ increment c++ post-increment ++c pre-increment -decrement count -post-decrement --count pre-decrement Example of post-increment and pre-increment:

c = 1; a = c++; /* a = 1, c = 2 */ /* a = 1 = c, then c is incremented by 1; now c=2 a=1 */ d = ++c; /* c = incremented by 1 first, then assign the value c = 3 to d; c = 3, d = 3 */ Lecture 5 - Paul Lin

25

Example 2 Using Increment and Decrement Operators /* incdec.c - This program is used to observe the action of the increment and decrement operators as applied to variables in different expressions.*/ #include void main() { int a = 0, b = 0, c = 0; printf( "Before the execution: a = %d b = %d c = %d\n", a, b, c ); a = ++b + ++c; /* a = 2, b = 1, c = 1*/ printf("After the execution\n"); printf( "a = ++b + ++c => a = %d, b = %d c = %d\n", a, b, c ); printf( "\nBefore the execution: a = %d b = %d c = %d\n", a, b, c ); Lecture 5 - Paul Lin

26

13

Example 2 (continue) Using Increment and Decrement Operators a = b++ + c++; /* a = 2, b = 2, c = 2 */ printf("After the execution\n"); printf( "a = b++ + c++ => a = %d, b = %d b, c ); printf( "\nBefore the execution: a = %d b %d\n", a, b, c ); a = ++b + c++; /* b = 3, c = 3, a = 5 */ printf("After the execution\n"); printf( "a = ++b + c++ => a = %d, b = %d b, c ); printf( "\nBefore the execution: a = %d b %d\n", a, b, c ); a = b-- + --c; printf("After the execution\n"); printf( "a = b-- + --c => a = %d, b = %d b, c );

c =%d\n", a, = %d c =

c =%d\n", a, = %d c =

c =%d\n", a,

} Lecture 5 - Paul Lin

27

Example 2 (continue) Using Increment and Decrement Operators

Lecture 5 - Paul Lin

28

14

Logical Operators Operator && || !

Meaning AND OR NOT (logical negation)

Example: (x = -3) -3 0 3 - Order of evaluation: Left to right; Stops when result known 29

Lecture 5 - Paul Lin

Logical Operators Operator && || !

(continue)

Meaning AND OR NOT (logical negation)

Example: ! (( x = -3)) equivalent to (x >= 3) || (x y)); printf("\n x < y = %d", (x < y)); printf("\n x == y = %d", (x == y)); printf("\n x != y = %d", (x != y)); printf("\n x + y = %f", x + y); Lecture 5 - Paul Lin

32

16

Example 3: Using Relational And Assignment Operators (continue)

printf("\n x - y = %f", x - y); printf("\n x * y = %f", x * y); printf("\n x / y = %f", x / y); printf("\n 10 /3 = %d", 10 / 3); printf("\n 10 %%3 = %d", 10 % 3); }

Lecture 5 - Paul Lin

33

Example 3: Using Relational And Assignment Operators (continue)

Output: please enter a value for x = 10 please enter a value for y = 20 x>y =0 x