<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomáš Svojanovský</title>
    <description>The latest articles on DEV Community by Tomáš Svojanovský (@tick-to-trade).</description>
    <link>https://dev.to/tick-to-trade</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1156438%2F9d449ad8-f1de-434d-97f9-f3d8cc0f5026.jpg</url>
      <title>DEV Community: Tomáš Svojanovský</title>
      <link>https://dev.to/tick-to-trade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tick-to-trade"/>
    <language>en</language>
    <item>
      <title>Boolean Expressions, If Statements, and the Ternary Operator in C++</title>
      <dc:creator>Tomáš Svojanovský</dc:creator>
      <pubDate>Sun, 31 May 2026 06:00:00 +0000</pubDate>
      <link>https://dev.to/tick-to-trade/boolean-expressions-if-statements-and-the-ternary-operator-in-c-5e0a</link>
      <guid>https://dev.to/tick-to-trade/boolean-expressions-if-statements-and-the-ternary-operator-in-c-5e0a</guid>
      <description>&lt;p&gt;Boolean logic is one of the most basic but important parts of programming. In C++, boolean expressions are used whenever we need to make decisions in our code.&lt;/p&gt;

&lt;p&gt;If you already know another programming language, most of this will feel familiar. C++ uses the same basic ideas: true, false, comparison operators, if statements, else if, else, and the ternary operator.&lt;/p&gt;

&lt;p&gt;Let’s go through them step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boolean Values in C++
&lt;/h2&gt;

&lt;p&gt;C++ has a built-in boolean type called bool.&lt;/p&gt;

&lt;p&gt;A bool variable can only store one of two values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both true and false are keywords in C++, and they are written in lowercase.&lt;/p&gt;

&lt;p&gt;This is important. For example, this is correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isReady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is not valid C++:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isReady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// wrong&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Boolean Expressions
&lt;/h2&gt;

&lt;p&gt;A boolean expression is an expression that evaluates to either true or false.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, b1 == b2 checks whether b1 and b2 have the same value.&lt;/p&gt;

&lt;p&gt;Since b1 is true and b2 is false, the result is false.&lt;/p&gt;

&lt;p&gt;So c1 stores:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C++ supports the usual comparison operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;==&lt;/span&gt;  &lt;span class="c1"&gt;// equal to&lt;/span&gt;
&lt;span class="o"&gt;!=&lt;/span&gt;  &lt;span class="c1"&gt;// not equal to&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;   &lt;span class="c1"&gt;// less than&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;   &lt;span class="c1"&gt;// greater than&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;  &lt;span class="c1"&gt;// less than or equal to&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;  &lt;span class="c1"&gt;// greater than or equal to&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also combine boolean expressions using logical operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;  &lt;span class="c1"&gt;// and&lt;/span&gt;
&lt;span class="o"&gt;||&lt;/span&gt;  &lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;   &lt;span class="c1"&gt;// not&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true OR false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the result is true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Parentheses for Clarity
&lt;/h2&gt;

&lt;p&gt;In many programming languages, including C++, operator precedence determines which parts of an expression are evaluated first.&lt;/p&gt;

&lt;p&gt;For simple expressions, this is usually obvious. But once you combine multiple conditions, the code can become harder to read.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but it is not immediately clear what the programmer intended.&lt;/p&gt;

&lt;p&gt;A clearer version would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or maybe this was intended:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two expressions are different.&lt;/p&gt;

&lt;p&gt;That is why it is usually a good idea to add parentheses when combining conditions. The goal is not just to make the compiler understand the code, but also to make the code understandable for humans.&lt;/p&gt;

&lt;h2&gt;
  
  
  If Statements
&lt;/h2&gt;

&lt;p&gt;The most common place where boolean expressions are used is inside if statements.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Printing&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The condition inside the parentheses is evaluated.&lt;/p&gt;

&lt;p&gt;If the condition is true, the code inside the curly braces runs.&lt;/p&gt;

&lt;p&gt;If the condition is false, the code block is skipped.&lt;/p&gt;

&lt;p&gt;In this example, b1 == true is true, so the program prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Printing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shorter Boolean Checks
&lt;/h2&gt;

&lt;p&gt;When a variable is already a boolean, you do not need to compare it explicitly to true.&lt;/p&gt;

&lt;p&gt;Instead of writing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Printing&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Printing&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is shorter and more idiomatic.&lt;/p&gt;

&lt;p&gt;Both versions mean the same thing:&lt;/p&gt;

&lt;p&gt;if b1 is true, run this block&lt;/p&gt;

