Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C programming without if, while, for, switch, goto statements nor ternary operator?
#1
Javagony, a subset of Java, allows exception handlers for conditional branch and recursion for looping. Some algorithms on Javagony are shown on this repository.

I wondered if something similar is possible on other languages. I am taking up C as an example.

So far I have ever hit on using signal.h and setjmp.h for branching. I have ever made this program only:

Code:
// Get a number and branch if it is three.
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>

int if3then(int sig){
  puts("three!");
  exit(EXIT_SUCCESS);
}

int main(void){
  if(SIG_ERR==signal(SIGFPE,if3then)){
     fprintf(stderr,"f\n");
     exit(EXIT_FAILURE);
  }
  int n;
  scanf("%d",&n);
  int if3=1/(n-3);
  puts("not three");
}

On my environment I once tried to make a program that goes back to main() after branching and another branch will be made later, but found that never does the program get out of if3then(). What if raise(somethingElse) later then longjmp()?

Well, at least if you put an assembly code or call the system, would they be the answers for my question, but aren't they cheating?
Reply
#2
In C, most of what you're trying to do is undefined behavior, and thus the compiler can basically do whatever it wants with your code. There's no guarantee anything meaningful will happen if you divide by zero, let alone get a floating point error signal.
If you need to contact me for any reason, or if you have any questions, concerns, problems or requests, message me here or email me at aaaaaa123456789@acidch.at.

This forum has been around for (loading...)
Reply
#3
Eventually I thought this may be the best equivalent (not tested yet):

Code:
// some are included
int main(void){
   int n;
   scanf("%d",&n);
   puts({"three!","not."}[n==3]);
}

PS. Failed; should have I coded like this:

Code:
#include<stdio.h>
int main(void){
    int n;
    scanf("%d",&n);
    puts("three!\0not."+((n!=3)*7));
}



Next step: looping. Managed to code truth-machine.
But could it be shorter?

Code:
#include<stdio.h>
#include<stdlib.h>
void print1inf(){
    puts("1");
    print1inf();
}
int main(void){
    int n;
    scanf("%d",&n);
    printf("%d\n",n);
    void(*fn[2])(int)={print1inf,exit};
    fn[!n](EXIT_SUCCESS);
}
Reply
#4
You might find my old fizzbuzz without constants interesting.
If you need to contact me for any reason, or if you have any questions, concerns, problems or requests, message me here or email me at aaaaaa123456789@acidch.at.

This forum has been around for (loading...)
[-] The following 1 user says Thank You to aaaaaa123456789 for this post:
  • queha
Reply
#5
Conclusion: Yes you can, using some arrays of function pointers.
The blog shows some examples to solve ten given problems on a competitive programming.
Reply
#6
When I posted the article on Qiita, two commenters pointed out there are more than one way to do so. They were:
  • to use logical operators
  • to use intptr_t
  • to use longjmp() and setjmp()
Reply




Users browsing this thread: 1 Guest(s)