关于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/05/04 07:52:21
关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include void main() { double unit_price = 5.0; long quantity = 0L; double discount = 0.0; printf(

关于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执行的时候会执行到它后面的第一个分号为止,也就是说,它只执行一条语句,除非你用花括号把后面的一串语句括起来变成一条语句.
以上.
喜欢简单的老狼

关于C语言if语句中花括号{}的使用区别?(1)加花括号的程序#include void main() { double unit_price = 5.0; long quantity = 0L; double discount = 0.0; printf( Enter the number that you want to buy:); scanf(%ld,&quantity); if(q 关于C语言中if语句后面是否加括号的问题.这里是加了括号.但是看到其他地方也有不加括号的, C语言的分支语句中,if()...if()...else...和if()...else if()...else...两者有什么区别? 如何在C语言中使用汉字作为if的判断语句? 在C语言中,if和else if是不是在不加花括号的情况下也是一个复合语句 c语言作业 编写一个输出学生成绩等级的程序1、能够使用if else语句实现. 2、多分支结构的正确使用. 3、能够使用switch case语句实现. 4、正确使用break语句 5、if语句和switch语句的使用区别和注 c 语言中while 语句中能加if else 语句么? C语言中怎么让while语句内的if语句反复判断? C语言中怎么让while语句内的if语句反复判断? C语言中关于if语句的问题为什么会出现以下情况,if正好起到了相反的效果,怎么改 C语言中 if else语句格式搞糊涂了!if(1>0){.;.;.;}else{.;.;.;}else后面可以这样加大括号吗?后面是不是还要加end if; C语言IF语句嵌套问题下列IF语句中,ENDIF表示相应的IF的结束:Y=0 IF X 在C语言中,if(!(i%10))这类条件语句中,的作用是什么? C语言语句“switch(--d%4)”“if(!(i%2))”中, C语言中常见的if语句的形式有哪几种?分别说出它们的作用 C语言中if判断语句为什么是错误的仍执行了它下面的语句?已经检验过if判断语句不成立! C语言中if语句的三种形式到底是怎样的?分别举例 C语言中,连续写2个if语句,和用if...else if有什么区别比如if(x==20)语句1;if (x==30)语句2;和 if(x==20)语句1;else if(x==30)语句2;这2者有什么区别?