&lt;p&gt;Similarly, instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Not printing&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Not printing&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ! operator means “not”.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;not b1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If b1 is true, then !b1 is false.&lt;/p&gt;

&lt;p&gt;If b1 is false, then !b1 is true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Else If and Else
&lt;/h2&gt;

&lt;p&gt;C++ also supports else if and else.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b2 is false&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"No condition matched&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happens:&lt;/p&gt;

&lt;p&gt;First, C++ checks if (b1).&lt;br&gt;
If that is false, it checks else if (!b2).&lt;br&gt;
If that is also false, it runs the else block.&lt;/p&gt;

&lt;p&gt;In this example, b1 is false and b2 is also false.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;b2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is true.&lt;/p&gt;

&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b2 is false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Curly Braces in If Statements
&lt;/h2&gt;

&lt;p&gt;In C++, curly braces define the body of an if, else if, or else block.&lt;/p&gt;

&lt;p&gt;This is the recommended style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Technically, if the body contains only one statement, you can omit the curly braces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is valid C++.&lt;/p&gt;

&lt;p&gt;However, it can easily become dangerous when you add more lines later.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"This always runs&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the first line belongs to the if statement.&lt;/p&gt;

&lt;p&gt;The second line always runs, because it is not inside a block.&lt;/p&gt;

&lt;p&gt;That is why many developers prefer to always use curly braces, even for one-line if statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"This only runs if b1 is true&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This style is safer and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler Warnings and Unused Variables
&lt;/h2&gt;

&lt;p&gt;When writing code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you may see a compiler warning if c1 is never used.&lt;/p&gt;

&lt;p&gt;The warning may say something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unused variable 'c1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local variable is initialized but not referenced
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that you created a variable, stored a value in it, but never used it later.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If c1 is not used anywhere else, then the variable is unnecessary.&lt;/p&gt;

&lt;p&gt;Compiler warnings are useful because they help you detect code that may be redundant, wasteful, or incorrect.&lt;/p&gt;

&lt;p&gt;In modern C++ development, it is common to enable many compiler warnings. They help you catch problems early.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Ternary Operator
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to assign one value if a condition is true, and another value if the condition is false.&lt;/p&gt;

&lt;p&gt;You can do this with a normal if statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdint&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works perfectly fine.&lt;/p&gt;

&lt;p&gt;But C++ also gives us a shorter way to write this: the ternary operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;value_if_true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value_if_false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;/p&gt;

&lt;p&gt;if b1 is true, store 10&lt;br&gt;
otherwise, store -10&lt;/p&gt;

&lt;p&gt;If b1 is true, then result becomes 10.&lt;/p&gt;

&lt;p&gt;If b1 is false, then result becomes -10.&lt;/p&gt;
&lt;h2&gt;
  
  
  Ternary Operator vs If Statement
&lt;/h2&gt;

&lt;p&gt;The ternary operator is useful when you want to choose between two values.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is much shorter than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the ternary operator should not be overused.&lt;/p&gt;

&lt;p&gt;This is readable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is much harder to understand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b3&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nested ternary expressions can quickly become difficult to read.&lt;/p&gt;

&lt;p&gt;In that case, an if, else if, else structure is usually better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is longer, but it is much clearer.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should You Use the Ternary Operator?
&lt;/h2&gt;

&lt;p&gt;Use the ternary operator when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you are choosing between two simple values&lt;/li&gt;
&lt;li&gt;the condition is easy to read&lt;/li&gt;
&lt;li&gt;the whole expression stays short&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;isPositive&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Avoid the ternary operator when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you need multiple branches&lt;/li&gt;
&lt;li&gt;the expression becomes too long&lt;/li&gt;
&lt;li&gt;you are doing more than just selecting a value&lt;/li&gt;
&lt;li&gt;readability suffers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bad example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may compile, but it is not pleasant to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Boolean expressions are used to control the flow of a program.&lt;/p&gt;

&lt;p&gt;In C++, a bool can store either true or false. You can use comparison operators and logical operators to build conditions, and those conditions can be used inside if, else if, and else statements.&lt;/p&gt;

&lt;p&gt;The ternary operator is a shorter alternative to a simple if-else assignment. It is useful when you want to choose between two values, but it should be used carefully. If the expression becomes too complex, a normal if statement is usually better.&lt;/p&gt;

&lt;p&gt;In general:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// use this for control flow&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;value_if_true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value_if_false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the ternary operator mainly for simple value selection.&lt;/p&gt;

