[go: up one dir, main page]

0% found this document useful (0 votes)
14 views81 pages

Programming Fundamentals C - Manoj Shetty Sir

Uploaded by

Devesh Pandit
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 (0 votes)
14 views81 pages

Programming Fundamentals C - Manoj Shetty Sir

Uploaded by

Devesh Pandit
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
You are on page 1/ 81

Number

Representation

1
Number System :: The Basics
We are accustomed to using the so-called
decimal number system
Ten digits :: 0,1,2,3,4,5,6,7,8,9
Every digit position has a weight which is a
power of 10
Base or radix is 10
Example:
234 = 2 x 102 + 3 x 101 + 4 x 100
250.67 = 2 x 102 + 5 x 101 + 0 x 100 + 6 x
10-1 + 7 x 10-2 2
Binary Number System
Two digits:
0 and 1
Every digit position has a weight which is a
power of 2
Base or radix is 2
Example:
110 = 1 x 22 + 1 x 21 + 0 x 20
101.01 = 1 x 22 + 0 x 21 + 1 x 20 + 0 x 2-1 +
1 x 2-2
3
Positional Number Systems (General)
Decimal Numbers:
10 Symbols {0,1,2,3,4,5,6,7,8,9}, Base or Radix is 10
136.25 = 1 102 + 3 101 + 6 100 + 2 10–1 + 3 10–2

4
Positional Number Systems (General)
Decimal Numbers:
10 Symbols {0,1,2,3,4,5,6,7,8,9}, Base or Radix is 10
136.25 = 1 102 + 3 101 + 6 100 + 2 10–1 + 3 10–2
Binary Numbers:
2 Symbols {0,1}, Base or Radix is 2
101.01 = 1 22 + 0 21 + 1 20 + 0 2–1 + 1 2–2

5
Positional Number Systems (General)
Decimal Numbers:
10 Symbols {0,1,2,3,4,5,6,7,8,9}, Base or Radix is 10
136.25 = 1 102 + 3 101 + 6 100 + 2 10–1 + 5 10–2
Binary Numbers:
2 Symbols {0,1}, Base or Radix is 2
101.01 = 1 22 + 0 21 + 1 20 + 0 2–1 + 1 2–2
Octal Numbers:
8 Symbols {0,1,2,3,4,5,6,7}, Base or Radix is 8
621.03 = 6 82 + 2 81 + 1 80 + 0 8–1 + 3 8–2

6
Positional Number Systems (General)
Decimal Numbers:
10 Symbols {0,1,2,3,4,5,6,7,8,9}, Base or Radix is 10
136.25 = 1 102 + 3 101 + 6 100 + 2 10–1 + 3 10–2
Binary Numbers:
2 Symbols {0,1}, Base or Radix is 2
101.01 = 1 22 + 0 21 + 1 20 + 0 2–1 + 1 2–2
Octal Numbers:
8 Symbols {0,1,2,3,4,5,6,7}, Base or Radix is 8
621.03 = 6 82 + 2 81 + 1 80 + 0 8–1 + 3 8–2
Hexadecimal Numbers:
16 Symbols {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}, Base is 16
6AF.3C = 6 162 + 10 161 + 15 160 + 3 16–1 + 12 16–2
7
Binary-to-Decimal Conversion
Each digit position of a binary number has
a weight
Some power of 2
A binary number:
B = bn-1 bn-2 …..b1 b0 . b-1 b-2 ….. b-m
Corresponding value in decimal:

n-1

D= bi 2i
i = -m
8
Examples
101011 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20
= 43
(101011)2 = (43)10

.0101 0x2-1 + 1x2-2 + 0x2-3 + 1x2-4


= .3125
(.0101)2 = (.3125)10

101.11 1x22 + 0x21 + 1x20 + 1x2-1 + 1x2-2


= 5.75

(101.11)2 = (5.75)10

9
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
Base NumbRem

2 89
2 44 1
2 22 0
2 11 0
2 5 1
2 2 1
2 1 0
0 1

