Вы находитесь на странице: 1из 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

condition if (condition)
{
This must be a True statement;
}
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)
n<0 {
printf(%d, n);
}
True

print False
n

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM Last Updated: September 2005 Slide 3
Selection Structure
Pattern 2

if (condition)
condition True statment_1 {
statement_1;
}
False else
{
statement_2;
statment_2 }

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM 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.

if (p==q)
p == q True
{
p=0 p = 0;
False
q = 0;
q=0
}
exchange( p,q)
else
{
print exchange(&p,&q);
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
if (condition_1)
{
statement_1;
}
condition_1 True statment_1
else if (condition_2)
False {
statement_2;
condition_2 True statment_2 }

else if (condition_n)
condition_n True statment_n {
statement_n;
False }
else
statment_m
{
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

if (score > 90)


score>90 True grade = 'A' {
grade = 'A';
False }
else if (score > 75)
score>75 True grade = 'B' {
grade = 'B';
False }
else if (score > 60)
score>60 True grade = 'C' {
grade = 'C';
False
}
else if (score > 50)
{
score>50 True grade = 'D'
grade = 'D';
}
False
else
grade = 'F'
{
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 == value switch (expr)


{
expr==val_1 True statment_1 case val_1 : statement_1;
break;
False

case val_2 : statement_2;


expr==val_2 True statment_2
break;

case val_n : statement_n;


expr==val_n True statment_n
break;

False
default: statement_m;
statment_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"
switch (grade)
{
False case 'A : printf("Excellent!);
break;
Print
grade=='B' True
"Very Good

case 'B' : printf("Very good!);


False break;

Print
grade=='C' True "Good" case 'C' : printf("Good);
break;
False

case 'D' : printf("Adequate);


Print
grade=='D' True "Adequate" break;

False default : printf("Fail);


break;
Print
"Fail" }

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 while (condition)


Here must be
a True
{
True
Repeated_Actions;
False }
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 while (i<11)
True
{
sum = sum + i
sum = sum + i;
False i = i + 2;
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 i=1;
do
{
PRINT
True
i printf(%d\n,i);
i = i + 1;
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)


initialize {
Repeated_Actions;
}

condition or

True
initialize;
Repeated_Actions while (condition)
False {
Repeated_Actions;
update
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;
total = 0 for (i=1; i<11; i++)
{
total = total + i;
i=1
}
printf(%d,total);

i<11 or

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

Flowchart to Code Guidelines | DCP2073 Basic C Programming | Jumail, FSKSM, UTM Last Updated: September 2005 Slide 15