&lt;p&gt;Readable code is more important than short code.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Integers and Floating-Point Numbers in C++</title>
      <dc:creator>Tomáš Svojanovský</dc:creator>
      <pubDate>Sat, 30 May 2026 07:22:48 +0000</pubDate>
      <link>https://dev.to/tick-to-trade/integers-and-floating-point-numbers-in-c-3ll5</link>
      <guid>https://dev.to/tick-to-trade/integers-and-floating-point-numbers-in-c-3ll5</guid>
      <description>&lt;h2&gt;
  
  
  File Extensions in C++
&lt;/h2&gt;

&lt;p&gt;Before we start writing code, it is worth mentioning that C++ source files can use different file extensions.&lt;/p&gt;

&lt;p&gt;Common C++ file extensions include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cxx&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three are valid for C++ source files.&lt;/p&gt;

&lt;p&gt;In practice, .cpp and .cc are probably the most common ones. The .cxx extension is also valid, but you will usually see it less often.&lt;/p&gt;

&lt;p&gt;For this example, let’s imagine we create a file called:&lt;/p&gt;

&lt;p&gt;integers_floats.cpp&lt;/p&gt;

&lt;h2&gt;
  
  
  Integer Types in C++
&lt;/h2&gt;

&lt;p&gt;An integer is a whole number without a fractional part.&lt;/p&gt;

&lt;p&gt;Examples of integers are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
42
-10
100000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In C++, integers can be either:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;signed&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A signed integer can store both negative and positive values.&lt;/p&gt;

&lt;p&gt;An unsigned integer can store only zero and positive values.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;   &lt;span class="c1"&gt;// signed&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="c1"&gt;// signed or unsigned&lt;/span&gt;
&lt;span class="mi"&gt;42&lt;/span&gt;    &lt;span class="c1"&gt;// signed or unsigned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fixed-Width Integer Types
&lt;/h2&gt;

&lt;p&gt;C++ provides a standard library header called:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#include &amp;lt;cstdint&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This header gives us access to fixed-width integer types.&lt;/p&gt;

&lt;p&gt;These are useful because they allow us to specify exactly how many bits an integer should use.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::int32_t&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;p&gt;a signed integer with 32 bits&lt;/p&gt;

&lt;p&gt;Here is a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdint&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;i1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type std::int32_t stores a signed 32-bit integer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signed and Unsigned Integer Types
&lt;/h2&gt;

&lt;p&gt;The  header gives us several useful integer types.&lt;/p&gt;

&lt;p&gt;Signed integer types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int8_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unsigned integer types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint16_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number in the type name tells us how many bits the type uses.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int8_t&lt;/span&gt;   &lt;span class="c1"&gt;// signed 8-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt;  &lt;span class="c1"&gt;// signed 16-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;  &lt;span class="c1"&gt;// signed 32-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;  &lt;span class="c1"&gt;// signed 64-bit integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for unsigned integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;   &lt;span class="c1"&gt;// unsigned 8-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint16_t&lt;/span&gt;  &lt;span class="c1"&gt;// unsigned 16-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;  &lt;span class="c1"&gt;// unsigned 32-bit integer&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt;  &lt;span class="c1"&gt;// unsigned 64-bit integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A larger number of bits means the type can store a larger range of values.&lt;/p&gt;

&lt;p&gt;For example, a 64-bit integer can store much larger values than an 8-bit integer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Using Fixed-Width Integer Types
&lt;/h2&gt;

&lt;p&gt;Here is an example using several integer types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdint&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int8_t&lt;/span&gt; &lt;span class="n"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;i16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;i64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;u8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;u16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us a clear way to define the size and signedness of our integer variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Integer Literals and the U Suffix
&lt;/h2&gt;

&lt;p&gt;When writing unsigned integer values, you can add a U suffix to the number.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::uint32_t value = 42U;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The U tells the compiler that the literal should be treated as unsigned.&lt;/p&gt;

&lt;p&gt;You can use either lowercase or uppercase:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="mi"&gt;42u&lt;/span&gt;
&lt;span class="mi"&gt;42U&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are valid.&lt;/p&gt;

&lt;p&gt;Personally, I prefer the uppercase version because it is easier to see.&lt;/p&gt;

&lt;p&gt;For larger unsigned values, you may also see suffixes like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;42ULL&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This means an unsigned long long literal, which is usually used for larger integer values.&lt;/p&gt;

&lt;p&gt;For now, the important thing to remember is this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;U&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is commonly used to indicate that a number is unsigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Floating-Point Types in C++
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Integers are useful for whole numbers.&lt;/p&gt;

&lt;p&gt;But what if we need decimal values?&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="mf"&gt;3.14&lt;/span&gt;
&lt;span class="mf"&gt;12.5&lt;/span&gt;
&lt;span class="mf"&gt;0.001&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For these values, C++ provides floating-point types.&lt;/p&gt;