(89)10 = (1011001)2
10
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
Base NumbRem
2 66
2 89 2 33 0
2 44 1 2 16 1
2 22 0 2 8 0
2 11 0 2 4 0
2 5 1 2 2 0
2 2 1 2 1 0
2 1 0 0 1
0 1
(66)10 = (1000010)2
(89)10 = (1011001)2
11
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
Base NumbRem 2 239
2 66 2 119 1
2 89 2 33 0 2 59 1
2 44 1 2 16 1 2 29 1
2 22 0 2 8 0 2 14 1
2 11 0 2 4 0 2 7 0
2 5 1 2 2 0 2 3 1
2 2 1 2 1 0 2 1 1
2 1 0 0 1 0 1
0 1
(66)10 = (1000010)2
(89)10 = (1011001)2 (239)10 = (11101111)2
12
Decimal to Binary: Fraction Part
Repeatedly multiply the given fraction by 2.
Accumulate the integer part (0 or 1).
If the integer part is 1, chop it off.
Arrange the integer parts in the order they are obtained.

Example: 0.634
.634 x 2 = 1.268
.268 x 2 = 0.536
.536 x 2 = 1.072
.072 x 2 = 0.144
.144 x 2 = 0.288
:
:
(.634)10 = (.10100……)2
13
Decimal to Binary: Fraction Part
Repeatedly multiply the given fraction by 2.
Accumulate the integer part (0 or 1).
If the integer part is 1, chop it off.
Arrange the integer parts in the order they are obtained.

Example: 0.634 Example: 0.0625


.634 x 2 = 1.268 .0625 x 2 = 0.125
.268 x 2 = 0.536 .1250 x 2 = 0.250
.536 x 2 = 1.072 .2500 x 2 = 0.500
.072 x 2 = 0.144 .5000 x 2 = 1.000
.144 x 2 = 0.288 (.0625)10 = (.0001)2
:
:
(.634)10 = (.10100……)2
14
Decimal to Binary: Fraction Part
Repeatedly multiply the given fraction by 2.
Accumulate the integer part (0 or 1).
If the integer part is 1, chop it off.
Arrange the integer parts in the order they are obtained.

Example: 0.634 Example: 0.0625


.634 x 2 = 1.268 .0625 x 2 = 0.125
.268 x 2 = 0.536 .1250 x 2 = 0.250
.536 x 2 = 1.072 .2500 x 2 = 0.500
.072 x 2 = 0.144 .5000 x 2 = 1.000
.144 x 2 = 0.288 (.0625)10 = (.0001)2
: (37)10 = (100101)2
:
(.0625)10 = (.0001)2
(.634)10 = (.10100……)2 (37.0625)10 = (100101.0001)2
15
Hexadecimal Number System
A compact way of representing binary numbers
16 different symbols (radix = 16)

0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111

16
Binary-to-Hexadecimal
Conversion
For the integer part,
Scan the binary number from right to left
Translate each group of four bits into the
corresponding hexadecimal digit
Add leading zeros if necessary

For the fractional part,


Scan the binary number from left to right
Translate each group of four bits into the
corresponding hexadecimal digit
Add trailing zeros if necessary
17
Example
1. (1011 0100 0011)2 = (B43)16
2. (10 1010 0001)2 = (2A1)16
3. (.1000 010)2 = (.84)16
4. (101 . 0101 111)2 = (5.5E)16

18
Hexadecimal-to-Binary
Conversion
Translate every hexadecimal digit into its
4-bit binary equivalent

Examples:
(3A5)16 = (0011 1010 0101)2
(12.3D)16 = (0001 0010 . 0011 1101)2
(1.8)16 = (0001 . 1000)2

19
Unsigned Binary Numbers
An n-bit binary number
B = bn-1bn-2 …. b2b1b0
2n distinct combinations are possible, 0 to 2n-1.
For example, for n = 3, there are 8 distinct
combinations
000, 001, 010, 011, 100, 101, 110, 111
Range of numbers that can be represented
n=8 0 to 28-1 (255)
n=16 0 to 216-1 (65535)
n=32 0 to 232-1 (4294967295)
20
Signed Integer Representation
Many of the numerical data items that are used
in a program are signed (positive or negative)
Question:: How to represent sign?

Three possible approaches:


Sign-magnitude representation
One’s complement representation
Two’s complement representation
21
Sign-magnitude Representation

For an n-bit number representation


The most significant bit (MSB) indicates sign
0 positive
1 negative
The remaining n-1 bits represent magnitude

bn-1 bn-2 b1 b0

Sign
Magnitude
22
Contd.
Range of numbers that can be
represented:
Maximum :: + (2n-1 – 1)
Minimum :: (2n-1 – 1)

