关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include 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(q
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/04 08:34:31
![关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include void main() { double unit_price = 5.0; long quantity = 0L; double discount = 0.0; printf(](/uploads/image/z/5444438-14-8.jpg?t=%E5%85%B3%E4%BA%8EC%E8%AF%AD%E8%A8%80if%E8%AF%AD%E5%8F%A5%E4%B8%AD%E8%8A%B1%E6%8B%AC%E5%8F%B7%7B%7D%E7%9A%84%E4%BD%BF%E7%94%A8%E5%8C%BA%E5%88%AB%3F%EF%BC%881%EF%BC%89%E5%8A%A0%E8%8A%B1%E6%8B%AC%E5%8F%B7%E7%9A%84%E7%A8%8B%E5%BA%8F%23include+void+main%28%29+%7B+double+unit_price+%3D+5.0%3B+long+quantity+%3D+0L%3B+double+discount+%3D+0.0%3B+printf%28%22%5CnEnter+the+number+that+you+want+to+buy%3A%22%29%3B+scanf%28%22%25ld%22%2C%26quantity%29%3B+if%28q)
关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include 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(q
关于C语言if语句中花括号{}的使用区别?
(1)加花括号的程序
#include
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
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语句中就不用加花括号呢?区别在哪?
关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include 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(q
简单地说,用花括号括起来的代码块是一个整体,在运行的时候就像一条语句一样执行下来.
if执行的时候会执行到它后面的第一个分号为止,也就是说,它只执行一条语句,除非你用花括号把后面的一串语句括起来变成一条语句.
以上.
喜欢简单的老狼