Jump to content

Number of paths


mpsn

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/257913-number-of-paths/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257913-number-of-paths/#findComment-1323189
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.