+ 1

Alternative to if-else/swith-case/ternary opertor.

A C program could be done using if-else,swith-case, ternary operator. Is there any other way to do the same?

21st Oct 2018, 1:45 PM
Abdus Samee
Abdus Samee - avatar
2 Antworten
+ 3
Kind of? Computer science theory says that to make a fully functioning programming language, all you need is a way to jump to different parts of your code, based on some condition ("branching"). if (a) { b; } Is the same as: for(;a;){ b; break; } while(a){ b; break; } if(!a){ goto X; } b; X: a && b; // this works because of "short-circuiting" And pretty much anything else you can think of. You could for example store two functions in an array and branch by picking the correct one. Or C also has the `longjmp` function which is similar to `goto`. But anyway, you listed the most important ones, if you know those you will be fine!
21st Oct 2018, 2:07 PM
Schindlabua
Schindlabua - avatar
+ 2
Yes. Function pointers> Mostly precise Array of function pointers. The C course here in SoloLearn have a module talking about that. With code example.
21st Oct 2018, 1:56 PM
Anya
Anya - avatar