&lt;p&gt;The two most common floating-point types are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A float is usually a 32-bit floating-point number.&lt;/p&gt;

&lt;p&gt;A double is usually a 64-bit floating-point number.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;12.0&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;12.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Use F with Float Literals?
&lt;/h2&gt;

&lt;p&gt;In C++, a decimal literal such as this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;12.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is interpreted as a double by default.&lt;/p&gt;

&lt;p&gt;So if you write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;float f1 = 12.0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the value on the right side is first treated as a double, and then it is stored into a float.&lt;/p&gt;

&lt;p&gt;To make it clear that the literal itself should be a float, you can add the F suffix:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;float f1 = 12.0F;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can also write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;float f1 = 12.0f;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both lowercase f and uppercase F are valid.&lt;/p&gt;

&lt;p&gt;Again, I prefer uppercase because it is easier to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  float and double Compared to Other Languages
&lt;/h2&gt;

&lt;p&gt;Some modern languages use names such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;float32&lt;/span&gt;
&lt;span class="n"&gt;float64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, Rust has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;f32&lt;/span&gt;
&lt;span class="n"&gt;f64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C++ does not use these names directly for built-in floating-point types.&lt;/p&gt;

&lt;p&gt;Instead, C++ uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt;   &lt;span class="c1"&gt;// usually 32-bit&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;  &lt;span class="c1"&gt;// usually 64-bit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can define your own aliases if you want names like float32 or float64, but that is a topic for another article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boolean Values
&lt;/h2&gt;

&lt;p&gt;C++ also has a boolean type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A boolean can store one of two values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;is_finished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A boolean is not usually considered a numeric type in the same way as integers and floats, but it is still one of the fundamental primitive types in C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Characters in C++
&lt;/h2&gt;

&lt;p&gt;C++ also has a character type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;char&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A char stores a single character.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'z'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the single quotation marks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;
&lt;span class="sc"&gt;'z'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Single quotation marks are used for single characters.&lt;/p&gt;

&lt;p&gt;Double quotation marks are used for strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;    &lt;span class="c1"&gt;// character&lt;/span&gt;
&lt;span class="s"&gt;"A"&lt;/span&gt;    &lt;span class="c1"&gt;// string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This distinction is very important in C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Characters Are Integers Under the Hood
&lt;/h2&gt;

&lt;p&gt;Although we use char for characters, under the hood characters are represented as integer values.&lt;/p&gt;

&lt;p&gt;For example, the character:&lt;/p&gt;

&lt;p&gt;'A'&lt;/p&gt;

&lt;p&gt;is mapped to a numeric value using a character encoding table such as ASCII.&lt;/p&gt;

&lt;p&gt;In ASCII, characters like A, B, C, a, b, and c all correspond to numeric values.&lt;/p&gt;

&lt;p&gt;That means char is closely related to integer types internally.&lt;/p&gt;

&lt;p&gt;However, when storing single characters, we normally use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;char&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int8_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though they may be similar in size, char better communicates the intention: we are storing a character, not just a small number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Not Just Use int?
&lt;/h2&gt;

&lt;p&gt;In many C++ tutorials, you will often see code like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int value = 42;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This works, and int is still very commonly used.&lt;/p&gt;

&lt;p&gt;However, the exact size of int can depend on the platform and compiler.&lt;/p&gt;

&lt;p&gt;For example, on one system an int might be 32 bits. On another system, such as some embedded platforms, it might be 16 bits.&lt;/p&gt;

&lt;p&gt;That means when you write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int value = 42;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you do not always know exactly how many bits the type has.&lt;/p&gt;

&lt;p&gt;For modern C++ code, especially when the exact size matters, fixed-width integer types from  are often a better choice.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::int32_t value = 42;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This makes the intention much clearer.&lt;/p&gt;

&lt;p&gt;You are saying:&lt;/p&gt;

&lt;p&gt;I want a signed integer with exactly 32 bits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;C++ gives us several ways to store numeric values.&lt;/p&gt;

&lt;p&gt;For integers, we can use fixed-width types from the  header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int8_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint16_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For decimal values, we usually use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For boolean values, we use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And for single characters, we use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;char&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The key idea is that different types have different sizes, ranges, and meanings.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Latency: What It Really Means, How It’s Measured, and Why Every Field Sees It Differently</title>
      <dc:creator>Tomáš Svojanovský</dc:creator>
      <pubDate>Thu, 04 Dec 2025 16:27:04 +0000</pubDate>
      <link>https://dev.to/tick-to-trade/latency-what-it-really-means-how-its-measured-and-why-every-field-sees-it-differently-226h</link>
      <guid>https://dev.to/tick-to-trade/latency-what-it-really-means-how-its-measured-and-why-every-field-sees-it-differently-226h</guid>
      <description>&lt;p&gt;Latency is basically the delay between the moment you start a task and the moment it finishes. Every system has some latency — if it’s doing any work at all, it can never be truly zero. In some systems, the latency is so small that it’s just a fraction of a millisecond, and there even an extra microsecond of delay can be a big problem.&lt;/p&gt;

