当前位置:网站首页>C programming language (2nd Edition) -- Reading Notes -- 1.3
C programming language (2nd Edition) -- Reading Notes -- 1.3
2022-07-27 11:28:00 【YovaVan】
for sentence
A specific task can be programmed in many ways .
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
main()
{
float fahr, celsius;
float lower, upper, step;
lower = 0; /* lower limit of temperatuire scale */
upper = 300; /* upper limit */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}The following is another program to convert Fahrenheit temperature to Celsius temperature .
#include <stdio.h>
main ()
{
/* Print Fahrenheit temperature - Centigrade temperature */
int fahr;
for(fahr = 0; fahr<=300;fahr = fahr + 20)
printf("%3d %6.1f\n" fahr, (5.0 / 9.0)*(fahr - 32));
}Improvements :
1. Remove most variables , Just one int Type variable fahr;
2.for The lower temperature limit in the statement 、 Both the upper limit and the step size are constants ;
3. The expression for calculating the Celsius temperature becomes printf The third argument to the function , It is no longer a separate assignment statement .
The last point of improvement is C A language General rules Example : In any situation where a certain type of variable is allowed , More complex expressions of this type can be used . because printf The third argument to the function must be the same as %6.1f Matching floating point values , So you can use any floating-point expression here .
for A statement is a circular statement , It's right while Generalization of sentences . take for Statements and while Statements are , Can be found for Statement operation is more intuitive . Parentheses contain 3 part , The parts are separated by semicolons . a :
fahr = 0;Is the initialization part , Execute only once before entering the loop . Two :
fahr <=300;It is the test or condition part of the control cycle . The loop control will evaluate this condition , If the result value is true (true), Execution loop body . After that, it will be implemented Three parts :
fahr = fahr + 20To loop the variable fahr Add one step , And evaluate the condition again . If the calculated value is false (false), The loop will terminate execution .
And while The sentence is the same ,for The body of a loop can be a statement , It can also be a group of statements locked by curly braces .
Initialization part ( a )、 Condition part ( Two ) And increase the step length ( Third ) Can be any expression .
In actual programming , Optional while And for Any circular statement in , It mainly depends on which one is clearer .for Statement is more suitable for the situation that initialization and increasing step size are both single statements and logically related , Because it puts the loop control statements together , And while More compact .
practice 1-5 Modify the procedure in the above section , In reverse order ( from 300 To 0 degree ) Print the temperature conversion table .
边栏推荐
- Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
- 记忆化搜索 AcWing 901. 滑雪
- 什么是私域流量?
- 解决 ImportError: cannot import name 'abs' 导入tensorflow报错
- tensorflow运行报错解决方法
- Tree DP acwing 285. dance without boss
- 区间问题 AcWing 906. 区间分组
- Win10 vscode code code format setting and remote breakpoint debugging
- IO stream_ Character stream, IO stream summary, IO stream case summary
- (10) File contains
猜你喜欢

Find the combination number acwing 887. find the combination number III

状态压缩DP AcWing 91. 最短Hamilton路径

Basic use of cmake

Longest ascending subsequence model acwing 1014. mountaineering

最长上升子序列模型 AcWing 1012. 友好城市

最长上升子序列模型 AcWing 272. 最长公共上升子序列

Redis+caffeine two-level cache enables smooth access speed

How to build a real-time development platform to deeply release the value of enterprise real-time data?

背包问题 AcWing 9. 分组背包问题

最长上升子序列模型 AcWing 1010. 拦截导弹
随机推荐
背包模型 AcWing 423. 采药
Analysis of distributed database and cache double write consistency scheme (Reprint)
最长上升子序列模型 AcWing 1010. 拦截导弹
FAQs of "relay chain" and "dot" in Poka ecosystem
ACM warm-up Exercise 1 in 2022 summer vacation (summary)
数字三角形模型 AcWing 1018. 最低通行费
Find the combination number acwing 887. find the combination number III
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
IO stream_ Overview and explanation of data input and output flow
Smart pointer (shared_ptr, unique_ptr, weak_ptr)
The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
Inclusion exclusion principle acwing 890. divisible numbers
Opengauss kernel analysis - statistics and row count estimation
Yiwen counts NFT projects valued at more than US $100million
博弈论 AcWing 894. 拆分-Nim游戏
01 BTC cryptology principle
ethereum rpc
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
15th largest value of data flow
Luogu p1896 non aggression