Control Flow
Control Flow
Taken from: Feuer, A. R., (1999). The C Puzzle Book, 3rd Printing.
USA. Addison Wesley Longman Inc. Used for educational
purposes.
Control Flow
Control Flow 1: if Statement
Explain
Control Flow 1: while and for Statement
Explain
Control Flow 3: Statement Nesting
Explain
Control Flow 4: switch , break and continue
Explain
Control Flow
C, as most programming languages, has control constructs for
conditional selection and looping. To work the puzzles in this
Control Flow 1
section, you will need to know how to determine the extend of each
construct. In a weel formated program, extend is indicated by
indentation. Reeding a poorly formated is dificult and error prone;
the following puzzles should convince you.
#include "defs.h"
int main()
{
int x, y = 1, z;
if( y!=0 ) x = 5;
PRINT1(d, x); // Control Flow 1.1
if( y==0 ) x = 3;
else x = 5;
PRINT1(d, x); // Control Flow 1.2
x = 1;
if( y<0 ) if( y>0 ) x = 3;
else x = 5;
PRINT1(d, x); // Control Flow 1.3
if( z=y<0 ) x = 3;
else if( y==0 ) x = 5;
else x = 7;
PRINT2(d, x, z); // Control Flow 1.4
if( z=(y==0) ) x = 5;
x = 3;
PRINT2(d, x, z); // Control Flow 1.5
if( x=z=y ) x = 3;
PRINT2(d, x, z); // Control Flow 1.6
system("pause");
}
Control Flow 2
x = 5
x = 5
x = 1
x = 7 z = 0
x = 3 z = 0
x = 3 z = 1
Explain
#include "defs.h"
int main()
{
int x, y, z;
x=y=0;
while( y<10 ) ++y; x += y;
PRINT2(d, x, y); // Control Flow 2.1
x=y=0;
while( y<10 ) x += ++y;
PRINT2(d, x, y); // Control Flow 2.2
y=1;
while( y<10 ) {
x = y++; z = ++y;
}
PRINT3(d, x, y, z); // Control Flow 2.3
system("pause");
}
Control Flow 3
x = 10 y = 10
x = 55 y = 10
x = 9 y = 11 z = 11
x = 9 y = 10
x = 10 y = 10
x = 0 y = 1000
x = 1 y = 100
x = 2 y = 10
Explain
#include "defs.h"
#define ENUF 3
#define EOS '\0'
#define NEXT(i) input[i++]
#define FALSE 0
#define TRUE 1
char input[] = "PI=3.14159, approximately";
int main()
{
char c;
int done, high, i, in, low;
i=low=in=high=0;
while( c=NEXT(i) != EOS)
if( c<'0' ) low++;
else if( c>'9' ) high++;
else in++;
PRINT3(d, low,in,high); // Control Flow 3.1
i=low=in=high=0; done=FALSE;
while( (c=NEXT(i)) !=EOS && !done )
if( c<'0' ) low++;
else if( c>'9' ) high++;
else in++;
if( low>=ENUF || high>=ENUF || in>ENUF ) done=TRUE;
PRINT3(d,low,in,high); // Control Flow 3.2
i=low=in=high=0; done=FALSE;
while( (c=NEXT(i)) !=EOS && !done )
Control Flow 4
if( c<'0' ) done = (++low==ENUF ? TRUE : FALSE) ;
else if( c>'9' ) done = (++high==ENUF ? TRUE : FALSE);
else done = (++in==ENUF ? TRUE : FALSE);
PRINT3(d,low,in,high); // Control Flow 3.3
system("pause");
}
low = 25 in = 0 high = 0
low = 3 in = 6 high = 16
low = 0 in = 0 high = 3
Explain
#include "defs.h"
int main()
{
int i, c;
for( i=2; (c=input[i])!='\0'; i++) {
switch(c) {
case 'a': putchar('i'); continue;
case '1': break;
case 1: while( (c=input[++i])!='\1' && c!='\0' ) ;
case 9: putchar('S');
case 'E':
case 'L': continue;
default: putchar(c);
continue;
}
putchar(' ');
}
putchar('\n');
system("pause");
}
Control Flow 5
SWITCH SWAMP
Explain
Control Flow 6