《Beginning C++17》-学习笔记-Chapter 04-Making Decisions

《Beginning C++17》-学习笔记-Chapter 01-Basic Ideas 书名:Beginning C++17 - From Novice to Professional 作者:Ivor Horton, Peter Van Weert 笔记: Blocks can be placed inside other blocks—this concept is called nesting. Blocks can be nested, one within anothe... 阅读详情

There are only two possible bool values, true and false. True and false are keywords and are literals of type bool. They are
sometimes called Boolean literals.

#include <iostream>

int main()
{

	bool isValid{ true }; // Define and initialize a logical variable to true.

	bool isFalse{}; // Define and initialize a logical variable to false.

	std::cout << isValid << std::endl;//输出1
	std::cout << std::boolalpha;//This statement makes bool values output as true and false
	std::cout << isFalse << std::endl;//输出false
	std::cout << std::noboolalpha;// This statement returns output of bool values to the default setting, which is o or 1
	std::cout << (4<5) << std::endl;//输出1

}

If you cast true to an integer type, the result will be 1; casting false to an integer results in 0. Conversely, you can also convert numerical values to type bool. Zero converts to false, and any nonzero value converts to true. When you have a numerical value where a bool value is expected, the compiler will insert an implicit conversion to convert the numerical value to type bool.


#include <iostream>
#include <cctype>  /*functions in this header return values of type int. The value will be nonzero (true) if the character is of the type being tested for, and 0 (false) if it isn’t. The reason they don’t return a bool value is that they originate from the C Standard Library and predate type bool in C++.*/

int main()
{
	char letter{}; // Store input here
	std::cout << "Enter a letter: "; // Prompt for the input
	std::cin >> letter;

	if (std::isupper(letter))
	{
		std::cout << "You entered an uppercase letter." << std::endl;
		std::cout << "Convert it to uppercase as:" << static_cast<char>(std::tolower(letter));/* The result of function "tolower()" will be returned as type int, so you need to explicitly cast it if you want to store it as type char*/
		return 0;
	}

	else
	{
		std::cout << "You entered a lowercase letter." << std::endl;
		std::cout <<"Convert it to uppercase as:" << static_cast<char>(std::toupper(letter));
		return 0;
	}
}

An else always belongs to the nearest preceding if that’s not already spoken for by another else.

When an else block is another if, writing else if on one line is an accepted convention.


The binary logical operators are so-called short-circuit operators.

If the first operand to a binary logical expression already determines the outcome, the compiler will make sure no time is wasted evaluating the second operand. This property of the logical operators && and || is called short-circuit evaluation.

The second operand of && is evaluated only after the first operand evaluates to true, and the second operand of || only after the first evaluates to false.


// Using the conditional operator to select output.
#include  <iostream>

int main()
{
	int apple{};               // 苹果数量

	std::cout << "How many apple(s) do you have? ";
	std::cin >> apple;

	std::cout << "I have " << apple
		<< (((apple == 1 ) || (apple ==0))? " apple" : " apples")
		<< " in total." << std::endl;
}

You can only switch on values of integral (int, long, unsigned short, etc.), character (char, etc.), and enumeration types.

Each case value must be a constant expression, which is an expression that the compiler can evaluate at compile time.
 

// Using the switch statement
#include <iostream>
#include <cctype>		// for std::isalpha() and tolower()

int main()
{
	char letter{};
	std::cout << "Enter a letter: ";
	std::cin >> letter;

	if (std::isalpha(letter))//检查输入的字符是否是字母
	{
		switch (std::tolower(letter))
		{
		case 'a': 
			std::cout << "You entered vowel 'a'." << std::endl;
			[[fallthrough]];/*can add a [[fallthrough]] statement in the same place where you would otherwise add a break statement to prevent compiler to issue a warning when compiling your program. This feature is available from C++ 17.*/
		case 'e': case 'i': case 'o': case 'u':
			std::cout << "You entered a vowel." << std::endl;
			break;/*This statement will break out of the if-else and exit the program. A break statement is not the only way to move control out of a switch statement. If the code following a case label contains a return statement, control instantly exits not only the switch statement but the surrounding function as well. */
		default:
			std::cout << "You entered a consonant." << std::endl;
			break;
		}
	}
	else
	{
		std::cout << "You did not enter a letter." << std::endl;
	}
}

Selecting Language Standard
To enable the latest features of the C++ language outlined in this book it is necessary to manually change the language standard setting for your project. You can do this by first going to Project ➤ Properties to bring up the Property pages. From there, navigate to Configuration Properties ➤ C/C++ ➤ Language ➤ C++ Language Standard. Select the ISO C++17 standard from the drop-down list. Click OK and the project will now be configured to compile according to the C++17 language standard.

 


Any variable declared within a block ceases to exist at the end of the block, so you cannot reference it outside the block

In general, it is considered good coding style to limit the scope of variables to the region in which they are used.

#include <iostream>

int main()
{
	int input;
	std::cout << "Enter a number" << std::endl;
	std::cin >> input;
	if (int local_value{input}; local_value > 0) /*This is a new way, beginnig with C++ 17, of initializing a variable within the scope of an if-else.*/
	{
		std::cout << "value entered is larger than 0." << std::endl;
	}
	else
	{
		std::cout << "value entered is not larger than 0." << std::endl;

	}

}

 

/*Even though starting with C++14, C++ supports binary integral literals (of form 0b11001010), C++ standard output functions and streams do not support outputting integral values in binary format. They mostly do support hexadecimal and octal formatting—for std::cout, for instance, you can use the std::hex and std::oct output manipulators defined in <ios>. But to output a character in binary format, you’ll thus have to write some code yourself. A char normally has only eight bits, remember? You can just stream these bits one by one.*/

#include <iostream>

int main() {
	char ch{};
	std::cout << "Enter a letter: ";
	std::cin >> ch;

	// Output the character code as binary
	std::cout << "The binary code for '" << ch << "' is "
		<< ((ch & 0b10000000) ? 1 : 0)
		<< ((ch & 0b01000000) ? 1 : 0)
		<< ((ch & 0b00100000) ? 1 : 0)
		<< ((ch & 0b00010000) ? 1 : 0)
		<< ((ch & 0b00001000) ? 1 : 0)
		<< ((ch & 0b00000100) ? 1 : 0)
		<< ((ch & 0b00000010) ? 1 : 0)
		<< ((ch & 0b00000001) ? 1 : 0)
		<< std::endl;

	return 0;
}

 

Beginning C++17_From Novice to Professional-Apress(2018).pdf Welcome to Beginning C++17. This is a revised and updated version of Ivor Horton’s original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise. We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, you’ll be learning a language that is already used by millions and that provides the capability for application development in just about any context. C++ is very powerful. Arguably, it’s more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then you’ll never have to look for another pocketknife again. C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. In essence, we have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade in particular has seen the ris 立即下载

相关推荐

Beginning C++17, 5th Edition

c++17 教材, Beginning C++17, 5th Edition Beginning C++17, 5th Edition

基于Ubuntu22.04在ROS2 Humble中编译turtlebot3遇到的坑

在branch humble上的TurtleBot3的源码在ubuntu22.04中的ROS2 humble中编译会报一些错误,此文是本人亲自趟过的雷,仅供大家参考。希望对大家有帮助

wycheny2011的博客 3860

Beginning C++17 From Novice to Professional, 5th edition.zip

Learn how to program using the updated C++17 language. You'll start with the basics and progress through step-by-step examples to become a working C++ programmer. All you need are Beginning C++17 and any recent C++ compiler and you'll soon be writing real C++ programs. There is no assumption of prior programming knowledge

C++17入门经典

C++17入门经典第1章 基本概念基本数据类型处理基本数据类型决策数组和循环指针和引用操作字符串定义函数函数模板程序文件和预处理指令 第1章 基本概念 基本数据类型 处理基本数据类型 决策 数组和循环 指针和引用 操作字符串 定义函数 函数模板 程序文件和预处理指令 ...

CC977的博客 5362

Filipek B. C++17 In Detail 2019.pdf

If you’ve ever asked “what’s in C++17 and what does it mean for me and my code?” — and I hope you have — then this book is for you. Now that the C++ standard is being released regularly every three years, one of the challenges we have as a community is learning and absorbing the new features that are being regularly added to the standard language and library. That means not only knowing what those features are, but also how to use them effectively to solve problems. Bartlomiej Filipek does a great job of this by not just listing the features, but explaining each of them with examples, including a whole Part 3 of the book about how to apply key new C++17 features to modernize and improve existing code — everything from upgrading enable_if to the new if constexpr, to refactoring code by applying the new optional and variant vocabulary types, to writing parallel code using the new standard parallel algorithms. In each case, the result is cleaner code that’s often also significantly faster too. The point of new features isn’t just to know about them for their own sake, but to know about how they can let us express our intent more clearly and directly than ever in our C++ code. That ability to directly “say what we mean” to express our intent, or to express “what” we want to achieve rather than sometimes-tortuous details of “how” to achieve it through indirect mechanisms, is the primary thing that determines how clean and writable and readable — and correct — our code will be. For C++ programmers working on real-world projects using reasonably up-to-date C++ compilers, C++17 is where it’s at in the industry today for writing robust production code. Knowing what’s in C++17 and how to use it well is an important tool that will elevate your day-to-day coding, and more likely than not reduce your day-to-day maintenance and debugging chores. If you’re one of the many who have enjoyed Barteks’s blog (bfilipek.com, frequently cited at isocpp.org), you’ll certainly also enjoy this entertaining and informative book. And if you haven’t enjoyed his blog yet, you should check it out too… and then enjoy the book.

Beginning C++17 From Novice to Professional(5th) epub

Beginning C++17 From Novice to Professional(5th) 英文epub 第5版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

《Beginning C++17》-学习笔记-Chapter 06-Pointers and References

#include &lt;iostream&gt; int main() { long* pnumber1{};//the statement initializes pnumber1 with the pointer equivalent of zero, which is a special address that doesn't point to anything. long* p...

CodingIsFun 395

C++入门经典.pdf

这是学习c++的很好的资料,分享给大家。

c语言中两个条件or,逻辑和条件与,或C之间的区别是什么?

本问题已经有最佳答案,请猛点这里访问。Possible Duplicate:What is the diffference between the | and || or operators?逻辑与与或:(x & y)(x | y)有条件的和或:(x && y)(x || y)到目前为止,我只知道条件操作数。我知道它做什么,以及如何在if语句中应用它。但是逻辑操作数的目的是...

weixin_39657825的博客 1376

Beginning C++17: From Novice to Profession

Beginning C++17: From Novice to Professional, 5th edition English | ISBN: 1484233654 | 2018 | PDF+EPUB This latest edition has been fully updated to the latest version of the language, C++17, and to all conventions and best practices of so-called modern C++. Beginning C++17 also introduces the elements of the C++ Standard Library that provide essential support for the C++17 language.

Beginning C++17 C++编程入门英文原版

Beginning C++17 C++编程入门英文原版。电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍。

使用c++17std库varaint替代varaint开源库报错处理

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考例如:不知道后面会不会有人使用更优的方法。

晴山の博客 837

UE5新手必看:如何在UMG控件中嵌入浏览器实现Web页面展示(附完整步骤)

本文为UE5新手提供了在UMG控件中无缝集成Web浏览器的完整实战指南。详细介绍了从启用WebBrowser插件、创建并配置WebBrowser控件蓝图,到编写动态加载与交互逻辑,以及在游戏关卡中实例化UI的全过程。文章还分享了性能优化技巧与常见问题排查方法,帮助开发者高效实现游戏UI内的Web页面展示与交互。

weixin_29243063的博客 710

Beginning C++17, 5th Edition 免积分下载

图书说明: 了解如何使用更新的C ++ 17语言进行编程。您将从基础知识入手,逐步学习如何成为一名正在运行的C ++程序员。您所需要的只是Beginning C ++ 17和任何最新的C ++编译器,您很快就会编写真正的C ++程序。没有先前编程知识的假设。 本书中解释的所有语言概念都...

chongyuwan4121的博客 442

Matlab_Marr小波尺度变换图

多尺度的连续小波变换分解后时间尺度图 墨西哥Marr小波函数: %delta 小波变换尺度 %N     小波函数的长度 %s     原始信号 %g     原始信号某个尺度下的小波变化系数 function g=Singularity_Detection(delta,N,s); %保持信号长度 n=length(s); % 构造墨西哥小帽小波函数 for index_x=1

桔色小西瓜 6601

详解各类以太网标准10BASE-T/100BASE-T4/100BASE-FX/1000BASE-X等

以太网是现实世界中最普遍的一种计算机网络。以太网有两类:第一类是经典以太网,第二类是交换式以太网,使用了一种称为交换机的设备连接不同的计算机。经典以太网是以太网的原始形式,运行速度从3-10 Mbps不等;而交换式以太网正是广泛应用的以太网,可运行在100Mbps、1000Mbps和10000Mbps高速率,分别以快速以太网、千兆以太网和万兆以太网的形式呈现

不积跬步,无以至千里! 5万+

C++ 17 标准正式发布:终于能更简单地编写和维护代码了!

点击上方“优快云”,选择“置顶公众号” 关键时刻,第一时间送达! 何为 C++ 17? C++17 是继 C++14 之后,C++ 编程语言 ISO/IEC 标准的下一次修订的非正式名称。而就在昨日,ISO C++ 委员会正式发布了 C++ 17 标准,官方名称为 ISO/IEC 14882:2017。 C++ 17 标准化图表 C ++ 17 主要

优快云资讯 7万+

PID温控实验平台搭建(二)——PID进阶知识介绍及源码分享

最近,我突发奇想去翻阅了一些我本科期间所做的一些小项目,发现都挺有意思的!希望通过我的讲解文章可以帮助你们少走一些弯路!今天,我要分享的是一个PID温控实验平台的搭建,这是一个系列文章,手把手地教你学习和认识PID算法,了解这种算法在温度控制中的应用。由于我知识有限,如果出现一些错误,希望大家可以帮助我指出来,我们一起学习进步!!!

qq_35953617的博客 8049
上一篇: 《Beginning C++17》-学习笔记-Chapter 03-Working with Fundamental Data Types
下一篇: 《The Passionate Programmer》书摘
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值