CHTP5e 03 Structured
CHTP5e 03 Structured
3
Structured Program
Development in C
OBJECTIVES
In this chapter you will learn:
Basic problem-solving techniques.
To develop algorithms through the process of
top-down, stepwise refinement.
To use the if selection statement and if...else
selection statement to select actions.
To use the while repetition statement to execute
statements in a program repeatedly.
Counter-controlled repetition and sentinel-controlled
repetition.
Structured programming.
The increment, decrement and assignment operators.
3.1 Introduction
3.2 Algorithms
3.3 Pseudocode
3.4 Control Structures
3.5 The if Selection Statement
3.6 The if...else Selection Statement
3.7 The while Repetition Statement
3.1 Introduction
Before writing a program:
– Have a thorough understanding of the problem
– Carefully plan an approach for solving it
While writing a program:
– Know what “building blocks” are available
– Use good programming principles
3.2 Algorithms
Computing problems
– All can be solved by executing a series of actions in a
specific order
Algorithm: procedure in terms of
– Actions to be executed
– The order in which these actions are to be executed
Program control
– Specify order in which statements are to be executed
3.3 Pseudocode
Pseudocode
– Artificial, informal language that helps us develop
algorithms
– Similar to everyday English
– Not actually executed on computers
– Helps us “think out” a program before writing it
- Easy to convert into a corresponding C++ program
- Consists only of executable statements
Enter grade: 98
Enter grade: 76
Enter grade: 71
Enter grade: 87
Enter grade: 83
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81
Assignment Sample
Explanation Assigns
operator expression
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+= c += 7 C = c + 7 10 to c
-= d -= 4 D = d - 4 1 to d
*= e *= 5 E = e * 5 20 to e
/= f /= 3 F = f / 3 2 to f
%= g %= 9 G = g % 9 3 to g
If c equals 5, then
printf( "%d", ++c );
– Prints 6
printf( "%d", c++ );
– Prints 5
– In either case, c now has the value of 6
When variable not in an expression
– Preincrementing and postincrementing have the same effect
++c;
printf( “%d”, c );
– Has the same effect as
c++;
printf( “%d”, c );
5
6
6
2007 Pearson Education,
Inc. All rights reserved.
75