&lt;p&gt;Applications that must respond extremely quickly are called low-latency applications. For these, the goal is to respond and return results as fast as possible. If latency rises, the system can slow down, degrade in performance, or even completely break.&lt;/p&gt;

&lt;p&gt;But if it runs with the expected low latency, it can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;beat the competition&lt;/li&gt;
&lt;li&gt;run at maximum performance&lt;/li&gt;
&lt;li&gt;handle more work (higher throughput)&lt;/li&gt;
&lt;li&gt;increase productivity&lt;/li&gt;
&lt;li&gt;or improve the user experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending on what the application does.&lt;/p&gt;

&lt;p&gt;Latency is both a quantitative and a qualitative concept.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quantitatively, it’s just time&lt;/li&gt;
&lt;li&gt;Qualitatively, it depends on the context — some systems can tolerate latency measured in seconds (like loading a webpage), but once a video starts playing, it can’t afford multi-second pauses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That would kill the entire experience.&lt;/p&gt;

&lt;p&gt;And then there’s the extreme case: &lt;strong&gt;high-frequency trading (HFT)&lt;/strong&gt;. There, a few microseconds decide whether a company is profitable or irrelevant.&lt;/p&gt;

&lt;p&gt;It’s a world where people optimize the network, software, CPU, RAM, and even the physical cables — all to shave off microseconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Is Latency Actually Measured?
&lt;/h2&gt;

&lt;p&gt;You can measure latency in multiple ways. The difference is mainly where you start measuring and where you stop. Here are the most common metrics.&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Time To First Byte (TTFB)
&lt;/h3&gt;

&lt;p&gt;The time between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;T₁&lt;/strong&gt;: when the sender transmits the first byte&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T₂&lt;/strong&gt;: when the receiver reads that first byte&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Useful for networks, APIs, or streaming. It shows how quickly the server starts responding, not how fast it completes the whole response.&lt;/p&gt;

&lt;h3&gt;
  
  
  2) Round-Trip Time (RTT)
&lt;/h3&gt;

&lt;p&gt;The full “there and back” cycle:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Send → deliver → process → respond → deliver&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;RTT includes the time the other side spends processing the data. It’s the complete picture of real-world response time.&lt;/p&gt;

&lt;p&gt;In trading, RTT consists of three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;time for market data to reach the participant&lt;/li&gt;
&lt;li&gt;time for the system to make a decision&lt;/li&gt;
&lt;li&gt;time for the order to reach the exchange&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3) Tick-to-Trade (TTT)
&lt;/h3&gt;

&lt;p&gt;A special trading-specific metric.&lt;/p&gt;

&lt;p&gt;It measures only what happens inside your infrastructure:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How long from receiving a packet until you send an order back&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TTT = pure algorithm + infrastructure reaction time.&lt;/p&gt;

&lt;p&gt;In practice → when you receive market data, how fast can you send a trade?&lt;/p&gt;

&lt;p&gt;This is where microseconds matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  4) CPU Clock Cycles
&lt;/h3&gt;

&lt;p&gt;The lowest-level measurement — CPU cycles.&lt;/p&gt;

&lt;p&gt;Useful for analyzing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how fast a specific instruction executes&lt;/li&gt;
&lt;li&gt;pipeline behavior, branch mispredictions, cache hits/misses&lt;/li&gt;
&lt;li&gt;how different compilers generate assembly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is “high-end optimization” where you tune the last few percent of performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Latency vs Throughput: Don’t Mix Them Up
&lt;/h2&gt;

&lt;p&gt;People often confuse latency and throughput, but they’re entirely different.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Latency = how fast you finish one thing&lt;/li&gt;
&lt;li&gt;Throughput = how many things you finish per unit of time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want high throughput, you use parallelism — individual tasks might take longer but you complete more of them.&lt;/p&gt;

&lt;p&gt;If you want low latency, every single operation must be fast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Latency Metrics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mean latency
&lt;/h3&gt;

&lt;p&gt;Average of all measurements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Median latency
&lt;/h3&gt;

