Solved

IF / ELSE IF

Posted on 2011-09-09
3
260 Views
Last Modified: 2012-05-12
Experts,
Any help on this problem would be greatly appreciated!

PROGRAM DETAILS: The user is suppose to select a number between 0 and 5000, the code will jump to the IF /ELSE IF code block and execute.

PROBLEM: The first two code blocks execute correctly (< 250.00 and 250.01 > || <= 500.00) however if I place a number above 500 it will continue to execute the second code block resulting in $8.00 to be charged in the shipping rate rather than $10.00 (in this example I illustrated it would be $10.00, etc..).

CODE:
using System;

namespace Shipping Program
{
    class Program
    {
        static void Main()
        {
            double totAmount;
            double shippingChrg;
            double totalCost;

            Console.WriteLine("Please enter the amount of your purchase");
            totAmount = Convert.ToDouble(Console.ReadLine());

            if (totAmount < 250.00)
            {
                totalCost = totAmount + 5.00;
                shippingChrg = 5.00;
                Console.WriteLine("The shipping charge on a purchase of " + totAmount.ToString("C2") + " is: " + shippingChrg.ToString("C2") + " the total cost is: " + totalCost.ToString("C2"));
            }
            else if (totAmount > 250.01 || totAmount <= 500.00)
            {
                totalCost = totAmount + 8.00;
                shippingChrg = 8.00;
                Console.WriteLine("The shipping charge on a purchase of " + totAmount.ToString("C2") + " is: " + shippingChrg.ToString("C2") + " the total cost is: " + totalCost.ToString("C2"));
            }
            else if (totAmount > 501.01 || totAmount < 1000.00)
            {
                totalCost = totAmount + 10.00;
                shippingChrg = 10.00;
                Console.WriteLine("The shipping charge on a purchase of " + totAmount.ToString("C2") + " is: " + shippingChrg.ToString("C2") + " the total cost is: " + totalCost.ToString("C2"));
            }
            else if (totAmount > 1000.01 || totAmount < 1000.00)
            {
                totalCost = totAmount + 15.00;
                shippingChrg = 15.00;
                Console.WriteLine("The shipping charge on a purchase of " + totAmount.ToString("C2") + " is: " + shippingChrg.ToString("C2") + " the total cost is: " + totalCost.ToString("C2"));
            }
            else (totCost = totAmount > 5000.01 )
            {
                totCost = totAmount + 20.00;

            }
                Console.ReadLine();
       }
    }
}

Open in new window

0
Comment
Question by:Michael Reeve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 36514882
totAmount > 250.01 || totAmount <= 500.00 should a conditional AND statement. It is executing because $500 is greater 250.01.

totAmount > 250.01 && totAmount <= 500.00
0
 

Author Closing Comment

by:Michael Reeve
ID: 36514905
Thank you so for catching my mistake! Really appreciate the quick response.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 36514937
You're welcome. I hadn't noticed these two earlier. I think that the first one is the same thing, should &&. I think that the other one should be under 5000.00

line 28 - totAmount > 501.01 || totAmount < 1000.00
line 34 - totAmount > 1000.01 || totAmount < 1000.00
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question