Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (doc.brown):

If-less programming, the very basics.

OpenStudy (doc.brown):

I just can't understand the most basic concept of programming without if statements. For example: Ask the user for a number { if the value is a number then print the number else if print "Not a number" } How do I achieve the same 2 outputs without using the ifs?

OpenStudy (rsmith6559):

Because of your question, I read a few articles on if-less programming. It's very interesting. The concept as I see it is to improve code quality by proper use of inheritance and polymorphism in OOP. Your example could, and maybe rightfully so, be written in a try, catch structure, with the "print "Not a number"" command being the error handler. Arguably better, cleaner, easier to test and more maintainable. What the articles really seemed to be against was type testing variables to decide how to proceed. Use polymorphism so if the variable is a string, the string version of the foo method is used. If the variable is a number, use the number version of the method foo. Either way, your controlling code would just be calling object.foo() . In your example, which is a bit simple for this to matter: void printNumber( int foo ) { std::cout << foo << std:endl; } void printNumber( string foo ) { std::cout << "Not a number" << std::endl; } The discussions of if-less programming reminds me of the discussions of goto-less programming back in the '80s.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!