&lt;p&gt;Better representation of reality. Unaffected by extreme outliers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Peak latency
&lt;/h3&gt;

&lt;p&gt;Worst-case scenario. Crucial in real-time systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Latency variance (jitter)
&lt;/h2&gt;

&lt;p&gt;How much latency fluctuates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Become a member
&lt;/h3&gt;

&lt;p&gt;Extremely important in HFT, robotics, and automotive — you need predictability.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Do Latency-Sensitive Applications Need?
&lt;/h3&gt;

&lt;p&gt;Robustness and correctness&lt;br&gt;
Low-latency systems process huge amounts of data. They must be stable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Low mean and median latency
&lt;/h3&gt;

&lt;p&gt;Both need to stay minimal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Capped peak latency
&lt;/h3&gt;

&lt;p&gt;You must guarantee the upper bound — the system must never “fall apart” and spike to seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Low variance
&lt;/h3&gt;

&lt;p&gt;Even if average latency is low, occasional millisecond-level spikes are bad.&lt;/p&gt;

&lt;h3&gt;
  
  
  High throughput
&lt;/h3&gt;

&lt;p&gt;Sometimes handling a massive load is more important than the absolute fastest single operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example — Server vs Client
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwrmwqxmfbrtviqcdv0l8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwrmwqxmfbrtviqcdv0l8.png" alt="Client x Server" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  T₁ — server sends the first byte
&lt;/h3&gt;

&lt;p&gt;Server starts transmitting the packet.&lt;/p&gt;

&lt;h3&gt;
  
  
  T₂ — client receives the first byte
&lt;/h3&gt;

&lt;p&gt;Client gets the first data from the server.&lt;/p&gt;

&lt;p&gt;→ T₂ − T₁ = TTFB (server → client)&lt;/p&gt;

&lt;h3&gt;
  
  
  T₃ — client sends a response
&lt;/h3&gt;

&lt;p&gt;Client processes the data and sends a packet back.&lt;/p&gt;

&lt;h3&gt;
  
  
  T₄ — server receives the first byte of the response
&lt;/h3&gt;

&lt;p&gt;→ T₄ − T₃ = TTFB (client → server)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full RTT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want the entire round-trip time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RTT = (T₂ − T₁) + (T₄ − T₃)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In many cases, you add the client processing time (T₂ → T₃), so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-world RTT = T₄ − T₁&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The full cycle from sending to receiving.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Should You Care?
&lt;/h2&gt;

&lt;p&gt;Latency is not just a technical parameter.&lt;/p&gt;

&lt;p&gt;It’s a competitive advantage, a quality-of-experience factor, and in some fields (trading, automotive, robotics), it’s literally a matter of life and death.&lt;/p&gt;

&lt;p&gt;And here’s the fascinating part:&lt;/p&gt;

&lt;p&gt;Every field sees latency completely differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  How React Thinks About Latency
&lt;/h2&gt;

&lt;p&gt;In the React world, latency is not about microseconds.&lt;/p&gt;

&lt;p&gt;React doesn’t care if a function takes 80 ns or 120 ns.&lt;/p&gt;

&lt;p&gt;React focuses on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keeping the UI “responsive enough” for humans&lt;/li&gt;
&lt;li&gt;avoiding flicker, tearing, and unnecessary re-renders&lt;/li&gt;
&lt;li&gt;preventing users from feeling slowdowns&lt;/li&gt;
&lt;li&gt;scheduling work so the UI stays interactive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React concepts around latency:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent rendering&lt;/strong&gt; — pause work to keep the UI smooth&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transitions&lt;/strong&gt; — mark lower-priority updates&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React Compiler&lt;/strong&gt; — eliminate unnecessary re-renders&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, latency is measured in tens or hundreds of milliseconds, because humans don’t notice anything under ~16 ms.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;400 ms? Fine.&lt;/li&gt;
&lt;li&gt;1 second? Annoying.&lt;/li&gt;
&lt;li&gt;2 seconds? Now it’s a problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this world, latency is a psychological experience more than a technical one.&lt;/p&gt;

&lt;h2&gt;
  
  
  And Then There’s the Opposite Extreme: Trading, Robotics, Automotive
&lt;/h2&gt;

&lt;p&gt;These fields don’t care about “user experience.”&lt;/p&gt;

&lt;p&gt;A single microsecond can mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your algorithm loses to competitors&lt;/li&gt;
&lt;li&gt;a robot doesn’t react in time&lt;/li&gt;
&lt;li&gt;a car fails to compute a braking trajectory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React couldn’t care less if something takes 0.1 ms instead of 1 ms.&lt;/p&gt;

