(1)加花括号的程序
#include <stdio.h>
void main()
{
double unit_price = 5.0;
long quantity = 0L;
double discount = 0.0;
printf("\nEnter the number that you want to buy:");
scanf("%ld",&quantity);
if(quantity > 30 && quantity < 50)
{
discount = 0.1;
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price* quantity *(1.0 - discount));
}
if(quantity > 50)
{
discount = 0.15;
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price * quantity *(1.0 - discount));
}
if(quantity > 0 && quantity < 30)
{
discount = 0.0;
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price * quantity *(1.0 - discount));
}
getch();
}
(2)不加花括号的程序
#include <stdio.h>
void main()
{
double unit_price = 5.0;
long quantity = 0L;
double total_price = 0.0;
double discount1 = 0.1;
double discount2 = 0.15;
double discount3 = 0.0;
printf("\nEnter the number that you want to buy:");
scanf("%ld",&quantity);
if(quantity > 30 && quantity < 50)
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price * quantity *(1.0 - discount1));
if(quantity > 50)
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price * quantity *(1.0 - discount2));
if(quantity > 0 && quantity < 30)
printf("\nThe price for %ld is $%.2lf.",quantity,unit_price * quantity *(1.0 - discount3));
getch();
}
我想问一下这俩个程序都是一样的结果 为什么第1个程序IF语句中要加花括号而第2个程序IF语句中就不用加花括号呢?区别在哪?