Program without conditional Statements - Possible ?

  • Thread starter Raghavendra R A V, CSS India
  • Start date
R

Raghavendra R A V, CSS India

hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu
 
M

Martin Dickopp

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

int main (void) { return 0; }

Martin
 
J

Joona I Palaste

Raghavendra R A V said:
Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...
Urgent - Please reply soon !
Thanks, Raghu

Yes, I know how to do this. It's surprisingly easy. Here's one such
program:

/* CODE STARTS HERE */
int main(void) {
return 0;
}
/* CODE ENDS HERE */

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
 
G

gabriel

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

void main() { puts("hello world"); }

Any questions?

The point is that yes it's possible, but you have to write it yourself
because only you know what the program is supposed to do...

Maybe you're asking "how do I make a decision in a program without actually
making a decision"... But if that's what you're asking for, then ask...
 
J

Jeremy Yallop

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ?

Use an array of function pointers. For example, here's a program
(with no "conditional" statements) that accepts a number of integers
as command line arguments and prints their sum.

#include <stdio.h>
#include <stdlib.h>

long ifthenelse(int testval,
long (*true)(char **, long),
long (*false)(char **, long),
char **p,
long c) {
long (*next[2])(char **, long);
next[0] = false;
next[1] = true;
return next[!!testval](p, c);
}

long end(char **p, long c) { return c; }

long acc(char **p, long c) {
return ifthenelse(p[1] != 0,
acc,
end,
p + 1,
c + strtol(*p, NULL, 0));
}

int main(int argc, char **argv) {
printf("Total: %ld\n", ifthenelse(argc > 1, acc, end, argv, 0));
return 0;
}
It would be a great help for me if someone helps me...

Urgent - Please reply soon !

You might want to look into upgrading your C compiler as well.

Jeremy.
 
E

E. Robert Tisdale

Does any one know how to write a C program
without using the conditional statements
if, for, while, do, switch, goto and even conditional statements?
Yes.

It would be a great help for me if someone helps me...

Do you have any experience with an *applicative*
(sometimes called *functional*) computer programming language
such as the LISt Processing (LISP) computer programming language?

The idea is to write programs containing *only* constant
(because C supports numerous different types) and function definitions.
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}

which can be used to define constants:

const int k = min(i, j);

or used in expressions to define other functions.
You define and apply recursive functions
instead of iteration in loops.
 
A

Andras Tantos

Do any one knows how to write a C program without using the
int main (void) { return 0; }

On a more serious note:

/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */
/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.

You can use recursion to simulate loops.
 
G

gabriel

Jeremy said:
printf("Total: %ld\n", ifthenelse(argc > 1, acc, end, argv, 0));

Bah, you just disguised an if-then-else statement as a function call, no?
 
G

gabriel

condotional statements ? It would be a great help for me if someone
helps me...

Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

It seems like sending someone on a quest to find a really complicated way
of solving a simple problem...
 
J

Julian V. Noble

Raghavendra R A V said:
hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu

He may want to do logical arithmetic with bitwise logical operators.
For example,

int my_max1( int a, int b)
{
int c;
c = (a<b);
return a * (!c) + b * (c);
}

int my_max2( int a, int b)
{
int c;
c = (a<b);
return (a & (-(!c))) + (b & (-c)); // this supposes -1 has all bits set!
}

The C convention, that "true" is 1, is very inconcenient for this.

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything, and thereby take away our free wiil
and that share of glory that rightfully belongs to ourselves."


-- N. Machiavelli, "The Prince".
 
S

Sidney Cadot

Richard said:
gabriel wrote:




Just one. How did you manage to get a one-line C program *wrong*???

That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.

Best regards, Sidney
 
S

Sidney Cadot

Sidney said:
Richard Heathfield wrote:


That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.

....Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side effect.

-- Sidney
 
D

Derk Gwen

(e-mail address removed) (Raghavendra R A V, CSS India) wrote:
# hie..
#
# Do any one knows how to write a C program without using the
# conditional statements if, for, while, do, switch, goto and even
# condotional statements ? It would be a great help for me if someone
# helps me...

It should be coverred in the part of your text devoted lambda notation or
combinator. The notation in C is clumsy, since you have to recreate lambdas
and continuations. The basic trick is to recognise
if (p) q; else r;
can be rewritten
If = \p,q,r. (Lambda choice[2] = {\.q, \.r}, choice[p()]())

While requires recursion and so you need to embed the environment.
while (p) q;
then becomes
While = \p,q. (If(p,\.While(p,q),Skip))

With those two out of the way,
for (p; q; r) s
For = \p,q,r,s. (p,While(q,\.(s,r)))
do p while (q)
Do = \p,q. (p, While(q,p))
switch (p) {case q: r; break; case t: u; break;...}
Switch = \p,q,r,s. (If(\.p==q,\.r,s))
Switch(p,q,\.r,\.Switch(p,t,\.u,\. ...))

For gotos and labels, there's an old article on converting
Algol 60 to ISWIM.
 
R

Richard Heathfield

Sidney said:
...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,

Er, no. :)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1
and have a side effect.

A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}

For C99, the line is beginning to stretch a little:

int puts(const char *);int main(){return 0*puts("Hello world");}
 
G

gabriel

Richard said:
Just one. How did you manage to get a one-line C program *wrong*???

I was trying to make a point, not please the c-std religious feelings of
others here (feel free to "correct" it if you want). Anyway, I'm putting
much more thought on this than on writing the one-liner program, so that
gives you an idea...
 
S

Sidney Cadot

Richard said:
Sidney Cadot wrote:

Sidney Cadot wrote:




...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,


Er, no. :)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1
and have a side effect.


A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}

I would think this program actually /is/ wrong "by the standards of c.l.c".
For C99, the line is beginning to stretch a little:

int puts(const char *);int main(){return 0*puts("Hello world");}

Hmmm. I guess I failed to stipulate that providing your own prototypes
doesn't count.

"Providing your own prototypes doesn't count."

....So there :p

Best regards, Sidney
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top