A problem:
Two different representations of zero
+0 0 000….0
-0 1 000….0
23
One’s Complement
Representation
Basic idea:
Positive numbers are represented exactly as in
sign-magnitude form
Negative numbers are represented in 1’s
complement form
How to compute the 1’s complement of a number?
Complement every bit of the number (1 0 and
0 1)
MSB will indicate the sign of the number
0 positive
1 negative
24
Example :: n=4
0000 +0 1000 -7
0001 +1 1001 -6
0010 +2 1010 -5
0011 +3 1011 -4
0100 +4 1100 -3
0101 +5 1101 -2
0110 +6 1110 -1
0111 +7 1111 -0

To find the representation of, say, -4, first note that


+4 = 0100
-4 = 1’s complement of 0100 = 1011
25
Contd.
Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: (2n-1 – 1)
A problem:
Two different representations of zero.
+0 0 000….0
-0 1 111….1
Advantage of 1’s complement representation
Subtraction can be done using addition
Leads to substantial saving in circuitry
26
Two’s Complement
Representation
Basic idea:
Positive numbers are represented exactly as in
sign-magnitude form
Negative numbers are represented in 2’s
complement form
How to compute the 2’s complement of a number?
Complement every bit of the number (1 0 and
0 1), and then add one to the resulting number
MSB will indicate the sign of the number
0 positive
1 negative
27
Example : n=4 1000 -8
0000 +0 1001 -7
0001 +1 1010 -6
0010 +2 1011 -5
0011 +3 1100 -4
0100 +4 1101 -3
0101 +5 1110 -2
0110 +6 1111 -1
0111 +7
To find the representation of, say, -4, first note that
+4 = 0100
-4 = 2’s complement of 0100 = 1011+1 = 1100
Rule : Value = – msb*2(n–1) + [unsigned value of rest]
Example: 0110 = 0 + 6 = 6 1110 = – 8 + 6 = – 2
28
Contd.
Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: 2n-1
Advantage:
Unique representation of zero
Subtraction can be done using addition
Leads to substantial saving in circuitry
Almost all computers today use the 2’s complement
representation for storing negative numbers

29
Contd.
In C
short int
16 bits + (215-1) to -215
int or long int
32 bits + (231-1) to -231
long long int
64 bits + (263-1) to -263

30
31
Adding Binary Numbers
Basic Rules: Example:
0+0=0
0+1=1 01101001
1+0=1 00110100
1+1=0 (carry 1) -------------
10011101

32
Subtraction Using Addition :: 1’s
Complement
How to compute A – B ?
Compute the 1’s complement of B (say, B1).
Compute R = A + B1
If the carry obtained after addition is ‘1’
Add the carry back to R (called end-around carry)
That is, R = R + 1
The result is a positive number
Else
The result is negative, and is in 1’s complement
form
33
Example 1 :: 6 – 2
1’s complement of 2 = 1101

Assume 4-bit
6 :: 0110 A representations
-2 :: 1101 B1 Since there is a carry, it is
1 0011 R added back to the result
1 The result is positive
0100 +4
End-around
carry

34
Example 2 :: 3 – 5
1’s complement of 5 = 1010

3 :: 0011 A
-5 :: 1010 B1
Assume 4-bit representations
1101 R
Since there is no carry, the
result is negative
1101 is the 1’s complement of
0010, that is, it represents –2
-2

35
Subtraction Using Addition :: 2’s
Complement
How to compute A – B ?
Compute the 2’s complement of B (say, B2)
Compute R = A + B2
If the carry obtained after addition is ‘1’
Ignore the carry
The result is a positive number
Else
The result is negative, and is in 2’s complement
form
36
Example 1 :: 6 – 2
2’s complement of 2 = 1101 + 1 = 1110

6 :: 0110 A Assume 4-bit


-2 :: 1110 B2 representations
Presence of carry indicates
1 0100 R that the result is positive
No need to add the end-
around carry like in 1’s
Ignore carry complement
+4

37
Example 2 :: 3 – 5
2’s complement of 5 = 1010 + 1 = 1011

3 :: 0011 A
-5 :: 1011 B2 Assume 4-bit representations

1110 R Since there is no carry, the


result is negative
1110 is the 2’s complement of
0010, that is, it represents –2
-2

