Loops in C
Loops in C
Loops in C
103
29.08.2018
For
loop,
Symbolic
Constants,
Character
Input/Output
RepeCCve
ComputaCon
• In
a
large
number
of
compuCng
problems,
you
will
observe
the
requirement
for
repeated
computaCon,
possibly
on
different
data.
• Example
1:
Fahrenheit
(F)
–
Celsius
(C)
conversion.
– Given
the
F
value,
use
the
same
formula
to
compute
the
corresponding
C
value.
• Example
2:
Find
the
CGPA
of
a
parCcular
student.
– Given
a
list
of
student
records
and
the
student
ID
of
a
parCcular
student
(say,
2014CS1009).
– Go
to
the
1st
student
ID
value,
and
compare
it
to
2014CS1009.
– If
it
matches,
look
up
the
CGPA
and
output.
– Else,
go
to
the
2nd
student
ID
value,
and
again
compare.
– Again,
we
see
the
repeated
computaCon.
Another
loop
–
the
For
Loop
• We
have
already
seen
the
while
loop.
• Another
loop
is
provided
in
C
–
the
for
loop.
The
structure
is
as
follows:
for
(expression
1;
expression
2;
expression
3)
{
Body
of
the
loop;
}
-‐
Expression
1:
ini<aliza<on
of
loop
variable.
-‐
Expression
2:
test
condi<on
(execute
loop
code
if
this
is
true).
-‐
Expression
3:
increasing
the
loop
variable.
• Let
us
write
the
F
–
C
conversion
program,
using
the
for
loop.
#
include
<stdio.h>
/*
print
F
–
C
table
*/
{
int
fahr;
for
(fahr
=
0;
fahr
<=
300;
fahr
=
fahr
+
20)
prinN(“%3d
%6.1f\n”,
fahr,
(5.0/9.0)*(fahr
–
32));
}
-‐ If
the
loop
body
has
1
statement,
{
}
not
needed.
-‐ If
the
loop
body
has
>
1
statement,
{
}
are
needed.
-‐ This
is
true
in
the
case
of
the
while
loop
as
well.
• This
looks
different
from
the
“while”
version
that
we
discussed
earlier.
• What’s
different?
– Most
of
the
variables
have
been
eliminated
(no
lower,
upper,
step).
• We
are
using
numbers
in
their
place.
– Expression/equaCon
that
calculates
the
C
value
appears
as
a
3rd
argument
in
princ.
• Important
rule:
– Wherever
it
is
allowed
to
use
the
variable
of
some
data
type,
we
can
use
an
expression
of
the
same
data
type.
– UlCmately,
the
expression
will
evaluate
to
the
variable
value.
For
Loop
Working
• How
does
the
for
loop
work?
– IniCalize
the
loop
variable
(it
determines
how
many
Cmes
the
loop
will
be
run)
as
expression
1.
– Evaluate
the
terminaCon
condiCon
as
expression
2.
– If
it’s
true,
execute
the
loop
body.
– Now,
execute
expression
3
(the
increment
of
the
loop
variable).
– Again,
evaluate
expression
2.
It
it’s
true,
execute
loop
body.
– Whenever
expression
2
evaluates
to
false,
get
out
of
the
loop.
Symbolic
Constants
• Note
that
in
the
F
–
C
program,
we
use
numbers,
such
as
300
and
20.
• Generally,
it’s
not
such
a
good
idea
to
use
the
numbers
in
this
form.
Why?
– They
may
not
make
much
sense
to
somebody
else
who
is
reading
the
program.
– They
will
be
hard
to
change
in
a
systemaCc
way.
• If
they
appear
5
Cmes
in
a
program,
and
we
decide
to
use
new
numbers,
we
have
to
make
changes
5
Cmes
in
the
program.
– A
beier
opCon
is
to
use
“symbolic
constants”.
– Let’s
see
this
in
acCon
in
the
F
–
C
program.
#
include
<stdio.h>
#
define
LOWER
0
/*
lower
limit
*/
#
define
UPPER
300
/*
upper
limit
*/
#
define
STEP
20
/*
step
size
*/
main
(
)
{
int
fahr;
for
(fahr
=
LOWER;
fahr
<=
UPPER;
fahr
=
fahr
+
STEP)
prinN(“%3d
%6.1f\n”,
fahr,
(5.0/9.0)*(fahr
–
32));
}
• To
define
a
symbolic
constant,
we
can
follow
this:
• #
define
name
replacement
text
• All
occurrences
of
name
in
the
program
are
subsCtuted
with
replacement
text.
• Note
that
this
solves
both
the
earlier
problems
(with
pukng
numbers
like
20,
300):
– The
values
LOWER,
UPPER
and
STEP
convey
more
meaning
that
plain
numbers
such
as
0,
300
and
20.
– If
we
want
to
change
the
upper
limit
to
500,
or
the
step
size
to
1,
we
need
to
make
only
one
change
in
the
program
(#
define
STEP
1
or
#define
UPPER
500).
– If
we
had
used
plain
numbers,
we
would
have
to
change
20
to
1
or
300
to
500
at
every
occurrence
in
the
program.
• Why
don’t
we
just
use
variables
for
step,
lower
and
upper?
• Note
that
for
one
parCcular
execuCon
of
the
program,
their
values
will
remain
the
same
(they
will
not
change).
• Hence,
it
is
beier
to
use
constants,
and
not
variables.
• For
the
case,
where
the
values
change
during
a
single
execuCon
of
the
program,
one
needs
to
use
variable.
– As
in
the
case
of
the
value
that
prints
the
Fahrenheit
value
(fahr).
• As
good
programming
pracCce,
constants
are
declared
with
UPPER
CASE
CAPITAL
leiers,
to
separate
them
from
variables,
which
are
declared
in
lower
case
leiers.
Character
Input/output
• Now,
we
are
going
to
discuss
a
family
of
related
programs
that
process
character
data.
• Characters
are
the
smallest
of
the
data
types.
• The
list
of
characters
used
in
C
is
given
in
the
ASCII
data
set
(hip://www.asciitable.com/).
•
ASCII
–
American
Standard
Code
for
InformaCon
Interchange.
• Table
is
a
character
encoding
system.
• All
characters
are
represented
in
the
computer
as
binary
numbers
(sequence
of
0s
and
1s).
• Binary
numbers
are
hard
to
read
–
01100100
• Binary
numbers
have
decimal
equivalent.
Binary
number
above
is
100
(in
decimal).
This
is
easier
to
read.
Character
I/O
• Character/text
input
and
output
is
regarded
as
a
“text
stream”.
– It
is
a
sequence
of
characters,
divided
into
lines.
– Each
line
consists
of
zero
or
more
characters
followed
by
a
newline
(\n)
character.
For
example:
– Abcdefgh\n
– Ijklmn\n
• We
now
discuss
two
simple
funcCons
for
character
I/O.
• getchar
(
).
– Every
Cme
it
is
called,
getchar
(
)
reads
the
next
input
character
from
a
text
stream
and
returns
that
as
its
value.
– Returned
value
can
be
used
accordingly
in
the
program.
– c
=
getchar
(
);
This
reads
the
next
character
form
the
input
and
then,
assigns
it
to
a
character
variable
c.
Character
I/O
• putchar
(c).
– This
funcCon
prints
the
value
of
the
character
c
to
the
screen.
• These
two
funcCon,
although
quite
simple,
can
be
used
to
write
a
wide
variety
of
code.
• Program
–
1:
copy
the
input
to
output,
one
character
at
a
<me.
– Read
a
character,
print
it,
read
next
character,
print
it,
read
next
character,
print
it,
and
so
on.
• What
will
the
steps
of
the
program
look
like?
Program
steps
1. Read
a
character.
2. While
(character
is
not
end-‐of-‐file
indicator)
3. Output
the
character
just
read.
4. Read
a
character.
#
include
<stdio.h>
/*
copy
input
to
output:
1st
version
*/
main
(
)
{
char
c;
c
=
getchar
(
);
while
(c
!=
EOF)
{
putchar
(c);
c
=
getchar
(
);
}
}
Char
I/O,
version
1
• Prints
input,
one
character
at
a
Cme.
• How
do
we
stop
the
input?
• By
using
the
end-‐of-‐file
indicator.
• This
value
varies
from
machine
to
machine
and
is
denoted
by
the
symbolic
constant
EOF.
• To
specify
EOF
on
Linux/Mac,
you
need
to
type
–
Control,
followed
by
D
(ctrl
–
d).
• This
sends
an
end
of
file
signal
to
C,
and
thus
ends
the
program.
• Note
that
the
book
declares
c
to
be
an
integer.
• That
is
fine
too,
as
all
characters
are
internally
represented
in
the
computer
as
numbers.
• Next
program
shows
an
example.
#
include
<stdio.h>
main
(
)
{
char
c;
c
=
‘a’;
prinN(“%c\n”,
c);
prinN(“%d\n”,
c);
}
Output
is:
a
97
#
include
<stdio.h>
/*
copy
input
to
output:
2nd
version
*/
main
(
)
{
char
c;
while
(
(c
=
getchar
(
)
)
!=
EOF)
putchar
(c);
}
Char
I/O,
version
2
• What
has
changed
in
the
2nd
version?
• The
character
reading,
it’s
assignment,
and
the
EOF
check
are
all
done
in
a
single
line
in
the
while
loop
condi<on.
• The
assignment
is
appearing
as
part
of
a
larger
expression.
• This
program
is
shorter
than
the
1st
version.
1. First,
read
a
character
using
getchar
(
).
2. Then,
assign
it
to
c.
3. Next,
check
if
it
is
an
EOF
character.
4. If
yes,
stop
program,
else,
print
it,
and
read
the
next
character.
5. If
no,
exit
the
program.
Char
I/O,
version
2
• Let’s
look
at
the
expression:
• While
(
(
c
=
getchar
(
)
)
!=
EOF).
• Whenever
we
have
mulCple
operators
(=,
<,
!=
etc.)
in
the
same
expression,
there
is
a
set
of
rules
that
define
what
is
the
order
of
execuCon.
• For
example,
4
+
5
*
2.
the
answer
could
be
14
or
18,
depending
on
the
order
of
execuCon
of
the
+
and
*.
• According
to
the
rules,
!=
is
executed
before
=,
if
they
appear
in
the
same
expression.
• However,
we
want
to
do
the
reverse.
• We
want
to
execute
=
before
!=
(assign
to
c,
and
then
check
if
EOF).
Char
I/O,
version
2
• To
make
any
operator
more
important,
enclose
corresponding
expression
in
(
).
• While
(
(c
=
getchar
(
)
)
!=
EOF
)
• The
green
brackets
(
)
give
precedence
to
=
and
it
is
evaluated
before
!=.
• What
happens
if
we
don’t
put
the
brackets?
• While
(
c
=
getchar
(
)
!=
EOF)
• The
!=
is
evaluated
first.
So,
the
character
read
is
compared
with
EOF,
and
the
answer
is
true
or
false.
• This
value
is
assigned
to
c.
Program
–
2:
Character
CounCng
• Let
us
look
at
another
program
that
uses
the
getchar
(
)
and
putchar
(
)
funcCons.
• We
need
to
write
a
program
that
counts
the
number
of
characters
in
the
input,
and
outputs
that
number.
• How
can
we
write
this
program?
– We
can
use
the
getchar
(
)
funcCon
to
receive
the
input,
one
character
at
a
Cme.
– We
need
to
maintain
an
integer
counter.
– Every
Cme
we
read
a
character,
we
increase
the
counter
by
1.
– We
keep
on
counCng
the
characters
Cll
we
hit
the
EOF.
– As
soon
as
we
hit
EOF,
we
can
print
the
current
value
of
the
counter.
Character
CounCng
with
While
#
include
<stdio.h>
/*
character
coun<ng,
1st
version
*/
main
(
)
{
long
nc;
nc
=
0;
while
(getchar
(
)
!=
EOF)
nc++;
prinN(“Number
of
characters
is:
%ld\n”,
nc);
}
• Note
that
we
chooses
to
use
long
as
the
data
type.
This
is
short
for
long
integer.
• Since
we
are
counCng
characters,
we
can
quickly
run
out
of
memory
if
the
value
of
nc
is
large.
• Hence,
long
is
a
good
choice
(twice
as
much
memory
reserved
as
int).
• For
even
more
memory,
we
can
use
the
double
data
type.
– This
is
typically
used
for
large
floaCng
point
values.
– We
can
ignore
the
digits
ater
the
decimal
point.
• Version
3
shows
this
on
the
next
slide.
• Note
that
here,
we
use
the
for
loop
instead
of
the
while
Character
CounCng
with
For
#
include
<stdio.h>
/*
character
coun<ng,
2nd
version
*/
main
(
)
{
double
nc;
for(nc
=
0;
getchar
(
)
!=
EOF;
nc++)
;
prinN(“Number
of
characters
is:
%
.0f”,
nc);
}