Professional Documents
Culture Documents
Guidelines
Slide 1
Selection Structure
Pattern 1
if (condition)
{
statement;
}
condition
This must be a True
True
statement
False
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
Slide 3
Selection Structure
Pattern 2
condition
True
statment_1
False
statment_2
if (condition)
{
statement_1;
}
else
{
statement_2;
}
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);
}
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;
}
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'
Slide 7
Selection Structure
Pattern 4
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;
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"
False
grade=='D'
False
Print
"Fail"
: printf("Fail);
break;
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;
}
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
Slide 11
Repetition Structure
Pattern 2
True
Repeated_Actions
do
{
Repeated_Actions;
} while(condition);
condition
The iterating
part must be a
True
False
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
Slide 13
Repetition Structure
Pattern 3
initialize
or
condition
True
Repeated_Actions
False
update
initialize;
while (condition)
{
Repeated_Actions;
update;
}
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);
Slide 15