38
2’s complement arithmetic: More
Examples
Example 1: 18-11 = ?
18 is represented as 00010010
11 is represented as 00001011
1’s complement of 11 is 11110100
2’s complement of 11 is 11110101
Add 18 to 2’s complement of 11

00010010
+ 11110101 00000111 is 7
----------------
00000111 (with a carry of 1
which is ignored) 39
Example 2: 7 - 9 = ?
7 is represented as 00000111
9 is represented as 00001001
1’s complement of 9 is 11110110
2’s complement of 9 is 11110111
Add 7 to 2’s complement of 9

00000111
+ 11110111 11111110 is -2
----------------
11111110 (with a carry of 0
which is ignored) 40
Overflow/Underflow:
Adding two +ve (-ve) numbers should not produce a
–ve (+ve) number. If it does, overflow (underflow) occurs

41
Overflow/Underflow:
Adding two +ve (-ve) numbers should not produce a
–ve (+ve) number. If it does, overflow (underflow) occurs

Another equivalent condition : carry in and carry


out from Most Significant Bit (MSB) differ.

42
Overflow/Underflow:
Adding two +ve (-ve) numbers should not produce a
–ve (+ve) number. If it does, overflow (underflow) occurs

Another equivalent condition : carry in and carry


out from Most Significant Bit (MSB) differ.

(64) 01000000
( 4) 00000100
--------------
(68) 01000100
carry (out)(in)
0 0
43
Overflow/Underflow:
Adding two +ve (-ve) numbers should not produce a
–ve (+ve) number. If it does, overflow (underflow) occurs

Another equivalent condition : carry in and carry


out from Most Significant Bit (MSB) differ.

(64) 01000000 (64) 01000000


( 4) 00000100 (96) 01100000
-------------- --------------
(68) 01000100 (-96) 10100000
carry (out)(in) carry out in differ:
0 0 0 1 overflow
44
Floating-point Numbers
The representations discussed so far applies only to
integers
Cannot represent numbers with fractional parts
We can assume a decimal point before a signed
number
In that case, pure fractions (without integer parts)
can be represented
We can also assume the decimal point somewhere in
between
This lacks flexibility
Very large and very small numbers cannot be
represented
45
Representation of Floating-Point
Numbers
A floating-point number F is represented by a
doublet <M,E> :
F = M x BE
B exponent base (usually 2)
M mantissa
E exponent
M is usually represented in 2’s complement
form, with an implied binary point before it
For example,
In decimal, 0.235 x 106
In binary, 0.101011 x 20110
46
Example :: 32-bit representation
M E
24 8
M represents a 2’s complement fraction
1 > M > -1
E represents the exponent (in 2’s complement form)
127 > E > -128
Points to note:
The number of significant digits depends on the
number of bits in M
6 significant digits for 24-bit mantissa
The range of the number depends on the number of
bits in E
1038 to 10-38 for 8-bit exponent. 47
A Warning
The representation for floating-point numbers as
shown is just for illustration
The actual representation is a little more
complex
Example: IEEE 754 Floating Point format

48
IEEE 754 Floating-Point Format
(Single Precision)
S E (Exponent) M (Mantissa)
(31) (30 … 23) (22 … 0)

S: Sign (0 is +ve, 1 is –ve)


E: Exponent (More bits gives a higher range)
M: Mantissa (More bits means higher precision)
[8 bytes are used for double precision]

Value of a Normal Number:


(-1)S (1.0 + 0.M) 2(E – 127)
49
An example
S E (Exponent) M (Mantissa)
(31) (30 … 23) (22 … 0)

1 10001100 11011000000000000000000

Value of a Normal Number:


= (-1)S (1.0 + 0.M) 2(E – 127)
= (-1)1 (1.0 + 0.1101100) 2(10001100 – 1111111)

= 1.1101100 21101 = 11101100000000


= 15104.0 ( in decimal)

50
Representing 0.3
S E (Exponent) M (Mantissa)
(31) (30 … 23) (22 … 0)

0.3 (decimal)
= 0.0100100100100100100100100…
= 1.00100100100100100100100100… 2 2
= 1.00100100100100100100100100… 2 125 127

= (-1)S (1.0 + 0.M) 2(E – 127)

0 01111101 00100100100100100100100

What are the largest and smallest numbers that


can be represented in this scheme?
51
Representing 0
S E (Exponent) M (Mantissa)
(31) (30 … 23) (22 … 0)

