Fundamental of Programming (C) Group...

65
Lecturer: Vahid Khodabakhshi Sharif University of Technology Department of Computer Engineering Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Group 4 Lecture 5 Structured Program Development CE 40153 - Fall 98

Transcript of Fundamental of Programming (C) Group...

Page 1: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Lecturer: Vahid Khodabakhshi

Sharif University of TechnologyDepartment of Computer Engineering

Bo

rro

wed

fro

m le

ctu

rer

no

tes

by

Om

id J

afar

inez

had

Fundamental of Programming (C) Group 4

Lecture 5

Structured Program Development

CE 40153 - Fall 98

Page 2: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 2/65

How to develop a program?

RequirementsProblem Analysis

DesignDesigning algorithm

Pseudo code Flow chart

ImplementationImplementing algorithm in

c/c++

Validation Test

Maintenance

Software Development

Life-Cycle

Refinement

Page 3: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 3/65

Page 4: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 4/65

Requirements Discovery• Problem Analysis

– Problem Identification

– Abstraction

• Inputs and outputs determination

• Defining their relation

Write a program to compute an employee'sweekly pay?

How many hours did you work?

How much do you get paid per hour?

employee's weekly pay equal to multiplication of Hours by Pay Rate

Page 5: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 5/65

Problem vs. Solution• A problem is something that causes trouble

• The solution is how to solve or fixe the problem

Page 6: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 6/65

Algorithm• A procedure for solving a problem in terms of actions

and the order in which these actions are to beexecuted in a computer program (program control)

• Action

• Order

Page 7: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 7/65

Execution order• Sequential execution, normally, statements in a

program are executed one after the other in theorder in which they’re written

• Various C statements enable you to specify that thenext statement to be executed may be other thanthe next one in sequence. This is called transfer ofcontrol

Page 8: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 8/65

Structured Programming• goto statement that allows programmers to specify a transfer of

control to one of many possible destinations in a program

• Research had demonstrated that programs could be writtenwithout any goto statements

• Programs produced with structured techniques were clearer, easierto debug and modify and more likely to be bug free in the firstplace.

• Research had demonstrated that all programs could be written interms of only three control structures, namely the sequencestructure, the selection structure and the repetition structure

Page 9: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 9/65

Pseudo code• An artificial and informal language that helps

you develop algorithms

– similar to everyday English

– are not executed on computers

– consists only of action statements

pay-calculation program:

1. Read Hours and Pay Rate2. weekly pay = Read Hours * Pay Rate

Page 10: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 10/65

Flowchart• A flowchart is a diagram that

depicts the flow of an algorithm

– pay-calculation program

• Each symbol represents a different type of operation

START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Page 11: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 11/65

Terminals• represented by rounded rectangles

• indicate a starting or ending point

START

END

START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

ENDTerminal

Terminal

Flow line

Page 12: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 12/65

Input/Output Operations• indicate an input or output operation

Display message “How many

hours did you work?”

Read Hours

START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Input/Output Operation

Page 13: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 13/65

Processes• indicates a process such as a

mathematical computation or variable assignment

START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Multiply Hours by Pay Rate.

Store result in Gross Pay.

Process

Page 14: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 14/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Output Operation

Variable Contents:Hours: ?Pay Rate: ?Gross Pay: ?

How many hours did you work?

Page 15: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 15/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Variable Contents:Hours: 40Pay Rate: ?Gross Pay: ?

How many hours did you work?

40

Input Operation

(User types 40)

Page 16: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 16/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Output Operation

Variable Contents:Hours: 40Pay Rate: ?Gross Pay: ?

How much do you get paid per hour?

Page 17: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 17/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Variable Contents:Hours: 40Pay Rate: 20Gross Pay: ?

How many hours did you work?

20

Input Operation

(User types 20)

Page 18: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 18/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Variable Contents:Hours: 40Pay Rate: 20Gross Pay: 800

How many hours did you work?

20

Process

(Gross Pay = Hours * Pay Rate)

Page 19: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 19/65

Execution START

Display message “How many hours did you

work?”

Read Hours

Display message “How much do you get paid

per hour?”

Read Pay Rate

Multiply Hours by Pay Rate. Store result

in Gross Pay.

Display Gross Pay

END

Variable Contents:Hours: 40Pay Rate: 20Gross Pay: 800

Your gross pay is 800

Output Operation

Page 20: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 20/65

Connectors• Sometimes a flowchart will not fit on one page

– A connector (represented by a small circle) allows you to connect two flowchart segments

A

ASTART

END

Page 21: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 21/65

Modules• A program module (such as a function in C) is

represented by a special symbol

• The position of the module symbol indicates the point the module is executed

