热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c语言if语句_Cif语句–第2部分

c语言if语句Inthelasttutorialwehavelearntaboutthebasicuseofdecisioncontrolinstructionusingifsta

c语言if语句

In the last tutorial we have learnt about the basic use of decision control instruction using if statement. Today we will extend that tutorial with some advanced examples.

A bit more about conditions

As I told you earlier that if statement is used with some conditions. Truly speaking it can also be used with some expression.
So the general form of if statement is

if(expression)
{
Statement 1
Statement 2
. . . . .
. . . . .
}

What is expression?

This expression can be any valid expression including relational and arithmetic expression. Some common examples are given below.

if(3+2)
printf(“This will print everytime”);

if(6/2+4)
printf(“This will print everytime”);

if(0)
printf(“This will never execute”);

Conclusion

Truly speaking in C language any non-zero number is considered to be TRUE whereas zero is considered as FALSE.

During the running of decision control instruction, a compiler checks the expression either it evaluates 0 or not. If the result is 0 then it will never execute the body of if statement. But if the expression evaluate to any non-zero number then it will execute the body of if statement.

In the first two examples, expression under if evaluates a non-zero value. So it will print the message.

In the last example, I have written 0. So the printf() statement is not executed. 

Note: Usage of expression is very important in C language. While writing big programs it is used very frequently. So I recommend you to understand the basic logic behind it correctly. If you have any problems then you can also post your queries in comments.

Now lets take one slightly complicated program to understand all the basics associated with if statement.

Sample Program

Make one program which will display the total amount after deducting the discount of 10%, if the total is more than 2000.


Output

C if Statement - Part 2

Now let’s try to understand this program


1. I hope you must understand the basic initial steps of this program. I will give the explanation for only if statement.

2. In if statement I have given the condition of (amnt>2000), it means it will check the amount if it is greater than 2000. Say, if it is greater than the amount then it will execute the statements in the body of if. Otherwise it will skip it.

3. In our test run we have given the value 2100, as it is greater than 2000. So the control passes inside the if statement body and do following things.

– First it will display the message “You are eligible for discount”
– In the next step it will calculate the discount i.e. 10% of the total amount.
– In the final step it will deduct the discount from the final amount.

4. Now in the last step it will display the final amount.

翻译自: https://www.thecrazyprogrammer.com/2014/12/c-if-statement-part-2.html

c语言if语句



推荐阅读
  • 本文探讨了如何在PHP与MySQL环境中实现高效的分页查询,包括基本的分页实现、性能优化技巧以及高级的分页策略。 ... [详细]
  • 本文介绍了SIP(Session Initiation Protocol,会话发起协议)的基本概念、功能、消息格式及其实现机制。SIP是一种在IP网络上用于建立、管理和终止多媒体通信会话的应用层协议。 ... [详细]
  • 本文详细介绍了 `org.apache.tinkerpop.gremlin.structure.VertexProperty` 类中的 `key()` 方法,并提供了多个实际应用的代码示例。通过这些示例,读者可以更好地理解该方法在图数据库操作中的具体用途。 ... [详细]
  • 洛谷 P4009 汽车加油行驶问题 解析
    探讨了经典算法题目——汽车加油行驶问题,通过网络流和费用流的视角,深入解析了该问题的解决方案。本文将详细阐述如何利用最短路径算法解决这一问题,并提供详细的代码实现。 ... [详细]
  • 本文介绍了如何通过C#语言调用动态链接库(DLL)中的函数来实现IC卡的基本操作,包括初始化设备、设置密码模式、获取设备状态等,并详细展示了将TextBox中的数据写入IC卡的具体实现方法。 ... [详细]
  • td{border:1pxsolid#808080;}参考:和FMX相关的类(表)TFmxObjectIFreeNotification ... [详细]
  • 本文是对《敏捷软件开发:原则、模式与实践》一书的深度解析,书中不仅探讨了敏捷方法的核心理念及其应用,还详细介绍了面向对象设计的原则、设计模式的应用技巧及UML的有效使用。 ... [详细]
  • 在尝试加载支持推送通知的iOS应用程序的Ad Hoc构建时,遇到了‘no valid aps-environment entitlement found for application’的错误提示。本文将探讨此错误的原因及多种可能的解决方案。 ... [详细]
  • 本文详细介绍了Oracle 11g中的创建表空间的方法,以及如何设置客户端和服务端的基本配置,包括用户管理、环境变量配置等。 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 长期从事ABAP开发工作的专业人士,在面对行业新趋势时,往往需要重新审视自己的发展方向。本文探讨了几位资深专家对ABAP未来走向的看法,以及开发者应如何调整技能以适应新的技术环境。 ... [详细]
  • Beetl是一款先进的Java模板引擎,以其丰富的功能、直观的语法、卓越的性能和易于维护的特点著称。它不仅适用于高响应需求的大型网站,也适合功能复杂的CMS管理系统,提供了一种全新的模板开发体验。 ... [详细]
  • publicclassBindActionextendsActionSupport{privateStringproString;privateStringcitString; ... [详细]
  • LeetCode 204: 计算质数
    本题要求计算小于给定非负整数n的所有质数的数量。感谢@mithmatt为此问题提供测试案例。 ... [详细]
  • Irish budget airline Ryanair announced plans to significantly increase its route network from Frankfurt Airport, marking a direct challenge to Lufthansa, Germany's leading carrier. ... [详细]
author-avatar
包括萨u盾根本_173
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有