0 00000000 00000000000000000000000
1 00000000 00000000000000000000000

Representing Inf ( )
0 11111111 00000000000000000000000

1 11111111 00000000000000000000000

Representing NaN (Not a Number)


0 11111111 Non zero
1 11111111 Non zero
52
Data Types
Each variable has a type, indicates what
type of values the variable can hold
Four common data types in C
int - can store integers (usually 4 bytes)
float - can store single-precision floating
point numbers (usually 4 bytes)
double - can store double-precision floating
point numbers (usually 8 bytes)
char - can store a character (1 byte)
99
Contd.
Must declare a variable (specify its type and
name) before using it anywhere in your program
All variable declarations should be at the
beginning of the main() or other functions
A value can also be assigned to a variable at the
time the variable is declared.
int speed = 30;
char flag = ‘y’;

100
More Data Types in C
Some of the basic data types can be augmented
by using certain data type qualifiers:
short size qualifier
long
signed
unsigned sign qualifier

Typical examples:
short int (usually 2 bytes)
long int (usually 4 bytes)
unsigned int (usually 4 bytes, but no way to store + or
-)
101
Some typical sizes (some of these can vary
depending on type of machine)
Integer data Bit
Minimum value Maximum value
type size
char 8 -27=-128 27-1=127
short int 16 -215=-32768 215-1=32767
int 32 -231=-2147483648 231-1=2147483647
long int 32 -231=-2147483648 231-1=2147483647
-263=- 263-
long long int 64
9223372036854775808 1=9223372036854775807
unsigned char 8 0 28-1=255
unsigned short int 16 0 216-1=65535
unsigned int 32 0 232-1=4294967295
unsigned long int 32 0 232-1=4294967295
unsigned long lon 264-
g int
64 0
1=18446744073709551615
102
Computing ex series up to 4 decimal
places
void main () {
float x, term, sum;
int cnt;
scanf (“%f”, &x) ;
term = 1.0; sum = 0;
for (cnt = 1; term >= 0.0001; ++cnt) {
sum += term;
term *= x/cnt;
}
printf (“%f\n”, sum) ;
}

eseries-2.c 22
Equivalence of for and while
for ( expr1; expr2; expr3)
statement;

expr1;
Same as
while (expr2) {
statement
expr3;
}
23
void main () {
int N, count, sum;
Sum of first N Natural
scanf (“%d”, &N) ; Numbers
sum = 0;
count = 1;
while (count <= N) {
sum = sum + count;
count = count + 1; void main () {
} int N, count, sum;
printf (“%d\n”, sum) ; scanf (“%d”, &N) ;
} sum = 0;
for (count=1; count <= N; ++count)
sum = sum + count;
printf (“%d\n”, sum) ;
}

24
Some observations on for
 Initialization, loop-continuation test, and update
can contain arithmetic expressions
for ( k = x; k <= 4 * x * y; k += y / x )
 Update may be negative (decrement)
for (digit = 9; digit >= 0; --digit)
 If loop continuation test is initially 0 (false)
 Body of for structure not performed
 No statement executed

 Program proceeds with statement after for


structure
25
Looping: do-while statement
do
statement
statement;
while (expression);

False
do { expression
Block of statements;
} while (expression);
True

26
Example
Problem: Prompt user to input “month” value, keep
prompting until a correct value of month is given
as input

do {
printf (“Please input month {1-12}”);
scanf (“%d”, &month);
} while ((month < 1) || (month > 12));

27
Decimal to binary conversion
(prints binary in reverse order)
void main() {
int dec;
scanf (“%d”, &dec);
do
{
printf (“%2d”, (dec % 2));
dec = dec / 2;
} while (dec != 0);
printf (“\n”);
}
28
Echo characters typed on screen
until end of line
void main () {
char echo ;
do {
scanf (“%c”, &echo);
printf (“%c”,echo);
} while (echo != ‘\n’) ;
}
29
Specifying “Infinite Loop”

while (1) { for (; ;)


{
statements
statements
}
}

do {
statements
} while (1);

30
The break Statement

 Break out of the loop body { }


 can use with while, do while, for, switch
 does not work with if, else
 Causes immediate exit from a while,
do/while, for or switch structure
 Program execution continues with the
first statement after the structure