• A separate flowchart can be constructed for the module

START

END

Read Input

Call calc_payfunction

Display results

module

Page 22: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 22/65

Control Structures• Sequence• Decision selection statement

– The if statement is called a single-selection statement because it selects or ignores a single action.

– The if…else statement is called a double-selection statement because it selects between two different actions.

– The switch statement is called a multiple-selection statement because it selects among many different actions

• Repetition– while– do…while– for

Page 23: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 23/65

Sequence Structure• a series of actions are performed in sequence

– The pay-calculating example

Page 24: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 24/65

Compound Statements• A statement is a specification of an action to

be taken by the computer as the programexecutes

• Compound Statements is a list of statements enclosed in braces, { }

Page 25: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 25/65

Decision Structure• One of two possible actions is taken,

depending on a condition

• Selection structures are used to chooseamong alternative courses of action

YESNO YESNOx < y?

Process BProcess A

Page 26: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 26/65

Decision Structure• The flowchart segment below shows how a

decision structure is expressed in C as anif/else statement

YESNO

x < y?

Calculate a as x times 2.

Calculate a as x plus y.

if (x < y)

a = x * 2;

else

a = x + y;

Flowchart C programming language

Page 27: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 27/65

Example• Compound statement is way to group many

statements together so they are treated as one

if (grade >= 60)

printf( "Passed.\n" ); // { printf( "Passed.\n" ); }

else

{

printf( "Failed.\n" );

printf( "You must take this course again.\n" );

}

Page 28: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 28/65

Decision Structure• The flowchart segment below shows a

decision structure with only one action toperform

if (x < y)

a = x * 2;

Flowchart C programming language

YESNOx < y?

Calculate a as x times 2.

Page 29: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 29/65

Combining Structures

Display “x is within limits.”

Display “x is outside the limits.”

YESNOx > min?

x < max?

YESNO

Display “x is outside the limits.”

if (x > min){

if (x < max)printf("x is within the limits");

elseprintf("x is outside the limits");

}else

printf("x is outside the limits");

Page 30: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 30/65

Exampleint k = 1, m = 4;

if (k < 2 || m == 3){

m = 2 + k, printf("%d", m);}else{

k = 1, printf("%d", k);}

int k = 1, m = 4;

if (k < 2 || m == 3){

m = 2 + k;printf("%d", m);

}else{

k = 1;printf("%d", k);

}

Which is more readable?

Page 31: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 31/65

Exampleif(x)

if(y)printf("Yes");

else printf("No");

if(x){

if(y)printf("Yes");

else printf("No");

}

if(x){

if(y)printf("Yes");

}else

printf("No");

if (x < 0) sign = -1;

else if (x == 0) sign = 0;

else sign = 1;

if (x < 0.25) count1++;

else if (x >= 0.25 && x < 0.5) count2++;

else if (x >= 0.5 && x < 0.75) count3++;

else count4++;

Page 32: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 32/65

Case Structure• One of several possible actions is taken,

depending on the contents of a variable

Page 33: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 33/65

Case Structure• indicates actions to perform depending on the

value in years_employed

CASEyears_employed

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

If years_employed = 1, bonus is set to 100

If years_employed = 2, bonus is set to 200

If years_employed = 3, bonus is set to 400

If years_employed is any other value, bonus is set to 800

Page 34: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 34/65

switch• A switch statement allows a single variable (integer

or char) to be compared with several possibleconstants

– A constant can not appear more than once, and there can only be one default expression

Page 35: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 35/65

switchswitch (variable)

{

case const:

statements...;

default:

statements...;

}

switch (c = toupper(getch())){

case ‘R’: printf("Red");break;

case ‘G’:printf("Green");break;

default:printf("other");

}

Page 36: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 36/65

Exampleswitch(betty) {

case 1: printf("betty = 1\n");

case 2: printf("betty=2\n"); break;

case 3: printf("betty=3\n"); break;

default: printf("Not sure\n");

}

CASEbetty?

1 2 3 Other

betty = 1 betty = 2 betty = 3 Not sure

Page 37: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 37/65

Repetition Structure• A loop tests a condition, and if the condition

exists, it performs an action. Then it tests thecondition again. If the condition still exists, theaction is repeated. This continues until thecondition no longer exists

x < y? Process AYES

Page 38: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 38/65

Repetition Structure• The flowchart segment below shows a

repetition structure expressed in C as a whileloop

while (x < y)

x++;

Flowchart C programming language

x < y? Add 1 to xYES

Page 39: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 39/65

Whilewhile (loop_repetition_condition)

statement;

OR

//Compound statementwhile (loop_repetition_condition) {

statement1;statement2; // …

}

Page 40: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 40/65

Controlling a Repetition Structure• The action performed by a repetition structure

must eventually cause the loop to terminate.Otherwise, an infinite loop is created

• In this flowchart segment, x is never changed. Once the loop starts, it will never end

• How can this flowchart be modified so it is no longer an infinite loop?

x < y? Display x

YES

Page 41: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 41/65

Controlling a Repetition Structure

• Adding an action within the repetition thatchanges the value of x

x < y? Display x Add 1 to x

YES

Page 42: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 42/65

A Pre-Test Repetition Structure• This type of structure is known as a pre-test

repetition structure. The condition is tested BEFORE any actions are performed

– if the condition does not exist, the loop will never begin

x < y? Display x Add 1 to x

YES

Page 43: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 43/65

Example

int counter = 0;while (counter < 9) {

printf("%d\n", counter ++);}

int counter = 0;while (counter < 1000) ;

while (1);

int counter = 0;while (counter < 9) {

printf("%d\n", counter);counter++;

}

int counter = 0;while (counter < 9)

printf("%d\n", counter ++);

int counter = 9;while (counter > 0)

printf("%d\n", counter --);

Page 44: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 44/65

A Post-Test Repetition Structure• The condition is tested AFTER the actions are

performed

– A post-test repetition structure always performs its actions at least once

Display x

Add 1 to x

YESx < y?

do

{

printf(…);

x++;

} while (x < y);

C programming language

Page 45: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 45/65

do-whiledo

statement;while (loop_repetition_condition)

OR

do //Compound statement {

statement1;statement2; // …

}while (loop_repetition_condition)

Page 46: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 46/65

Example

Input : 5 integern1, n2, n3, n4, n5

Output: The summation of n1, n2, .., n5

Input example: 2 3 4 5 6

Output example: 20

Can you

identify the

input and

output?

Draw a flowchart for the

following problem:

Read 5 integer and display the

value of their summation.

Page 47: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 47/65

Input n1

Input n2

Input n3

input n4

input n5

output sum

sum ← n1+n2+n3+n4+n5

start

2n1

Assume input example:2 3 4 5 6

3n2

4n3

5n4

6n5

20sum

end

This flowchart does

not use loop, hence

we need to use 6

different variables

Page 48: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 48/65

counter ← 1, sum ← 0

counter < 6

sum ← sum + n

false

true

counter++

output sum

input n

1counter

sum 0

1 < 6 true

2n

0 + 22

2

2 < 6 true

3

2 + 35

3

3 < 6 true

4

5 + 49

4

4 < 6 true

5

9 + 514

5

5 < 6 true

6

14 + 620

6

6 < 6 false

Assume input example:2 3 4 5 6

This loop is

counter-controlledThe counter

Increases by 1

Uses only

3 variables

Page 49: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 49/65

C Programming Language

Decreasing Counter-Controlled Loop

counter ← 5, sum ← 0

counter > 0

sum←sum+ x

false

true

counter--

output sum

input x

Page 50: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 50/65

For

for (initial_value ; condition; update_counter)

statement;

OR

// Compound statement

for (initial_value ; condition; update_counter) {

statement;

statement; // …

}

Page 51: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 51/65

int x, sum, i;

sum = 0;

for (i = 0; i < 5; i++) {

scanf(“%d”,&x);

sum = sum + x;

}printf(“%d”,sum);

counter ← 1, sum ← 0

counter < 6

sum ← sum + n

false

true

counter++

output sum

input n

Page 52: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 52/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

???

_

Page 53: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 53/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

1

_

Page 54: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 54/65

num

1

_

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

Page 55: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 55/65

num

1

1 _

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

Page 56: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 56/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

2

1 _

Page 57: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 57/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

2

1 _

Page 58: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 58/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

2

1 2 _

Page 59: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 59/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

3

1 2 _

Page 60: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 60/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

3

1 2 _

Page 61: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 61/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

3

1 2 3 _

Page 62: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 62/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

4

1 2 3 _

Page 63: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 63/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

4

1 2 3 _

Page 64: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 64/65

Example:

for (num = 1; num <= 3; num++ )

printf(“%d\t”, num);

printf(“have come to exit\n”);

num

4

1 2 3 have come to exit_

Page 65: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C401… · Control Structures •Sequence •Decision selection statement –The

Structured Program Development – Lecture 5

Sharif University of TechnologyDepartment of Computer Engineering 65/65

Example*********************

#include <stdio.h>

int main(){

int iCounter = 0;int jCounter = 0;

while (iCounter < 7){

jCounter = 0;while (jCounter < iCounter){

printf("*");jCounter++;

}printf("\n");iCounter++;

}

getch();return 0;

}