SOFTWARE DEVELOPMENT IN C CM9041 - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

SOFTWARE DEVELOPMENT IN C CM9041

Description:

Increment and Decrement Operators. count=count 1; == count; count ... Similarly with decrement operators. Using increment and decrement operators in for loops ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 34
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: SOFTWARE DEVELOPMENT IN C CM9041


1
SOFTWARE DEVELOPMENT IN CCM904-1
  • Lecture 2
  • Control Structures in C

2
Last Week
  • C is a procedural language
  • Simple
  • Compiled
  • Cf Java
  • Interaction achieved by printf and scanf

3
This week
  • Modularity
  • Control structures
  • Sequence
  • Selection
  • Repetition

4
Declaring and using a function (modularity)
  • include ltstdio.hgt
  • int sum(int,int) /Function prototype /
  • int main(void)
  • int num14, num27
  • int total
  • totalsum(num1,num2)
  • printf(The sum of the numbers is d\n,
    total)
  • return 0
  •  
  • int sum(int n1, int n2)
  • / Function to find the sum of two numbers /
  • int sumresult
  • sumresultn1n2
  • return sumresult

5
Conditions and Operators
  • Conditions
  •  No boolean type
  • integer 0 false, 1 true
  • e.g.
  • 5gt3 has the value 1
  • 2gt3 has the value 0
  •  Relational Operators
  •  lt, lt, , !, gt, gt
  •  if (x1) printf(OK)
  • if (x1) printf(OK)
  •  LogicalOperators
  •  ! NOT
  • AND
  • OR

6
If Statements
  • if (condition)
  • statement
  • if (condition)
  • statement1
  • else
  • statement 2
  •  
  • where a statement can be a compound statement.

7
Some Examples
  • If the conditional value is non-zero then
    statement1 is executed, otherwise statement2 is
    executed.
  • e.g 1.
  • if (agelt18age gt75)
  • printf(Error)

8
IF.. cf IF.. ELSE
  • e.g.2
  •   if (gender !m gender !f)
  • printf(Error)
  •   e.g 3.
  • if (x3)
  • printf(Yes)
  • else
  • printf(No)
  • e.g 4.
  • if (xlt0) x-x

9
Compound statements
  • e.g 5.
  • if (altb)
  • tempa / compound /
  • ab / statement /
  • btemp

10
Compound Statements
  • e.g7.
  • if (agtb)
  • maxa
  • flag1
  • else
  • maxb
  • flag2

e.g 6. if (agtb) maxa else
maxb
11
Validation Example
  • include ltstdio.hgt
  • int main(void)
  • int valid,age
  • char gender
  • scanf(dc,age, gender)
  • valid1
  • if (agelt18agegt65)
  • valid0
  • printf(\nAGE ERROR\n)
  • if (gender !m gender !f)
  • valid0
  • printf(\nGENDER ERROR\n)
  • if (valid)

12
If.. Else Chains
  • e.g.
  •   if (month1)
  • printf(January)
  • else if (month2)
  • printf(February)
  • else if (month3)
  • printf(March)
  • else
  • printf(Error)

13
Dangling else problem
  • 1. if (xgt0)
  • if (yA)
  • printf(Positive A)
  • else
  • printf(Negative)
  •  
  • 2. if (xgt0)
  • if (yA)
  • printf(Positive A)
  • else
  • printf(Negative)

14
Switch Statement
  • Provides a multi-way decision by testing whether
    an expression matches one of a number of constant
    values.
  • General Form
  •   switch (expression)
  • case const_expr1statements
  • case const_expr2statements
  • .
  • defaultstatements

15
Example Switch Statement
  • Classify an input character c as digit, white
    space or other.
  •  
  • switch (c)
  • case
  • case \n
  • case \t nwhitenwhite1
  • break
  • case 0 case 1 case 2 case 3
  • case 4 case 5 case 6 case 7
  • case 8 case 9
  • ndigitndigit1
  • break
  • default nothernother1

16
Iteration
  • Non-deterministic
  • No pre-set number of iterations
  • Use
  • While
  • Do While
  • Deterministic
  • Known number of iterations
  • Use
  • For

17
Non-deterministic Loop While statement
  • Format
  • while (condition)
  • statement
  • where the statement can be a compound
  • statement
  • e.g.
  • sum0
  • scanf(d,x)
  • while (x!-9999)
  • sumsumx
  • scanf(d,x)

18
Read Ahead Technique
  • Input data 1 4 5 6 9999
  •  
  • Unwinding the iteration
  • Read num 1
  • Process num
  • Read num 4
  • Process num
  • Read num 5
  • Process num
  • Read num 6
  • Process num
  • Read num -9999

