Friday, 1 May 2009

Double check the logic

As part of the task, I had been given some logic from a specification like this, and have to code (similar to this) in C++: -

On paper

There are 6 categories, A, B, C, D, E, and F.
Two images, image1, and image 2.

if (category == "A" || cateogory == "B")
[
Output Image1
]

if (category != "B" || category != "B")
[
Output Image2
]

The code that I wrote: -

if (category == "A" || category == "B")
{
output(image1);
}

if (category != "A" || category != "B")
{
output(image2);
}

Now this is wrong in terms of the logic, which I had found out that if it specifies this in the logic, the code doesn't reflect this.
This is because, say category A can fall into both the if statements, its an OR statement so it can meet the criteria of category B

Solution :

if (category != "A")
{
//Fall into the next one
if (category != "B")
{
Output(image2);
}
}

This solves the logical error.

No comments: