Learncpp___CH1

2023-11-06

“You have to write a program once to know how you should have written it the first time.”

Short answer: You don’t. C++ is one part using what you know, and two parts looking up how to do the rest.

1.2 — Comments

Proper use of comments

Best practice

Comment your code liberally, and write your comments as if speaking to someone who has no idea what the code does. Don’t assume you’ll remember why you made specific choices.

Commenting out code

Converting one or more lines of code into a comment is called commenting out your code. This provides a convenient way to (temporarily) exclude parts of your code from being included in your compiled program.

Tip

If you always use single line comments for your normal comments, then you can always use multi-line comments to comment out your code without conflict. If you use multi-line comments to document your code, then commenting-out code using comments can become more challenging.

If you do need to comment out a code block that contains multi-line comments, you can also consider using the #if 0 preprocessor directive, which we discuss in lesson 2.10 – Introduction to the preprocessor.

Summary

  • At the library, program, or function level, use comments to describe what.
  • Inside the library, program, or function, use comments to describe how.
  • At the statement level, use comments to describe why.

1.3 — Introduction to objects and variables

1.7 — Keywords and naming identifiers

Identifier names that start with a capital letter are typically used for user-defined types.

1.8 — Whitespace and basic formatting

Whitespace is a term that refers to characters that are used for formatting purposes. In C++, this refers primarily to spaces, tabs, and newlines. The C++ compiler generally ignores whitespace, with a few minor exceptions (when processing text literals). For this reason, we say that C++ is a whitespace-independent language.

1.10 — Introduction to expressions

Statements are used when we want the program to perform an action.
Expressions are used when we want the program to calculate a value.

1.11 — Developing your first program

Best practice

New programmers often try to write an entire program all at once, and then get overwhelmed when it produces a lot of errors. A better strategy is to add one piece at a time, make sure it compiles, and test it. Then when you’re sure it’s working, move on to the next piece.

The preferred solution

#include <iostream>

// preferred version
int main()
{
	std::cout << "Enter an integer: ";

	int num{ };
	std::cin >> num;

	std::cout << "Double that number is: " <<  num * 2 << '\n'; // use an expression to multiply num * 2 at the point where we are going to print it

	return 0;
}

This is the preferred solution of the bunch. When std::cout executes, the expression num * 2 will get evaluated, and the result will be double num‘s value. That value will get printed. The value in num itself will not be altered, so we can use it again later if we wish.

This version is our reference solution.

Author’s note

One more thing: You may be thinking, “C++ has so many rules and concepts. How do I remember all of this stuff?”.

Short answer: You don’t. C++ is one part using what you know, and two parts looking up how to do the rest.

As you read through this site for the first time, focus less on memorizing specifics, and more on understanding what’s possible. Then, when you have a need to implement something in a program you’re writing, you can come back here (or to a reference site) and refresh yourself on how to do so.

1.x — Chapter 1 summary and quiz

statement
function
syntax
Comments
Data
value
identifier
definition statement
instantiated
data type
integer
Copy assignment
Initialization
std::cout
std::endl
uninitialized variable
undefined behavior
keywords
literal constant
operation operands operator
Unary Binary Ternary
expression evaluation result
expression statement

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Learncpp___CH1 的相关文章

随机推荐

  • 华为机考笔记之字符串以水仙花规则拆分

    题目 一 输入一组字符串 求字符串分组后 每组的和为水仙花数 注 水仙花数是一个三位数 其个位 十位 百位的立次方 的和等于自身 如 371 3 3 7 3 1 1 1 如果无法找到该分组 返回0 2 找到该分组 切分组不唯一 返回 1 3
  • js 每隔 10 秒钟 运行一次,发送一个 ajax 请求

    每隔 10 秒钟 运行一次 发送一个 ajax 请求 function runEvery10Sec 1000 10 10 秒钟 setTimeout runEvery10Sec 1000 10 ajax dataType json type
  • jQuery取得select选中的值

    jQuery取得select选中的值 本来以为jQuery select1 val 是取得选中的值 那么jQuery select1 text 就是取得的文本 这是不正确的 正确做法是 jQuery select1 option selec
  • Qt中的单例模式:实现一个单例的界面类

    文章目录 前言 一 什么是单例模式 二 单例模式的优缺点及使用场景 三 Qt中单例类的创建 四 单例类的使用 测试 总结 前言 本文主要讲述了使用加锁的懒汉式来实现单例 文中示例将一个界面类修改为单例类 并在主界面获取多次该类的实例来进行测
  • 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数

    统计一个字符串中大写字母字符 小写字母字符 数字字符出现的次数 不考虑其他字符 1 需求 统计一个字符串中大写字母字符 小写字母字符 数字字符出现的次数 不考虑其他字符 举例 Hello123World 结果 大写字符 2个 小写字符 8个
  • IDEA中常用的插件

    目录 Free MyBatis plugin codehelper generator grep console Translation Alibaba Java Coding Guidelines CamelCase GenerateAl
  • unordered_map的哈希HASH重载——举例unordered_map与pair联合使用

    有些时候 为了图省力 我们没准会这样的调用一个函数 unordered map lt pair
  • 分子模拟—Ovito渲染案例教程

    heartsuit spadesuit 分子模拟 Ovito渲染案例教程 heartsuit
  • 利用Nodejs 构建 WEB服务器

    前言 Web 服务器一般指网站服务器 是指驻留于因特网上某种类型计算机的程序 可以 向浏览器等 Web 客户端提供文档 也可以放置网站文件 让全世界浏览 可以放置数据文件 让全世界下载 目前最主流的三个 Web 服务器是 Apache Ng
  • 运算放大器芯片输出扩流电路三例

    工作原理 图1所示为三种集成运算放大器输出电流扩展电路 图 a 为双极性扩展电路 图 b 图 c 为单极性扩展电路 在图1 a 所示电路中 当输出电压为正时 BG1管工作 BG2管截止 输出电压为负时 BG1管截止 BC2管工作 二极管D1
  • C、C++、C#、python、java编程—文件读取

    C资料 菜鸟教程 C语言中文网 C community C 资料 菜鸟教程 cplusplus C community C 资料 菜鸟教程 microsoftC 文档 python资料 菜鸟教程 python标准库 Java资料 菜鸟教程
  • Multisim14.0安装教程

    2 安装步骤 解压 打开 Multisim14 0 鼠标右击 NI Circuit Design Suite 14 0 exe 选择 以管理员身份运行 点击确定 选择文件的解压路径 最好不解压在C盘 安装完成删掉即可 然后点击 Unzip
  • 华为OD机试2023(JS,C++,JAVA,PYTHON)-服务器能耗统计

    本篇题解 服务器耗能 题目描述 服务器有三种运行状态 空载 单任务 多任务 每个 时间片 的能耗的分别为 1 1 1 3 3 3 4 4 4 每个任务由起始时间片和结束时间片定义运行时间 如果一个时间片只有一个任务需要执行 则服务器处于单任
  • 微信小程序的常见的面试题(总结)

    1 微信小程序有几个文件 WXML WeiXin Markup Language 是框架设计的一套标签语言 结合基础组件 事件系统 可以构建出页面的结构 内部主要是微信自己定义的一套组件 WXSS WeiXin Style Sheets 是
  • Qt实现IP输入框(模仿Windows系统中的IP输入框)

    本文章所用的代码整理自Qt实现IP输入框 qt中ip地址输入框 GreenArrowMan的博客 CSDN博客 感谢原作者分享 本代码在上述作者代码基础上做了如下修改 1 屏蔽中文输入法 2 修复原作者代码中输入框四周的黑色边线无法正常显示
  • 【安装问题】python安装weditor出现报错总结

    python安装weditor出现报错总结 问题描述 主要原因是公司使用的python2版本过老 由于不能随意升级python版本 只能在python2的基础上解决办法 有很多地方需要更新 总结命令如下 基本上试过一遍 问题就可以解决了 解
  • 深度学习源码小项目汇总-代码全

    demo仓库和视频演示 到此一游7758258的个人空间 哔哩哔哩 bilibili 卷积网路CNN分类的模型一般使用包括alexnet DenseNet DLA GoogleNet Mobilenet ResNet ResNeXt Shu
  • 财报解读:毛利持续改善,金山云正在“弯道超车”?

    一季度 云巨头们的表现持续稳健 依旧稳坐前排 而作为中小云代表的金山云也在5月23日发布了2023年一季度财报 盈利能力持续改善成为通篇最亮眼的一笔 随着AI大模型打开了新的 潘多拉魔盒 云市场也在发生着巨变 但AI能否成为云厂商打开盈利大
  • JavaScript实现经典消方块游戏

    操作方式 在游戏区域中任意位置滑动手势 点击屏幕下方的按钮 键盘WASD和 都可以操作 游戏动作 操作 方块向左移动 左划 按下蓝色键 左一 A 方块向右移动 右划 按下橙色键 右一 D 强制方块下落 下划 按下粉色键 左二 S 改变方块方
  • Learncpp___CH1

    You have to write a program once to know how you should have written it the first time Short answer You don t C is one p