Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then Else Statement 1

Status
Not open for further replies.

ReportGuy

Programmer
May 8, 2003
9
US
Crystal Designer 9
Database: Xtreme Sample Database 9

I'm having an issue with an "if then else statement" in one of my reports. The problem is when the YearHired doesn't equal 1991, it doesn't get evaluated. I recreated the formula using the Xtreme database, and I'm still having the problem.

Local NumberVar YearHired := Year({Employee.Hire Date});
If YearHired = 1991 then
if {Employee.Salary} > 75000 then
{Employee.Salary}+({Employee.Salary}*.10)
else if {Employee.Salary} <= 75000 then
{Employee.Salary}+({Employee.Salary}*.08)
else if {Employee.Salary} < 35000 then
{Employee.Salary}+({Employee.Salary}*.06)
Else
If YearHired = 1992 then
if {Employee.Salary} > 45000 then
{Employee.Salary}+({Employee.Salary}*.09)
else if {Employee.Salary} <= 45000 then
{Employee.Salary}+({Employee.Salary}*.07)
Else
If YearHired = 1993 then
if {Employee.Salary} > 55000 then
{Employee.Salary}+({Employee.Salary}*.09)
else if {Employee.Salary} >=30000 then
{Employee.Salary}+({Employee.Salary}*.06)
else if {Employee.Salary} < 30000 then
{Employee.Salary}+({Employee.Salary}*.04)
Else
If YearHired = 1994 then
if {Employee.Salary} > 70000 then
{Employee.Salary}+({Employee.Salary}*.10)
else {Employee.Salary}+({Employee.Salary}*.05)







 
Try this:
------------------------------------------------------
Local NumberVar YearHired := Year({Employee.Hire Date});
Local CurrencyVar salary := {Employee.Salary};
If YearHired = 1991 then
( if salary > 75000 then salary * 1.10
else if salary > 35000 then salary * 1.08
else salary * 1.06 )
Else
If YearHired = 1992 then
( if salary > 45000 then salary * 1.09
else salary * 1.07 )
Else
If YearHired = 1993 then
( if salary > 55000 then salary * 1.09
else if salary > 30000 then salary * 1.06
else salary * 1.04 )
Else
If YearHired = 1994 then
( if salary > 70000 then salary * 1.10
else salary * 1.05 )
-----------------------------------------------------------
You don't need to do the final "else if" on your inner If Then Else statement. It can be assumed.
Also, it's always a good idea to put your inner statements in parentheses.
HTH
Bob Suruncle
By the way, didn't you CR RD2 instructor tell you this?








Bob Suruncle
 
This is easier with a case statemnt, but if you want to use If.Then.Else, you'll need to use some brackets.

Local NumberVar YearHired := Year({Employee.Hire Date});
If YearHired = 1991 then (
if {Employee.Salary} > 75000 then
{Employee.Salary}+({Employee.Salary}*.10)
else if {Employee.Salary} <= 75000 then
{Employee.Salary}+({Employee.Salary}*.08)
else if {Employee.Salary} < 35000 then
{Employee.Salary}+({Employee.Salary}*.06)
)
Else
If YearHired = 1992 then (
if {Employee.Salary} > 45000 then
{Employee.Salary}+({Employee.Salary}*.09)
else if {Employee.Salary} <= 45000 then
{Employee.Salary}+({Employee.Salary}*.07)
)
Else
If YearHired = 1993 then (
if {Employee.Salary} > 55000 then
{Employee.Salary}+({Employee.Salary}*.09)
else if {Employee.Salary} >=30000 then
{Employee.Salary}+({Employee.Salary}*.06)
else if {Employee.Salary} < 30000 then
{Employee.Salary}+({Employee.Salary}*.04)
)
Else
If YearHired = 1994 then
if {Employee.Salary} > 70000 then
{Employee.Salary}+({Employee.Salary}*.10)
else {Employee.Salary}+({Employee.Salary}*.05);

Andrew Baines
Chase International
 
I rewrote it using case statements as I find them easier to read then nested If's. It seems to be working fine now.
Code:
Local NumberVar YearHired := Year({Employee.Hire Date});
select YearHired 
    case 1991 :
        (
        select {Employee.Salary}
            case Is >  75000 : {Employee.Salary}*1.10
            case Is >= 35000 : {Employee.Salary}*1.08
            default          : {Employee.Salary}*1.06
        )
    case 1992 : 
        (
        select {Employee.Salary}
            case Is > 45000  : {Employee.Salary}*1.09
            default          : {Employee.Salary}*1.07
        )
    case 1993 :
        (
        select {Employee.Salary}
            case Is > 55000  : {Employee.Salary}*1.09
            case Is >= 30000 : {Employee.Salary}*1.06
            default          : {Employee.Salary}*1.04
        )
    case 1994 : 
        (
        select {Employee.Salary}
            case Is > 70000  : {Employee.Salary}*1.10
            default          : {Employee.Salary}*1.05
        );

~Brian
 
Brian,

I don't recall seeing the use of "case is" before. Thanks for demonstrating it (*).

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top