热门标签 | 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语句



推荐阅读
  • MySQL初级篇——字符串、日期时间、流程控制函数的相关应用
    文章目录:1.字符串函数2.日期时间函数2.1获取日期时间2.2日期与时间戳的转换2.3获取年月日、时分秒、星期数、天数等函数2.4时间和秒钟的转换2. ... [详细]
  • 本文介绍了如何在 Spring Boot 项目中使用 spring-boot-starter-quartz 组件实现定时任务,并将 cron 表达式存储在数据库中,以便动态调整任务执行频率。 ... [详细]
  • 兆芯X86 CPU架构的演进与现状(国产CPU系列)
    本文详细介绍了兆芯X86 CPU架构的发展历程,从公司成立背景到关键技术授权,再到具体芯片架构的演进,全面解析了兆芯在国产CPU领域的贡献与挑战。 ... [详细]
  • 本文介绍了如何利用 `matplotlib` 库中的 `FuncAnimation` 类将 Python 中的动态图像保存为视频文件。通过详细解释 `FuncAnimation` 类的参数和方法,文章提供了多种实用技巧,帮助用户高效地生成高质量的动态图像视频。此外,还探讨了不同视频编码器的选择及其对输出文件质量的影响,为读者提供了全面的技术指导。 ... [详细]
  • 优化后的标题:深入探讨网关安全:将微服务升级为OAuth2资源服务器的最佳实践
    本文深入探讨了如何将微服务升级为OAuth2资源服务器,以订单服务为例,详细介绍了在POM文件中添加 `spring-cloud-starter-oauth2` 依赖,并配置Spring Security以实现对微服务的保护。通过这一过程,不仅增强了系统的安全性,还提高了资源访问的可控性和灵活性。文章还讨论了最佳实践,包括如何配置OAuth2客户端和资源服务器,以及如何处理常见的安全问题和错误。 ... [详细]
  • 当使用 `new` 表达式(即通过 `new` 动态创建对象)时,会发生两件事:首先,内存被分配用于存储新对象;其次,该对象的构造函数被调用以初始化对象。为了确保资源管理的一致性和避免内存泄漏,建议在使用 `new` 和 `delete` 时保持形式一致。例如,如果使用 `new[]` 分配数组,则应使用 `delete[]` 来释放内存;同样,如果使用 `new` 分配单个对象,则应使用 `delete` 来释放内存。这种一致性有助于防止常见的编程错误,提高代码的健壮性和可维护性。 ... [详细]
  • 本文介绍了如何在Python中使用插值方法将不同分辨率的数据统一到相同的分辨率。 ... [详细]
  • 【妙】bug称它为数组越界的妙用
    1、聊一聊首先跟大家推荐一首非常温柔的歌曲,跑步的常听。本文主要把自己对C语言中柔性数组、零数组等等的理解分享给大家,并聊聊如何构建一种统一化的学习思想 ... [详细]
  • Spring – Bean Life Cycle
    Spring – Bean Life Cycle ... [详细]
  • 本文详细介绍了在 CentOS 7 系统中配置 fstab 文件以实现开机自动挂载 NFS 共享目录的方法,并解决了常见的配置失败问题。 ... [详细]
  • 本文介绍了几种常用的图像相似度对比方法,包括直方图方法、图像模板匹配、PSNR峰值信噪比、SSIM结构相似性和感知哈希算法。每种方法都有其优缺点,适用于不同的应用场景。 ... [详细]
  • 应用链时代,详解 Avalanche 与 Cosmos 的差异 ... [详细]
  • 本文探讨了C语言和C++中大小写的处理方式,并详细介绍了如何在C++中实现不区分大小写的字符串比较。通过自定义`char_traits`类,可以灵活地处理字符的比较、复制和转换。 ... [详细]
  • poj 3352 Road Construction ... [详细]
  • 命令模式是一种行为设计模式,它将请求封装成一个独立的对象,从而允许你参数化不同的请求、队列请求或者记录请求日志。本文将详细介绍命令模式的基本概念、组件及其在实际场景中的应用。 ... [详细]
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社区 版权所有