19
  • The iterated step is
  • Process num
  • Read num
  •   
  • Which leads to
  •  
  • Read num
  • While num ! -9999
  • Process num
  • Read num

20
Example While Loop
  • Define Problem
  • Problem
  • Read a series of integers terminated by 9999.
    Count the number of integers that are greater
    than 150 and find the largest and smallest
    numbers greater than 150.Print a message if none
    of the integers are greater than 150.
  •  
  • Example Input
  • 100 200 10 80 400 150 6 -9999
  •  
  • Expected Output
  •   Number of valuesgt1502
  • Max400
  • Min200

21
  • 2. Design
  • Initialise
  • count0
  • max0
  • min99999
  • Read first number
  • Input values and process
  • While number ltgt -9999 loop
  • If number gt 150 then
  • Increment count
  • Check if new largest
  • If numbergtmax then
  • maxnumber
  • Check if new smallest
  • If number ltmin then
  • minnumber
  • Read next number
  • End loop

22
  • include ltstdio.hgt
  • int main(void)
  • /program description/
  • int num,max,min,count
  • count0
  • min99999
  • max0
  • scanf(d, num)
  • while (num !-9999)
  • if (numgt150)
  • count
  • if (numgtmax) maxnum
  • if (numltmin) minnum
  • scanf(d,num)

3. Code
23
Non-deterministic loop Do While loop
  • General Form
  • do
  • statement
  • while (expression)
  •  
  • Unlike the while statement, the expression in the
    do while comes after statement (the loop
    body) which is thus executed at least once.
  • Note that it is very easy to make mistakes with
    the do while loop, although it may compile and
    appear to produce valid code.

24
Deterministic Loop For statement
  • For statement format
  • for (initialising list condition altering list)
  • statement
  • initialising list Executed once
  • altering list Executed at the end of each
    iteration
  • condition Evaluated and checked at the
  • start of each
    iteration i.e. a
  • leading decision

25
ExampleFor loop
  • for (count1countlt10countcount2)
  • printf(3d,count)
  • Gives output 1 3 5 7 9
  • Equivalent While loop
  • count1
  • while (countlt10)
  • printf(3d,count)
  • countcount2

26
Increment and Decrement Operators
  • countcount1 count count
  • countcount-1 --count count--
  • Increment can occur before or after statement
    actioned
  • count3
  • xcount count 4, x 4
  • count3
  • xcount count4, x3
  • Similarly with decrement operators.

27
Using increment and decrement operators in for
loops
  • for (count0countlt10count)
  • printf(3d,count)
  • Prints 1 2 3 4 5 6 7 8 9 10
  • for (count0countlt10count)
  • printf(3d,count)
  • Prints 0 1 2 3 4 5 6 7 8 9 10
  • Classic Error
  •  for (i0ilt10i)
  • printf(d, i)
  •  

28
Example 1 Deterministic Loop
  • Problem Print a multiplication table of size N.
  • e.g.
  • 1 2 3 4 5
  • 2 4 6 8 10
  • 3 6 9 12 15 where N5
  • 4 8 12 16 20
  • 5 10 15 20 25
  •  
  • The structure of the data determines the
    structure of the program

29
Design
  • Data
  • Repetition of rows
  • Repetition of values (rowcolumn)
  •  
  • Design
  • Read N
  • Perform N times
  • Print a row
  • Perform N times
  • Print a value

30
Code
  • include ltstdio.hgt
  • / Reads an integer N and prints the
  • N times multiplication table /
  • int main(void)
  • int row, col, n
  • printf(\nPlease enter the value of N )
  • scanf(d, n)
  • printf(\n\n)
  • for (row1rowltnrow)
  • for (col1colltncol)
  • printf(6d, rowcol)
  • printf(\n)
  • return 0

31
Deterministic Loops Example 2
  • Problem
  • Write a program to encode lower case letters in a
    message by performing a cyclic shift one place to
    the right.
  •  
  • Example Input
  • The cat sat on the mat
  • Example Output
  • Uif dbu tbu po uif nbu

32
Code
  • include ltstdio.hgt
  • / Character processing example /
  • int main(void)
  • char c
  • while (scanf(c, ch) ! EOF)
  • if (ch z)
  • printf(c, a)
  • else if (ch gt a ch lty)
  • printf(c, ch1)
  • else
  • printf(c, ch)
  • return 0

33
Next week
  • Simple software development lifecycle
  • Program design
  • Testing
Write a Comment
User Comments (0)
About PowerShow.com