0% found this document useful (0 votes)
11 views24 pages

Eee0115 2

Chapter 3 of 'C How to Program' by Deitel & Deitel covers structured program development in C, focusing on algorithms, pseudocode, and control structures such as selection and repetition statements. It explains the use of if, if-else, and while statements, along with examples of counter-controlled and sentinel-controlled repetition. Additionally, it discusses assignment operators and increment/decrement operators for simplifying expressions.

Uploaded by

youngkej1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

Eee0115 2

Chapter 3 of 'C How to Program' by Deitel & Deitel covers structured program development in C, focusing on algorithms, pseudocode, and control structures such as selection and repetition statements. It explains the use of if, if-else, and while statements, along with examples of counter-controlled and sentinel-controlled repetition. Additionally, it discusses assignment operators and increment/decrement operators for simplifying expressions.

Uploaded by

youngkej1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

EEE0115

Chapter 3: Structured
Program Development in C

C How to Program
1 Deitel & Deitel
2

 Algorithms
 Pseudocode
 Control Structures
 The if Selection Statement
 The if...else Selection Statement
 The while Repetition Statement
3

Algorithms
 Algorithm is defined as the actions to be
executed in a specific order to solve a
given problem.
 Program control is an important concept
and it defiens the specific order in which
statements are to be executed.
4

Pseudocode
 Artificial or informal language which is
used to develop algorithms
 Pseudocodes are not executed on
computers.
 They are similar to everyday English.
 Pseudocodes can be converted easily to C
program.
5

Control Structures
 All programs can be written in terms of 3
control structures:
 Sequence structures: Statements executed
sequentially. It is default control structure.
 Selection structures: if, if-else, switch.
 Repetition structures: while, do-while, for.
6

The if selection statement


 Used to choose among alternative
actions:
 Pseudocode of if:
 If student’s grade is less than 50
print “Failed”
 If the condition returns true, print
statement is executed.
 If the condition returns false, the body of if
is not executed. Program control goes to
next statement after if block.
7

The if selection statement


 Pseudocode statement is transferred to C
program as follows:
if(grade<50)
printf("Passed\n" );
 Indentation improves program readibility.
 C ignores whitespace characters (space,
tab, and newline).
8

The if-else selection statement


 This statement specifies an action for
both true condition and false condition.
 Psuedocode of if-else:

If student’ grade is less than 50


Print “Failed”
else
Print “Passed”
9

The if-else selection statement


 C code :

If (grade<50)
printf(“Failed\n”);
else
printf(“Passed\n”);
 Ternary conditional operator (?:)
 It has three arguments (condition, value
if true, value if false)
 printf( "%s\n", grade >= 60 ?
"Passed" : "Failed" );
 grade >= 60 ? printf( “Passed\n” ) :
printf( “Failed\n” );
10

The if-else selection statement


 Nested if-else statements
 They are used to test for multiple cases.
 if-else selection statements are placed inside
other if-else selection statements.
 Whenever a condition returns true, rest of the
statements are skipped.
11

The if-else selection statement


 Compound statement is defined as a set
of statements within a pair of braces
 Compound statements with declarations
are defined as block.

if(a>10)
{
printf("Your value is greater than 10\n");
printf("Enter a new value\n");
}
12

The while repetition statement


 Repetitionstructures are used
whenever a number of actions to be
repeated while a condition remains
true.
 Example pseudocode:
While varible x is less than 10
Add x to current some
 Example C code:
while(x<10)
sum=sum+x;
13

Counter-Controlled Repetition
1 /* Fig. 3.6: fig03_06.c
2 Class average program with counter-controlled repetition */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int counter; /* number of grade to be entered next */
9 int grade; /* grade value */
10 int total; /* sum of grades input by user */
11 int average; /* average of grades */
12
13 /* initialization phase */
14 total = 0; /* initialize total */
15 counter = 1; /* initialize loop counter */
16
17 /* processing phase */
18 while ( counter <= 10 ) { /* loop 10 times */
19 printf( "Enter grade: " ); /* prompt for input */
20 scanf( "%d", &grade ); /* read grade from user */
21 total = total + grade; /* add grade to total */
22 counter = counter + 1; /* increment counter */
23 } /* end while */
14

Counter-Controlled Repetition
24
25 /* termination phase */
26 average = total / 10; /* integer division */
27
28 printf( "Class average is %d\n", average ); /* display result */
29
30 return 0; /* indicate program ended successfully */
31
32 } /* end function main */

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
15

Algorithms (Top-down, stepwise refinement)


 Programs usually have three phases:
 Initialization
 Processing
 Termination
16

Algorithms (Top-down, stepwise refinement)


A class-averaging program (unknown
number of students)
1 /* Fig. 3.8: fig03_08.c
2 Class average program with sentinel-controlled repetition */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int counter; /* number of grades entered */
9 int grade; /* grade value */
10 int total; /* sum of grades */
11
12 float average; /* number with decimal point for average */
13
14 /* initialization phase */
15 total = 0; /* initialize total */
16 counter = 0; /* initialize loop counter */
17
18 /* processing phase */
19 /* get first grade from user */
20 printf( "Enter grade, -1 to end: " ); /* prompt for input */
21 scanf( "%d", &grade ); /* read grade from user */
22
17

