C Language Conditional control statement (ii)

Source: Internet
Author: User
Tags printf

3.if...elseif statement

The actual application often faces more choice, at this time, will If...else expands, then obtains the IF...ELSEIF structure, its general form is:

if< expression 1>
    statement 1
else if< expression 2>
     statement 2
     else if< expression 3>
            statement 3
else Statement 4

The corresponding flowchart is shown in Figure 3-4.

[Example 3-7] The problem of taxation of goods, the price of more than 10,000 yuan in the levy of 5%, 5000 yuan above 10,000 yuan below the levy 3%, 1000 yuan above 5000 below the levy 2%, 1000 yuan below the tax exemption, read the price of goods, calculate and export taxes.

Analysis: Read price, compute tax, this is a more complex branch structure program design (should pay attention to avoid repeated taxation). Assuming that the price of goods is more than 10,000 yuan, the tax should be divided into cumulative, each paragraph using different tax rates for collection.

Algorithm:

若price>=10000
则tax=0.05*(price-10000);price=10000;
否则,若price>=5000
则tax=0.03*(price-5000)+tax;price=5000;
否则,若price>=1000
则tax=0.02*(price-1000)+tax;price=1000;

The procedure is as follows:

#include <stdio.h>
main()
{
float price,tax=0;
printf("input price:");
scanf("%f",&price);
if(price>=10000.0)
{
tax=0.05*(price-10000)+tax;price=10000;
}
if(price>=5000.0)
{
tax=0.03*(price-5000)+tax;price=5000;
}
if(price>=1000.00)
{
tax=0.02*(price-1000)+tax;
}
printf("thetax=%10.3f",tax);
}

To run the program:

RUN¿
15000¿
thetax=480.000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.