mpsn Posted February 28, 2012 Share Posted February 28, 2012 Hi, I need some help with homework ques using the following function: <?php function getValue($x, $y, $z) { if ($x != 0) $y = 5; else $z = $z-$x; if ($z > 1) $z = $z/$x; else $z = $y; return $z; } ?> Here is the question: Provide a function, getValue(x y z), for the number of paths through the program in terms of x, y, and z. I'd appreciate any help, I think the ques is asking for a general formula, but I think the number of paths is just 4, b/c each if-statement can be: T to F, T to T, F to F, F to T. I don't know... Quote Link to comment https://forums.phpfreaks.com/topic/257913-number-of-paths/ Share on other sites More sharing options...
Alex Posted March 2, 2012 Share Posted March 2, 2012 In this case it would be 4, but you have to be careful because that's not necessarily the case just because there are two if/else statements. For example, consider the following code: // $x, $y, $z each given a true or false value if($x) { $y = true; } else { $z = true; } if($x || $z) { } else { } In that case it would be impossible for the else statement on the second if statement to ever be called. If $x had an initial value of true, then the if statement is true, but if $x was false to begin with then $z gets set to true, and as a result the statement, $x || $z is still true. Quote Link to comment https://forums.phpfreaks.com/topic/257913-number-of-paths/#findComment-1323189 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.