&lt;p&gt;Trading systems absolutely care.&lt;/p&gt;

&lt;p&gt;They’d lose money — and relevance — over a 0.001 ms difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  And There’s a “Third World” — Typical Backends and APIs
&lt;/h2&gt;

&lt;p&gt;Here, latency is measured in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;milliseconds&lt;/strong&gt; (e.g., 20–200 ms)&lt;/li&gt;
&lt;li&gt;the focus is throughput, scaling, caching, parallelization&lt;/li&gt;
&lt;li&gt;as long as requests finish within a few hundred ms, everything is fine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the kind of system where “latency” means something completely different than in HFT or real-time control.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Point
&lt;/h2&gt;

&lt;p&gt;Understanding latency is all about understanding context:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;React&lt;/strong&gt;, you optimize for UI smoothness and user perception&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;backend systems&lt;/strong&gt;, you optimize for scaling and throughput&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;robotics and trading&lt;/strong&gt;, you optimize for microsecond reaction times&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And yet all these fields use the exact same word: latency.&lt;/p&gt;

&lt;p&gt;Just in completely different universes.&lt;/p&gt;

</description>
      <category>latency</category>
      <category>cpp</category>
      <category>performance</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Why C++ Still Matters: A Modern Look for Today’s Developers</title>
      <dc:creator>Tomáš Svojanovský</dc:creator>
      <pubDate>Fri, 28 Nov 2025 20:21:24 +0000</pubDate>
      <link>https://dev.to/tick-to-trade/why-c-still-matters-a-modern-look-for-todays-developers-833</link>
      <guid>https://dev.to/tick-to-trade/why-c-still-matters-a-modern-look-for-todays-developers-833</guid>
      <description>&lt;p&gt;C++ has been around for decades, but it’s still one of the most important languages in the world.&lt;/p&gt;

&lt;p&gt;Whether you’re building games, trading systems, robotics applications, or high-performance backends, C++ continues to dominate the domains where speed, control, and reliability matter most.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. C++ Is Everywhere — More Than You Think
&lt;/h2&gt;

&lt;p&gt;C++ quietly powers a massive part of the software ecosystem.&lt;/p&gt;

&lt;p&gt;Game engines like Unreal are almost entirely written in C++ to render millions of polygons each second.&lt;/p&gt;

&lt;p&gt;Web browsers (Chrome) rely on C++ for JavaScript engines, layout engines, and rendering pipelines — the most performance-critical pieces.&lt;/p&gt;

&lt;p&gt;Graphics, physics, and audio pipelines across all platforms are built from C++ for maximum efficiency.&lt;/p&gt;

&lt;p&gt;Embedded devices, sometimes with only kilobytes of memory, depend on C++ to get fine-grained control with minimal overhead.&lt;/p&gt;

&lt;p&gt;If it needs to run fast or run everywhere, chances are it’s using C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Flexibility and Portability: C++ Runs on Anything
&lt;/h2&gt;

&lt;p&gt;One reason C++ survived decades of new languages is its range.&lt;br&gt;
The same language can target:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;massive cloud servers processing thousands of operations per second&lt;/li&gt;
&lt;li&gt;standard desktop computers&lt;/li&gt;
&lt;li&gt;tiny microcontrollers managing sensors&lt;/li&gt;
&lt;li&gt;flight computers in aircraft and spacecraft&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Very few languages can deliver both high-level abstractions and near-metal performance across this wide spectrum.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. From Pointers to Smart Pointers: Modern C++ Gives You a Choice
&lt;/h2&gt;

&lt;p&gt;C++ inherits raw low-level control from C: manual memory, pointers, direct hardware access.&lt;/p&gt;

&lt;p&gt;But modern C++ gives you safe, high-level tools too:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Smart pointers (unique_ptr, shared_ptr)&lt;/li&gt;
&lt;li&gt;RAII for automatic resource management&lt;/li&gt;
&lt;li&gt;Standard containers (vector, map, unordered_map)&lt;/li&gt;
&lt;li&gt;Robust type safety and templates&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can write C++ like a low-level system engineer or like a high-level application developer — the choice is yours.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The Power of Open Standardization
&lt;/h2&gt;

&lt;p&gt;C++ isn’t owned by a corporation.&lt;br&gt;
Its evolution is guided by ISO’s WG21 committee, made up of compiler authors, industry experts, and academics.&lt;/p&gt;

