0% found this document useful (1 vote)
131 views21 pages

Decision Structures in Programming

This document discusses decision structures in programming languages. It covers if, if-else, and nested if-else statements. Examples are provided to illustrate how to use these statements to evaluate conditions and execute different code blocks based on whether the conditions are true or false. Flowcharts are also used to visualize the logic and flow of several algorithms that employ decision structures. The document concludes with references for further reading on algorithms and programming concepts.

Uploaded by

Hdh Dhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
131 views21 pages

Decision Structures in Programming

This document discusses decision structures in programming languages. It covers if, if-else, and nested if-else statements. Examples are provided to illustrate how to use these statements to evaluate conditions and execute different code blocks based on whether the conditions are true or false. Flowcharts are also used to visualize the logic and flow of several algorithms that employ decision structures. The document concludes with references for further reading on algorithms and programming concepts.

Uploaded by

Hdh Dhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CME111 Programming Languages I

Week 3
Decision Structures

Assist. Prof. Dr. Caner ÖZCAN


Algorithm

2
Decision Structures
► The diamond symbol also called the decision symbol
which indicates that a decision is to be made is one of the
most important flowcharting symbol.
► The decision symbol contains an expression such as a
condition that can be either true or false.
► The decision symbol has two flowlines emerging from it.
One indicates the direction to be taken when the
expression in the symbol is true; the other indicates the
direction to be taken when the expression is false.

true
CONDITION

false 3
Decision Structures (if)
► The if selection statement performs an indicated action
only when the condition is true; otherwise the action is
skipped.
► Also called single selection.

True
condition

if <conditional expression>
False
action1; action1

4
Decision Structures (if)
► Assume that passing grade of a course is 60.
► If we want to print "Passed" if the student's grade
is greater than or equal to 60 :
► Pseudo code:
if grade >= 60
print "Passed" true
grade>= 60 print "Passed"

false

5
Decision Structures (if-else)
► The if..else selection statement allows you to specify that
different actions are to be performed when the condition
is true than when the condition is false.
► Also called double selection.

True
condition
if <conditional expression>
action1; False

else
action2; action2 action1

6
Decision Structures (if-else)
► If we want to print "Passed" if the student's grade
is greater than or equal to 60 and print "Failed" if
the student's grade is less than 60. :
► Pseudo code:
if grade >= 60
print "PASSED" true
grade>= 60 print “PASSED"
else
print "FAILED"
false

print “FAILED"

7
Nested if-else Structure
► Sometimes we test for multiple cases by placing
if..else statements inside if..else statements

True
condition1 action1
if <conditional expression>
action1; False

else if <conditional expression> True


condition2 action 2
action2;
False
else if <conditional expression>
True
action3; condition3 action3
…….
False
else
actionN; actionN

8
Nested if-else Structure
► For example, we want to test if number n is equal
to zero or grater than zero or less than zero.

if n < 0
print "less than zero"
else if n == 0
print "zero"
else
print "greater than zero"

9
Nested if-else Structure

true
print “Less than
n<0
zero"

false

true
n == 0 print “Zero"

false

print “Greater
than zero"

10
Example 1
► Read two numbers from keyboard and subtract
the small number from big one and print result.
– Pseudo code:
Start
Read A,B
if A>=B
result = A-B
else result = B-A
print result
End
11
Example 1

12
Example 2
► Enter grade from keyboard and convert it into letter form.
– Pseudo code:
Start
Read, grade
if grade >= 90
print "A"
else if grade >= 80
print "B"
else if grade >= 70
print "C"
else if grade >= 60
print "D"
else
print "F"
End
13
Example 2

14
Example 3
► Draw a flowchart of an algorithm that calculates the
area of a square when the input value is 1 from
keyboard and calculates round measure when the
input value is 2. If user puts out of 1 or 2 it will give
"Wrong input message".
► The edge length of square is also put from keyboard.

15
Example 3

16
Example 4
► Draw flowchart of an algorithm that calculates the
roots of a second degree equation so that it's
coefficients are put from keyboard.

17
Example 4

18
Example 5
► Pricing conditions are given for a flight ticket. Draw a
flowchart of an algorithm to calculate total price to be
paid. Number of passengers will be given from keyboard.

Zaman Sınıf Price


1-Morning 1- First Class 150 TL
2- Economy 100 TL
2-After Noon 1- First Class 140 TL
2- Economy 90 TL
3-At Night 1- First Class 120 TL
2- Economy 70 TL
%15 discount for more
than 2 passengers

19
Example 5

20
References
► Doç. Dr. Fahri Vatansever, “Algoritma Geliştirme ve
Programlamaya Giriş”, Seçkin Yayıncılık, 12. Baskı,
2015.
► J. G. Brookshear, “Computer Science: An Overview
10th Ed.”, Addison Wisley, 2009.
► Kaan Aslan, “A’dan Z’ye C Klavuzu 8. Basım”, Pusula
Yayıncılık, 2002.
► Paul J. Deitel, “C How to Program”, Harvey Deitel.
► Bayram AKGÜL, C Programlama Ders notları

21

You might also like