Professional Documents
Culture Documents
Lecture 06 - Programming Fundamentals
Lecture 06 - Programming Fundamentals
1
Lecture 6 Objectives
• Solve examples
Lecture 6 Contents
6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.1 Introduction
• Note: One problem can have more than one solution i.e.
algorithms.
____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 6 6.1 Introduction
Algorithm
____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 6 Contents
6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.2 Control Structures
Introduction
Control Structures
____________________________________________________________________________________________________________________________________
I
Radzi, N.H.M, Hashim, S.Z.M. and Samsuri, P. (2001). Pengaturcaraan C. Malaysia: McGraw-Hill.
2
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 6 6.2 Control Structures
(a) Sequential Structure
•A series of steps or
statements performed in the 1. Start
order they are written in an 2. Statement_1
algorithm. 3. Statement_2
4. Statement_3
•Pseudo-code: mark the
beginning and end of a block n. Statement_n-1
of statements. n+1. End
Lecture 6 6.2 Control Structures
statement_2 statement
12 > 5 is true
> Greater than
< Less than 7 <= 5 is false
>= Greater than or equal to
if x is 10, then
<= Less than or equal to
x == 10 is true
== Equal to
x != 8 is true
!= Not equal to
x == 8 is
false
Lecture 6 6.2 Control Structures
Selective Structures
(statement)
if if...else switch
_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
if selection statement
: TRUE
n. if condition condition
condition
n.1 statement FALSE
n+1. end_if
: statement
Lecture 6 6.2 Control Structures
• Example: if
Suppose the passing grade on an exam is 60.
The pseudo-code statement:
_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
• Example: if...else
Suppose the passing grade on an exam is 60.
The pseudo-code statement:
2) Nesting:
• a connecting control statement can be within
another control statement.
_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
Stacked if
False
Age > 18?
Gender == True
‘F’ ?
Nested if
• if within if FALSE
• Pseudo-code: test1
TRUE
• Example:
Lecture 6 6.2 Control Structures
Nested if...else
• if...else within if
• The pseudo-code statement:
Algorithm: nested if
:
n.if condition
:
n.m if condition
n.m.1 statement
:
n+1. end_if
:
_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________
Nested if...else
x
FALSE
condition
TRUE
statement
____________________________________________________________________________________________________________________________________
* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall
Lecture 6 6.2 Control Structures
_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
Activity 6.1:
_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
TRUE
grade≥80 Print “B”
FALSE
TRUE
grade≥70 Print “C”
FALSE
TRUE
Grade≥60 Print “D”
FALSE
Print”F”
Lecture 6 6.2 Control Structures
(c) Repetition Structure
x Start
initialization cnt=0
FALSE
FALSE
condition cnt<5
TRUE TRUE
increment cnt=cnt+1
y End
(a) (b)
• Example Loop
Lecture 6 Contents
6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.3 Summary
____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 6 6.3 Summary
____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education