&lt;p&gt;The last decade transformed the language:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;C++11 introduced lambdas, auto, move semantics&lt;/li&gt;
&lt;li&gt;C++14/17 refined the modern style&lt;/li&gt;
&lt;li&gt;C++20 brought concepts, coroutines, and ranges&lt;/li&gt;
&lt;li&gt;C++23 continued improving usability and compile-time power&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Modern C++ feels nothing like the 90s version. It’s faster, safer, more expressive, and significantly more pleasant to write.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Quality-of-Life Features That Changed Everything
&lt;/h2&gt;

&lt;p&gt;New language features massively improved developer experience:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;auto reduces noise and improves readability&lt;/li&gt;
&lt;li&gt;Lambdas make callbacks, algorithms, and small functions easy&lt;/li&gt;
&lt;li&gt;Ranges bring fluent, Python-like data transformations with compile-time guarantees&lt;/li&gt;
&lt;li&gt;Smart pointers eliminate many memory bugs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Modern C++ lets you write expressive, high-level code without giving up control.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Why Certain Industries Still Choose C++
&lt;/h2&gt;

&lt;p&gt;C++ remains dominant in fields where performance is non-negotiable.&lt;/p&gt;

&lt;p&gt;AAA game engines rely on it for stable 60–120 FPS loops.&lt;/p&gt;

&lt;p&gt;High-frequency trading (HFT) uses C++ because microseconds literally mean money.&lt;/p&gt;

&lt;p&gt;Robotics needs deterministic performance and tight hardware control.&lt;/p&gt;

&lt;p&gt;Scientific computing depends on C++ for large simulations (climate models, epidemiology, physics).&lt;/p&gt;

&lt;p&gt;If every microsecond counts, C++ wins.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Yes — C++ Has Dangerous Parts. But Modern C++ Helps You Avoid Them
&lt;/h2&gt;

&lt;p&gt;With great power comes… undefined behavior.&lt;/p&gt;

&lt;p&gt;Classic C++ bugs include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;segmentation faults&lt;/li&gt;
&lt;li&gt;buffer overflows&lt;/li&gt;
&lt;li&gt;memory leaks,&lt;/li&gt;
&lt;li&gt;dereferencing invalid pointers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On a regular PC this means a crash.&lt;br&gt;
On embedded hardware, it can mean bricking the device.&lt;/p&gt;

&lt;p&gt;But modern C++ provides safe defaults:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;smart pointers&lt;/li&gt;
&lt;li&gt;RAII patterns&lt;/li&gt;
&lt;li&gt;containers instead of raw arrays&lt;/li&gt;
&lt;li&gt;standard algorithms instead of manual loops&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You only drop to low-level code when you really need to.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. A Rich, Optimized Standard Library
&lt;/h2&gt;

&lt;p&gt;The C++ standard library is huge and extremely optimized:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data structures: vectors, lists, hash tables, trees, heaps, queues…&lt;/li&gt;
&lt;li&gt;Algorithms: sort, search, transform, accumulate, shuffle, partition…&lt;/li&gt;
&lt;li&gt;Compile-time features: constexpr, templates, concepts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You rarely need to reinvent anything — the STL implementations are battle-tested and highly tuned by compiler vendors.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Multi-Paradigm: Program However You Want
&lt;/h2&gt;

&lt;p&gt;C++ is one of the most flexible languages ever created.&lt;/p&gt;

&lt;p&gt;You can write:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object-oriented: classes, inheritance, polymorphism&lt;/li&gt;
&lt;li&gt;Functional: lambdas, ranges, immutability&lt;/li&gt;
&lt;li&gt;Generic: templates, concepts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Low-level systems code: memory management, hardware access.&lt;/p&gt;

&lt;p&gt;Instead of forcing a paradigm, C++ gives you tools that match your problem &lt;br&gt;
domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Better Syntax for Humans and Compilers
&lt;/h2&gt;

&lt;p&gt;Modern syntax makes generic programming far more accessible:&lt;/p&gt;

&lt;p&gt;Trailing return types (auto func() -&amp;gt; type) help readability with complex templates.&lt;/p&gt;

&lt;p&gt;decltype lets you extract types from expressions.&lt;/p&gt;

&lt;p&gt;Return type deduction removes boilerplate from functions and templates.&lt;/p&gt;

&lt;p&gt;These features help C++ scale into extremely large and complex codebases — which is why industries like finance and gaming rely on it for long-lived projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Isn’t Going Anywhere
&lt;/h2&gt;

&lt;p&gt;C++ is fast, flexible, portable, and more modern than ever.&lt;br&gt;
It powers the most performance-critical systems on Earth, yet it now offers the high-level tools needed to write safer and cleaner code.&lt;/p&gt;

&lt;p&gt;If you’re a developer who wants to understand how software really works — from hardware to abstractions — C++ remains one of the strongest languages you can learn.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