31
An Example
void main() {
int fact, i;
fact = 1; i = 1;
while ( i<10 ) {/* run loop –break when fact >100*/
fact = fact * i;
if ( fact > 100 ) {
printf ("Factorial of %d above 100", i);
break; /* break out of the while loop */
}
++i;
}
}
32
Test if a number is prime or not
void main() {
int n, i=2;
scanf (“%d”, &n);
while (i < n) {
if (n % i == 0) {
printf (“%d is not a prime \n”, n);
break;
}
++i;
}
if (i == n) printf (“%d is a prime \n”, n);
33
}
More efficient??
void main() {
int n, i = 2, flag = 0;
double limit;
scanf (“%d”, &n);
limit = sqrt(n);
while (i <= limit) {
if (n % i == 0) {
printf (“%d is not a prime \n”, n);
flag = 1; break;
}
i = i + 1;
}
if (flag == 0) printf (“%d is a prime \n”, n);
} 34
The continue Statement
 Skips the remaining statements in the body of
a while, for or do/while structure
 Proceeds with the next iteration of the loop
 while and do/while loop
 Loop-continuation test is evaluated
immediately after the continue statement is
executed
 for loop
 expr3 is evaluated, then expr2 is evaluated
35
An Example with break and
continue
void main() {
int fact = 1, i = 1;
while (1) {
fact = fact * i;
++i;
if ( i <=10 )
continue; /* not done yet ! Go to loop and
perform next iteration*/
break;
}
36
}
Some Loop Pitfalls
while (sum <= NUM) ; for (i=0; i<=NUM; ++i);
sum = sum+2; sum = sum+i;

for (i=1; i!=10; i=i+2)


sum = sum+i;

double x;
for (x=0.0; x<2.0; x=x+0.2)
printf(“%.18f\n”, x);

37
Nested Loops: Printing a 2-D
Figure
 How would you print the following
diagram?
*****
*****
*****

repeat 3 times
repeat 5 times
print a row of 5 *’s
print *

38
Nested Loops
const int ROWS = 3; row = 1;
const int COLS = 5; while (row <= ROWS) {
... /* print a row of 5 *’s */ outer
loop
row = 1; col = 1;
while (row <= ROWS) { while (col <= COLS) {
/* print a row of 5 *’s printf (“* “);
inner
*/ col++; loop
... }
++row; printf(“\n”);
} ++row;
}
39
2-D Figure: with for loop
Print const int ROWS = 3;
***** const int COLS = 5;
....
***** for (row=1; row<=ROWS; ++row) {
***** for (col=1; col<=COLS; ++col) {
printf(“* ”);
}
printf(“\n”);
}
40
Another 2-D Figure
Print const int ROWS = 5;
* ....
** int row, col;
for (row=1; row<=ROWS; ++row) {
*** for (col=1; col<=row; ++col) {
**** printf(“* ”);
***** }
printf(“\n”);
}
2d-figure.c 41
Yet Another One

Print const int ROWS = 5;


....
*****
int row, col;
**** for (row=0; row<ROWS; ++row) {
*** for (col=1; col<=row; ++col)
** printf(" ");
* for (col=1; col<=ROWS-row; ++col)
printf("* ");
printf ("\n");
}
42
break and continue with nested
loops
 For nested loops, break and continue are
matched with the nearest loops (for, while, do-
while)
 Example:
while (i < n) {
for (k=1; k < m; ++k) {
if (k % i == 0) break;
}
i = i + 1; Breaks here
}

43
Example
void main()
{
int low, high, desired, i, flag = 0;
scanf(“%d %d %d”, &low, &high, &desired);
i = low;
while (i < high) {
for (j = i+1; j <= high; ++j) {
if (j % i == desired) {
flag = 1;
break;
} Breaks here
}
if (flag == 1) break;
i = i + 1;
} Breaks here
} 44
The comma operator
 Separates expressions
 Syntax
expr-1, expr-2, …,expr-n
 expr-1, expr-2,…are all expressions
 Is itself an expression, which evaluates to the value of
the last expression in the sequence
 Since all but last expression values are discarded, not
of much general use
 But useful in for loops, by using side effects of the
expressions

45
Example
 We can give several expressions separated by
commas in place of expr1 and expr3 in a for
loop to do multiple assignments for example

for (fact=1, i=1; i<=10;++ i)


fact = fact * i;

for (sum=0, i=1; i<=N; ++i)


sum = sum + i * i;
46

You might also like