Algorithms (Top-down, stepwise refinement)


A class-averaging program (unknown
number of students)
23 /* loop while sentinel value not yet read from user */
24 while ( grade != -1 ) {
25 total = total + grade; /* add grade to total */
26 counter = counter + 1; /* increment counter */
27
28 /* get next grade from user */
29 printf( "Enter grade, -1 to end: " ); /* prompt for input */
30 scanf("%d", &grade); /* read next grade */
31 } /* end while */
32
33 /* termination phase */
34 /* if user entered at least one grade */
35 if ( counter != 0 ) {
36
37 /* calculate average of all grades entered */
38 average = ( float ) total / counter; /* avoid truncation */
39
40 /* display average with two digits of precision */
41 printf( "Class average is %.2f\n", average );
42 } /* end if */
43 else { /* if no grades were entered, output message */
44 printf( "No grades were entered\n" );
45 } /* end else */
46
47 return 0; /* indicate program ended successfully */
48
49 } /* end function main */
18

Algorithms (Top-down, stepwise refinement)


A class-averaging program (unknown
number of students)

Enter grade, -1 to end: 75


Enter grade, -1 to end: 94
Enter grade, -1 to end: 97
Enter grade, -1 to end: 88
Enter grade, -1 to end: 70
Enter grade, -1 to end: 64
Enter grade, -1 to end: 83
Enter grade, -1 to end: 89
Enter grade, -1 to end: -1
Class average is 82.50

Enter grade, -1 to end: -1


No grades were entered
19

Algorithms (Top-down, stepwise refinement)


 Nested Control Structures
1 /* Fig. 3.10: fig03_10.c
2 Analysis of examination results */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 /* initialize variables in definitions */
9 int passes = 0; /* number of passes */
10 int failures = 0; /* number of failures */
11 int student = 1; /* student counter */
12 int result; /* one exam result */
13
14 /* process 10 students using counter-controlled loop */
15 while ( student <= 10 ) {
16
17 /* prompt user for input and obtain value from user */
18 printf( "Enter result ( 1=pass,2=fail ): " );
19 scanf( "%d", &result );
20
21 /* if result 1, increment passes */
22 if ( result == 1 ) {
23 passes = passes + 1;
24 } /* end if */
25 else { /* otherwise, increment failures */
26 failures = failures + 1;
27 } /* end else */
28
29 student = student + 1; /* increment student counter */
30 } /* end while */
20

Algorithms (Top-down, stepwise refinement)


 Nested Control Structures
31
32 /* termination phase; display number of passes and failures */
33 printf( "Passed %d\n", passes );
34 printf( "Failed %d\n", failures );
35
36 /* if more than eight students passed, print "raise tuition" */
37 if ( passes > 8 ) {
38 printf( "Raise tuition\n" );
39 } /* end if */
40
41 return 0; /* indicate program ended successfully */
42
43 } /* end function main */

Enter Result (1=pass,2=fail): 1


Enter Result (1=pass,2=fail): 2
Enter Result (1=pass,2=fail): 2
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 2
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 2
Passed 6
Failed 4
(continued on next slide… )
21

Algorithms (Top-down, stepwise refinement)


 Nested Control Structures

(continued from previous slide…)


Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 2
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Enter Result (1=pass,2=fail): 1
Passed 9
Failed 1
Raise tuition
22

Assignment Operators
 Assignment operators can be used
instead of assignment expressions:
a=a+5 can be written as a+=5
a=a-5 can be written as a-=5
a=a*5 can be written as a/=5
a=a/5 can be written as a+=5
a=a%5 can be written as a%=5
23

Increment and Decrement Operators


 Increment operator (c++) can be used
instead of c=c+1
 Decrement operator (c--) can be used
instead of c=c-1
 c++ and c-- postincrement operators
 Expression executes before the variable is
changed
 ++c and --c postincrement operators
 Variable is changed and then expression
executes.
24

Increment and Decrement Operators


1 /* Fig. 3.13: fig03_13.c
2 Preincrementing and postincrementing */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int c; /* define variable */
9
10 /* demonstrate postincrement */
11 c = 5; /* assign 5 to c */
12 printf( "%d\n", c ); /* print 5 */
13 printf( "%d\n", c++ ); /* print 5 then postincrement */
14 printf( "%d\n\n", c ); /* print 6 */
15
16 /* demonstrate preincrement */
17 c = 5; /* assign 5 to c */
18 printf( "%d\n", c ); /* print 5 */
19 printf( "%d\n", ++c ); /* preincrement then print 6 */
20 printf( "%d\n", c ); /* print 6 */
21
22 return 0; /* indicate program ended successfully */
23
24 } /* end function main */
5
5
6

5
6
6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy