• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Best way(s) to work out IF/THEN/ELSE Logic?

NTB

Diamond Member
Just curious...if anybody has any good suggestions, I'm all ears 🙂 Simple stuff is just that - simple. But trying to work out the flow for multiple, usually inter-related conditions (and doing it correctly) can sometimes slow me to a crawl. A lot of times I can think of multiple ways to do it, but either A ) I'm not sure which way would be best or B ) *NONE* of them sound like a good solution 😛 So I thought I would post this to see how other, likely more experienced programmers approach such an issue. Maybe I can save some of the newer programmers, like myself, some headaches too.

The reason I bring this up is that I am working on what I though would be a simple 'class attendance'-type form. No such luck...the 'powers that be' have requested both a single 'all attended' checkbox, as well as the ability to set a 'never attended' flag or a 'last date of attendance' for each student. So:

(A) = 'all attended' flag
(B) = 'Never attended flag
(C) = 'Last Date Attended' box

  • If (A) is set, (B) and (C) should be blank for all students
  • If (A) is NOT set, either (B) -or- (C) must be set for at least 1 student
  • (B) and (C) should never both be set
  • Attendance records should only be updated if something has changed and the above rules have been followed.

There are a few other checks I can do, like having the 'last date' be valid only if it falls into the correct date range, but hopefully you get the general idea. This one still isn't too bad by any means, but it is what made me think to post this.

Nathan
 
So does this form allow a teacher to take attendance for a class of students on a particular attendance date?
 
IF A THEN
---IF B OR C THEN
------ERROR;
---ELSE
------Do Stuff;
ELSE
IF C AND NOT B THEN
---Do Stuff;
ELSE
IF B AND NOT C THEN
---Do Stuff;
ELSE
---ERROR;

That would probably be the way I set things up.
 
Originally posted by: RedCOMET
How about a switch/case with some if/else logic for the "sub" conditions?

You could do that, but it would effectively screw up anyone that is reading it. It would probably have to looks something like

int flags =0;
flags = (((A && A << 1) || (B && B)) << 1) || (C && C)

switch (flags)
{
---case 4:
------Do A Stuff;
------break;
---case 2:
------Do B Stuff;
------break;
---case 1:
------Do C Stuff;
------break;
---default:
------Error;
}

If you didn't do it that way, then the switch case statement would be completely useless. (because you conditions are only 1 or 0)
 
Something to catch all states.

if not A then
--if not B AND not C then
----if B AND not C then
------do stuff;
----else if C AND not B then
------update last attended;
----else
------ERROR;
--else
----ERROR;
else if B is null AND C is null then
---do stuff;
else
---ERROR;
 
Originally posted by: KIAman
Something to catch all states.

if not A then
--if not B AND not C then
----if B AND not C then
------do stuff;
----else if C AND not B then
------update last attended;
----else
------ERROR;
--else
----ERROR;
else if B is null AND C is null then
---do stuff;
else
---ERROR;

Ummm.. This wont work.

line 2, if not b and not c then, you proceed to look and do stuff if b or c is true. Basically, if A is false then it will always error out.
 
Originally posted by: KIAman
Something to catch all states.

if not A then
--if not B AND not C then
----if B AND not C then
------do stuff;
----else if C AND not B then
------update last attended;
----else
------ERROR;
--else
----ERROR;
else if B is null AND C is null then
---do stuff;
else
---ERROR;

:0 fixed!

if not A then
--if B AND not C then
----do stuff;
--else if C AND not B then
----update last attended;
--else
----ERROR;
else if B is null AND C is null then
---do stuff;
else
---ERROR;
 
Back
Top