You are on page 1of 15

Flowchart to Code

Guidelines

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 1

Selection Structure
Pattern 1

if (condition)
{
statement;
}

condition
This must be a True

True

statement

False

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 2

Selection Structure
Example 1: Printing a number only if it is a negative

if (n<0)
{
printf(%d, n);
}

n<0

True
print
n

False

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 3

Selection Structure
Pattern 2

condition

True

statment_1

False

statment_2

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

if (condition)
{
statement_1;
}
else
{
statement_2;
}

Last Updated: September 2005

Slide 4

Selection Structure
Example 2: If two numbers (p and q) are equivalent reset them to zero,
otherwise exchange or swap their value each other and then print the
new values.

p == q

True
p=0

False
q=0
exchange( p,q)

print
p, q

if (p==q)
{
p = 0;
q = 0;
}
else
{
exchange(&p,&q);
printf(%d%d,p,q);
}

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 5

Selection Structure
Pattern 3

condition_1

True

statment_1

True

statment_2

False

condition_2

condition_n

False

statment_m

True

statment_n

if (condition_1)
{
statement_1;
}
else if (condition_2)
{
statement_2;
}

else if (condition_n)
{
statement_n;
}
else
{
statement_m;
}

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 6

Selection Structure
Example 3: Identifying the grade of a score

score>90

True

grade = 'A'

True

grade = 'B'

True

grade = 'C'

True

grade = 'D'

False

score>75

False

score>60

False

score>50

False

grade = 'F'

if (score > 90)


{
grade = 'A';
}
else if (score > 75)
{
grade = 'B';
}
else if (score > 60)
{
grade = 'C';
}
else if (score > 50)
{
grade = 'D';
}
else
{
grade = 'F';
}

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 7

Selection Structure
Pattern 4

The conditions must be in this form:

expression

expr==val_1

True

== value

statment_1

switch (expr)
{
case val_1 :

statement_1;
break;

False

expr==val_2

True

statment_2

expr==val_n

True

statment_n

False

statment_m

case val_2 :

statement_2;
break;

case val_n :

statement_n;
break;

default:

statement_m;
break;

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 8

Selection Structure
Example 4: Printing the description of a grade.

grade=='A'

True

Print
"Excellent"

True

Print
"Very Good

False

grade=='B'

switch (grade)
{
case 'A : printf("Excellent!);
break;
case 'B' : printf("Very good!);
break;

False

grade=='C'

True

Print
"Good"

True

Print
"Adequate"

case 'C' : printf("Good);


break;

False

grade=='D'

case 'D' : printf("Adequate);


break;
default

False
Print
"Fail"

: printf("Fail);
break;

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 9

Repetition Structure
Pattern 1

Here must be
a False

condition
Here must be
a True

True

Repeated_Actions

False

while (condition)
{
Repeated_Actions;
}

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 10

Repetition Structure
Example: Calculate the average of odd numbers 1 to 9

sum = 0
i =1

sum = 0;
i=1;
i < 11

True
sum = sum + i
False
i = i+2

while (i<11)
{
sum = sum + i;
i = i + 2;
}
avrg = sum/5.0;

av rg = sum /5

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 11

Repetition Structure
Pattern 2

True

Repeated_Actions

do
{
Repeated_Actions;
} while(condition);

condition
The iterating
part must be a
True

False

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 12

Repetition Structure
Example: Prints numbers 1 to 10

i =1

True

PRINT
i

i = i +1

i=1;

do
{
printf(%d\n,i);
i = i + 1;
} while (i<11);

i <11

False

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 13

Repetition Structure
Pattern 3

for (initialize; condition; update)


{
Repeated_Actions;
}

initialize

or

condition

True
Repeated_Actions
False
update

initialize;
while (condition)
{
Repeated_Actions;
update;
}

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 14

Repetition Structure
Example: Print the total of numbers 1 to 10
total = 0;
for (i=1; i<11; i++)
{
total = total + i;
}
printf(%d,total);

total = 0

i=1

or

i<11

True
total = total + i
False
i=i+1

PRINT
total

total = 0;
i=1;
while (i<11)
{
total = total + i;
i++;
}
printf(%d,total);

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM

Last Updated: September 2005

Slide 15

You might also like