Tuesday, 21 January 2014

Control Flow in C++ vs Java

Control flow is a term which is used a lot in computer science. It refers to the different paths of executions a program may choose to run.

If, else, while, for and do  are some of the keywords used in both languages and have almost the same meaning. These keywords are associated with a condition to be checked upon.

In C++ the result of the conditional test may return any integer value(negative, positive or zero). In Java the result of the test should be a boolean( true or false) value and anything else is not permitted( if you try you may get a compile time error). If C++ can return an integer for a conditional test  then, how does the C++ compiler decide on which path to choose?

The answer is, in C++, any non-zero integer or pointer is considered to be "true", and the integer 0 is considered to be "false".




No comments:

Post a Comment