You are on page 1of 11

4 Specifying Algorithms:

Flow of Control
COL 100 - Introduction to Computer Science and
Programming
II Semester 2015-2016
Department of Computer Science and Engineering
Indian Institute of Technology Delhi

Programming Tasks
Specification
data
algorithms

Analysis
Design and Implementation
Verification/Testing

Jan 2016

P. R. Panda, I.I.T Delhi

Detailed Course Contents


Topic
Sub-topics
The Compu*ng Computer Components: Hardware
Environment Memory: addressing and hierarchy
Program Execu*on: compiler and opera*ng system
Instruc*ons: format and execu*on
Specica*on: Number Representa*on: binary, two's complement, oa*ng point
Data
Data types: integer, oa*ng point, character, string
Aggregates: arrays and structures
Dynamic data: linked list
Specica*on: Control ow: condi*onals and loops
Algorithm
Sub-programs - func*ons and procedures
Divide and conquer: recursion
Dynamic programming (opt), Concurrency: thread (opt)
Analysis
Complexity
Recurrence Rela*ons
Design and
Modularity - separate compila*on, interfaces, and classes
Implementa*on Abstrac*on Mechanisms: templates, overloading (opt), class hierarchy (opt)
Library and reuse
SoPware Engineering: Make
Verica*on
Asser*ons and Invariants
Jan 2016
P. R. Panda, I.I.T Delhi
3
Tes*ng

Specifying an Algorithm
Algorithm
Unambiguous specification of a method/
procedure

Jan 2016

P. R. Panda, I.I.T Delhi

Algorithm Structure and


Sequencing
START
a=4

main ( )
{
int a, b, c;

b=5

a = 4;
b = 5;
c = a + b;
cout << c;

c=a+b
Print value of c

}
END
Jan 2016

P. R. Panda, I.I.T Delhi

Conditionals
START

a divisible
by 2?

Print even

main ( )
{
int a;

F
cin >> a; // Input from user
if (a % 2 == 0)
cout << even << endl;
else
cout << odd << endl;

Print odd
}

END
Jan 2016

What does the instruction


sequence look like?
P. R. Panda, I.I.T Delhi

Nested Conditionals
Program to detect Leap year

START
T
T

100
|Y?
F

400
|Y?

Leap

4|
Y?
F
Leap

No

Jan 2016

END

main()
{
if (year % 4 == 0) {
F
if (year % 100 == 0) {
if (year % 400 == 0) {
cout << Leap;
} else {
cout << No;
No
}
} else {
cout << Leap;
}
} else {
cout << No << endl;
}
}
P. R. Panda, I.I.T Delhi

Different Ways of Composing


Conditionals
main()
{
if (year % 4 == 0)
if (year % 100 == 0)
cout << No;
else
cout << Yes;
else
cout << No;
}

OR

main()
{
if (year % 100 == 0)
cout << No;
else
if (year % 4 == 0)
cout << Yes;
else
cout << No;
}

Which
is
better?

More specific case in inner conditional


Jan 2016

P. R. Panda, I.I.T Delhi

Multi-way Branch:
SWITCH/CASE Statement
START

month
apr,jun,...

30

feb
28

END
Jan 2016

others
31

main()
{
int month, days, year; ...
switch (month) {
case 4:
case 5:
case 9:
case 11: days = 30; break;
case 2: days = (year % 4) ? 28 : 29; break;
default: days = 31;
}
cout << Days in month << month << =
<< days;
}
P. R. Panda, I.I.T Delhi

Conditionals in C++

Evaluating Conditionals in C/C++


LOGICAL expression
if (a > b)
if (a == b) // if a equals b
if (a != b) // if a doesnt equal b
INTEGER expression
if (year % 4 == 0) // if y is
divisible by 4
if (year % 4 != 0) // if y is NOT
divisible by 4
if (y) // if (y is non-zero)
if (!y) // if (y == 0)
Ternary Operator ?
a = b ? c : d;
if (b) a = c; else a = d;
Jan 2016

main()
{
int month, days, year; ...
switch (month) {
case 4:
case 5:
case 9:
case 11: days = 30; break;
case 2: days = (year % 4)?28:29; break;
default: days = 31;
}
cout << Days in month << month << =
<< days;
}

P. R. Panda, I.I.T Delhi

10

The C++ SWITCH


default case executed
when none of the cases
match
break necessary to exit
from switch
if no break, then control
flows to next case

Condition expression must


be integer
why?

Case expressions must be


constant
why?
Jan 2016

main()
{
int month, days, year; ...
switch (month) {
case 4:
case 5:
case 9:
case 11: days = 30; break;
case 2: days = (year % 4) ? 28 : 29; break;
default: days = 31;
}
cout << Days in month << month << =
<< days;
}

P. R. Panda, I.I.T Delhi

11

You might also like