Introduction to C Programming CE003121 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Introduction to C Programming CE003121

Description:

Atomisation of complex problem into manageable units possibly reusable in ... printf('Better luck at the resit'); break; case 4: case 5: case 6: case 7: case 8: ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 22
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming CE003121


1
Introduction to C ProgrammingCE00312-1
  • Lecture 3
  • Control Structures in C

2
Program Structure
  • Modularity
  • Atomisation of complex problem into manageable
    units possibly reusable in other complex
    problems
  • Functions
  • Control structures
  • Basic mechanics of process deconstruction
  • Most problems can be expressed as combinations of
  • Sequence
  • Selection
  • Repetition

3
Modularity in a C Program
  • The Function main
  • In C, all code is packaged in functions
  • main is the function that the system calls when
    you run your program
  • Every program must have one and only one main
  • All functions may return a value

4
Function Components
  • A function definition consists of two
  • parts
  •  A Specification ( or header or interface)
    Specifies the function name, the return value and
    argument list
  • A Body ( or block or implementation) Code to be
    executed when the function is called. A sequence
    of statements enclosed in braces

5
Declaring and using a function
  • 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

6
Program Control - Sequence
  • Default control structure
  • Program execution occurs sequentially unless
    otherwise directed
  • Each step should be the logical consequence of
    the preceding one
  • Statements should be arranged such that they
    occur in the right order

7
Program control - Selection
  • Selection
  • Implemented using
  • If..
  • Single branch
  • If..else..
  • Multiple branching
  • Ordinal relationship in branch determination
  • Switch..case...
  • Multiple branching
  • Category branch determination

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

9
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

10
Some Examples
  • If the conditional value is non-zero then
    statement1 is executed, otherwise execution
    passes to next statement after if construct.
  • e.g. 1
  • if (agelt18age gt75)
  • printf(Error)
  • e.g. 2
  •   if (gender !m gender !f)
  • printf(Error)
  • e.g. 3
  • if (xlt0) x-x

11
IF.. cf IF.. ELSE
  • If the conditional value is non-zero then
    statement1 is executed, otherwise statement2 is
    executed.
  •  
  • e.g. 4
  • if (x3)
  • printf(Yes)
  • else
  • printf(No)

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

13
Compound Statements
  • e.g. 7
  • if (agtb)
  • maxa
  • flag1
  • else
  • maxb
  • flag2

e.g. 6 if (agtb) maxa else
maxb
14
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)
printf(\nData OK\n) else printf(\nData
Error\n) return 0
15
If.. Else Chains
  • e.g.
  •   if (month1)
  • printf(January)
  • else if (month2)
  • printf(February)
  • else if (month3)
  • printf(March)
  • else
  • printf(Error)

16
Dangling else problem
  • 1. if (xgt0)
  • if (yA)
  • printf(Positive A)
  • else
  • printf(Negative)
  •  
  • Which condition determines the outcome?
  • 2. if (xgt0)
  • if (yA)
  • printf(Positive A)
  • else
  • printf(Negative)
  • A is only printed if x gt and y is A

17
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 c1statements
  • case c2statements
  • .
  • defaultstatements

18
switch Statement
  • expression is any integer valued expression
  • c1, c2 are integer valued constants
  • (remember integer include chars!)
  • statements are any C statements
  • default is optional
  • c1, c2 must be unique (i.e same constant cannot
    appear twice)
  • each case can have only one value (not range)

19
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

20
Integer example switch
  • switch (gradePoint)
  • case 1
  • printf (You have failed badly)
  • break
  • case 3
  • printf(A narrow fail\n)
  • case 2
  • printf(Better luck at the resit)
  • break
  • case 4 case 5 case 6 case 7 case 8
  • printf(Well done, you passed)
  • break
  • default
  • printf(Not a valid grade point)

21
Switch
  • after switch is evaluated control falls to
    matching case
  • if no match, go to default if present
  • terminate with break or by reaching end of switch
    block
  • can group cases together by omitting break -
    careful!
  • No need for ... within case
Write a Comment
User Comments (0)
About PowerShow.com