C++ Programming 2. C++ Fundamentals 15 mins read 18,906 views

Expressions in C++: Operands, Operators, and Evaluation

When writing a C++ program, you often need to perform calculations, compare values, assign data to variables, or check conditions. These operations are performe...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is Expressions in C++: Operands, Operators, and Evaluation? When writing a C++ program, you often need to perform calculations, compare values, assign data to variables, or check conditions. These operations are performe...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How are expressions formed, evaluated, and classified into value categories (lvalue, prvalue, xvalue) in C++?
<p data-pm-slice="1 1 []">When writing a C++ program, you often need to perform calculations, compare values, assign data to variables, or check conditions. These operations are performed using something called <strong>expressions</strong>.</p><p data-pm-slice="1 1 []"><br></p><p>For example, when you write <code dir="ltr">10 + 5</code>, C++ evaluates the expression and produces the result <code dir="ltr">15</code>.</p><p><br></p><p>But how does C++ understand this calculation? What are operands and operators? And how does the compiler decide which operation should happen first?</p><p><br></p><p>To answer these questions, you need to understand expressions in C++.</p><p>In this tutorial, you will learn what expressions are, how operands and operators work together, the different types of expressions, operator precedence, associativity, and how C++ evaluates expressions with practical examples.</p>

Explanation & In-Depth Details

<h2 class="h4 fw-bold text-dark mb-3"><span style="font-size: 1.15rem;">What Is an Expression in C++?</span></h2><p>An expression in C++ is a combination of values, variables, operators, and sometimes function calls that produces a result or performs an operation.</p><p>Expressions are used in almost every C++ program.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span>
<span class="js-id">a</span> <span class="js-op">*</span> <span class="js-id">b</span>
<span class="js-id">x</span> <span class="js-op">-</span> <span class="js-id">y</span>
<span class="js-op">(</span><span class="js-id">price</span> <span class="js-op">+</span> <span class="js-id">tax</span><span class="js-op">)</span> <span class="js-op">/</span> <span class="js-num">2</span></code></pre></div><p>Each of these is an expression because it represents an operation that C++ can evaluate.</p><h3>Example: A Simple Expression in C++</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Result: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">15</span></code></pre></div><p>In this program, <code>a + b</code> is an expression. C++ adds the values of the two variables and produces the result 15.</p><h2>What Are Operands and Operators in C++?</h2><p>To understand expressions, you first need to know two important terms: operands and operators.</p><h3>1. What Is an Operand?</h3><p>An operand is a value, variable, or other expression on which an operator performs an operation.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span></code></pre></div><p>Here:</p><ul><li><p><code>10</code> is the first operand.</p></li><li><p><code>5</code> is the second operand.</p></li><li><p><code>+</code> is the operator.</p></li></ul><p>Operands can be numbers, variables, constants, or the results of other expressions.</p><h3>2. What Is an Operator?</h3><p>An operator is a symbol or keyword that tells C++ what operation to perform.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span></code></pre></div><p>In this expression, the <code>+</code> operator adds the values of <code>a</code> and <code>b</code>.</p><p>Other common operators include:</p><table><tbody><tr><th>Operator</th><th>Meaning</th><th>Example</th></tr><tr><td><code>+</code></td><td>Addition</td><td><code>a + b</code></td></tr><tr><td><code>-</code></td><td>Subtraction</td><td><code>a - b</code></td></tr><tr><td><code>*</code></td><td>Multiplication</td><td><code>a * b</code></td></tr><tr><td><code>/</code></td><td>Division</td><td><code>a / b</code></td></tr><tr><td><code>%</code></td><td>Remainder</td><td><code>a % b</code></td></tr><tr><td><code>==</code></td><td>Equal to</td><td><code>a == b</code></td></tr><tr><td><code>&gt;</code></td><td>Greater than</td><td><code>a &gt; b</code></td></tr><tr><td><code>&amp;&amp;</code></td><td>Logical AND</td><td><code>a &amp;&amp; b</code></td></tr></tbody></table><h2>Types of Expressions in C++</h2><p>C++ supports different types of expressions depending on the operators and values being used.</p><p>The commonly studied categories include:</p><ol start="1"><li><p>Arithmetic expressions</p></li><li><p>Relational expressions</p></li><li><p>Logical expressions</p></li><li><p>Assignment expressions</p></li><li><p>Conditional expressions</p></li><li><p>Bitwise expressions</p></li><li><p>Increment and decrement expressions</p></li><li><p>Comma expressions</p></li></ol><p>Let's understand each type with simple examples.</p><h2>1. Arithmetic Expressions in C++</h2><p>Arithmetic expressions are used to perform mathematical calculations.</p><p>C++ provides arithmetic operators such as addition, subtraction, multiplication, division, and modulus.</p><h3>Common Arithmetic Operators</h3><table><tbody><tr><th>Operator</th><th>Operation</th><th>Example</th></tr><tr><td><code>+</code></td><td>Addition</td><td><code>10 + 5</code></td></tr><tr><td><code>-</code></td><td>Subtraction</td><td><code>10 - 5</code></td></tr><tr><td><code>*</code></td><td>Multiplication</td><td><code>10 * 5</code></td></tr><tr><td><code>/</code></td><td>Division</td><td><code>10 / 5</code></td></tr><tr><td><code>%</code></td><td>Modulus</td><td><code>10 % 3</code></td></tr></tbody></table><h3>Example: Arithmetic Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Addition: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Subtraction: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">-</span> <span class="js-id">b</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Multiplication: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">*</span> <span class="js-id">b</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Division: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">/</span> <span class="js-id">b</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Remainder: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">%</span> <span class="js-num">3</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Addition</span><span class="js-op">:</span> <span class="js-num">30</span>
<span class="js-id">Subtraction</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Multiplication</span><span class="js-op">:</span> <span class="js-num">200</span>
<span class="js-id">Division</span><span class="js-op">:</span> <span class="js-num">2</span>
<span class="js-id">Remainder</span><span class="js-op">:</span> <span class="js-num">2</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>a + b</code> adds two numbers.</p></li><li><p><code>a - b</code> subtracts the second number from the first.</p></li><li><p><code>a * b</code> multiplies two numbers.</p></li><li><p><code>a / b</code> divides the first number by the second.</p></li><li><p><code>a % 3</code> returns the remainder after division.</p></li></ul><p><strong>Important:</strong> When both operands are integers, division produces an integer result. For example, <code>10 / 3</code> produces <code>3</code>, not <code>3.33</code>.</p><p>To get a decimal result, use a floating-point operand:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">double</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10.0</span> <span class="js-op">/</span> <span class="js-num">3</span><span class="js-op">;</span></code></pre></div><h2>2. Relational Expressions in C++</h2><p>Relational expressions are used to compare two values.</p><p>They produce a Boolean result: <code>true</code> or <code>false</code>.</p><p>These expressions are commonly used in decision-making statements such as <code>if</code>, <code>while</code>, and <code>for</code>.</p><h3>Common Relational Operators</h3><table><tbody><tr><th>Operator</th><th>Meaning</th><th>Example</th></tr><tr><td><code>==</code></td><td>Equal to</td><td><code>a == b</code></td></tr><tr><td><code>!=</code></td><td>Not equal to</td><td><code>a != b</code></td></tr><tr><td><code>&gt;</code></td><td>Greater than</td><td><code>a &gt; b</code></td></tr><tr><td><code>&lt;</code></td><td>Less than</td><td><code>a &lt; b</code></td></tr><tr><td><code>&gt;=</code></td><td>Greater than or equal to</td><td><code>a &gt;= b</code></td></tr><tr><td><code>&lt;=</code></td><td>Less than or equal to</td><td><code>a &lt;= b</code></td></tr></tbody></table><h3>Example: Relational Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&gt;</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&lt;</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">==</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">!=</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">0</span>
<span class="js-num">0</span>
<span class="js-num">1</span></code></pre></div><p>By default, C++ displays <code>true</code> as <code>1</code> and <code>false</code> as <code>0</code> when using <code>cout</code>.</p><p>You can display readable Boolean values using <code>boolalpha</code>:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">boolalpha</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&gt;</span> <span class="js-id">b</span><span class="js-op">);</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">true</span></code></pre></div><h2>3. Logical Expressions in C++</h2><p>Logical expressions are used to combine or reverse conditions.</p><p>They are particularly useful when a program needs to check multiple conditions before performing an operation.</p><p>C++ provides three main logical operators.</p><h3>Common Logical Operators</h3><table><tbody><tr><th>Operator</th><th>Name</th><th>Meaning</th></tr><tr><td><code>&amp;&amp;</code></td><td>Logical AND</td><td>True when both conditions are true</td></tr><tr><td>`</td><td></td><td>`</td><td>Logical OR</td><td>True when at least one condition is true</td></tr><tr><td><code>!</code></td><td>Logical NOT</td><td>Reverses a Boolean value</td></tr></tbody></table><h3>Example: Logical Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">salary</span> <span class="js-op">=</span> <span class="js-num">30000</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">&gt;=</span> <span class="js-num">18</span> <span class="js-op">&amp;&amp;</span> <span class="js-id">salary</span> <span class="js-op">&gt;=</span> <span class="js-num">20000</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">&lt;</span> <span class="js-num">18</span> <span class="js-op">||</span> <span class="js-id">salary</span> <span class="js-op">&gt;=</span> <span class="js-num">20000</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">!(</span><span class="js-id">age</span> <span class="js-op">&gt;=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">1</span>
<span class="js-num">0</span></code></pre></div><h3>Explanation</h3><ol start="1"><li><p>The first expression checks whether both conditions are true.</p></li><li><p>The second expression checks whether at least one condition is true.</p></li><li><p>The third expression reverses the result of the condition.</p></li></ol><h3>Short-Circuit Evaluation</h3><p>C++ uses short-circuit evaluation with logical AND (<code>&amp;&amp;</code>) and logical OR (<code>||</code>).</p><p>This means the second condition may not be evaluated if the first condition already determines the final result.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>

<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">b</span> <span class="js-op">!=</span> <span class="js-num">0</span> <span class="js-op">&amp;&amp;</span> <span class="js-id">a</span> <span class="js-op">/</span> <span class="js-id">b</span> <span class="js-op">&gt;</span> <span class="js-num">2</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Valid"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Here, the second condition is not evaluated because <code>b != 0</code> is false.</p><p>This prevents division by zero in this example.</p><h2>4. Assignment Expressions in C++</h2><p>Assignment expressions are used to assign values to variables.</p><p>The assignment operator is <code>=</code>.</p><p>C++ also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment.</p><h3>Common Assignment Operators</h3><table><tbody><tr><th>Operator</th><th>Meaning</th><th>Example</th></tr><tr><td><code>=</code></td><td>Assign value</td><td><code>a = 10</code></td></tr><tr><td><code>+=</code></td><td>Add and assign</td><td><code>a += 5</code></td></tr><tr><td><code>-=</code></td><td>Subtract and assign</td><td><code>a -= 5</code></td></tr><tr><td><code>*=</code></td><td>Multiply and assign</td><td><code>a *= 2</code></td></tr><tr><td><code>/=</code></td><td>Divide and assign</td><td><code>a /= 2</code></td></tr><tr><td><code>%=</code></td><td>Modulus and assign</td><td><code>a %= 3</code></td></tr></tbody></table><h3>Example: Assignment Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>

<span class="js-id">number</span> <span class="js-op">+=</span> <span class="js-num">5</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"After Addition: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">number</span> <span class="js-op">*=</span> <span class="js-num">2</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"After Multiplication: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">After</span> <span class="js-id">Addition</span><span class="js-op">:</span> <span class="js-num">15</span>
<span class="js-id">After</span> <span class="js-id">Multiplication</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><p>The <code>+=</code> operator adds a value to the existing variable, while <code>*=</code> multiplies the current value and assigns the result back.</p><h2>5. Conditional Expressions in C++</h2><p>The conditional operator <code>?:</code> is also known as the ternary operator because it uses three operands.</p><p>It is used to choose one of two values based on a condition.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">condition</span> <span class="js-op">?</span> <span class="js-id">expression1</span> <span class="js-op">:</span> <span class="js-id">expression2</span><span class="js-op">;</span></code></pre></div><p>If the condition is true, <code>expression1</code> is selected. Otherwise, <code>expression2</code> is selected.</p><h3>Example: Conditional Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>

<span class="js-id">string</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">&gt;=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">?</span> <span class="js-str">"Adult"</span> <span class="js-op">:</span> <span class="js-str">"Minor"</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Adult</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p>The condition checks whether age is greater than or equal to 18.</p></li><li><p>If true, the expression returns <code>"Adult"</code>.</p></li><li><p>If false, it returns <code>"Minor"</code>.</p></li></ul><p>The conditional operator is useful for simple decisions where you need to choose between two expressions.</p><h2>6. Bitwise Expressions in C++</h2><p>Bitwise expressions operate on the individual bits of integer values.</p><p>They are commonly used in low-level programming, embedded systems, networking, and situations where bit-level operations are required.</p><h3>Common Bitwise Operators</h3><table><tbody><tr><th>Operator</th><th>Meaning</th></tr><tr><td><code>&amp;</code></td><td>Bitwise AND</td></tr><tr><td>`</td><td>`</td><td>Bitwise OR</td></tr><tr><td><code>^</code></td><td>Bitwise XOR</td></tr><tr><td><code>~</code></td><td>Bitwise NOT</td></tr><tr><td><code>&lt;&lt;</code></td><td>Left shift</td></tr><tr><td><code>&gt;&gt;</code></td><td>Right shift</td></tr></tbody></table><h3>Example: Bitwise Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">3</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"AND: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&amp;</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"OR: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">|</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"XOR: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">^</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Left Shift: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&lt;&lt;</span> <span class="js-num">1</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Right Shift: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&gt;&gt;</span> <span class="js-num">1</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">AND</span><span class="js-op">:</span> <span class="js-num">1</span>
<span class="js-id">OR</span><span class="js-op">:</span> <span class="js-num">7</span>
<span class="js-id">XOR</span><span class="js-op">:</span> <span class="js-num">6</span>
<span class="js-id">Left</span> <span class="js-id">Shift</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Right</span> <span class="js-id">Shift</span><span class="js-op">:</span> <span class="js-num">2</span></code></pre></div><h3>Explanation Using Binary Values</h3><p>The number 5 in binary is <code>0101</code>, and the number 3 is <code>0011</code> when shown using four bits.</p><ul><li><p>AND produces <code>0001</code>, which is 1.</p></li><li><p>OR produces <code>0111</code>, which is 7.</p></li><li><p>XOR produces <code>0110</code>, which is 6.</p></li><li><p>Left shifting 5 by one position produces 10.</p></li><li><p>Right shifting positive 5 by one position produces 2.</p></li></ul><h2>7. Increment and Decrement Expressions in C++</h2><p>Increment and decrement operators are used to increase or decrease a variable's value by one.</p><p>C++ provides two forms of each operator.</p><h3>Increment Operators</h3><ul><li><p><code>++a</code> – Prefix increment.</p></li><li><p><code>a++</code> – Postfix increment.</p></li></ul><h3>Decrement Operators</h3><ul><li><p><code>--a</code> – Prefix decrement.</p></li><li><p><code>a--</code> – Postfix decrement.</p></li></ul><h3>Example: Increment and Decrement</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Original Value: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Prefix Increment: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">++</span><span class="js-id">a</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Postfix Increment: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span><span class="js-op">++</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Final Value: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Prefix</span> <span class="js-id">Increment</span><span class="js-op">:</span> <span class="js-num">11</span>
<span class="js-id">Postfix</span> <span class="js-id">Increment</span><span class="js-op">:</span> <span class="js-num">11</span>
<span class="js-id">Final</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">12</span></code></pre></div><h3>Difference Between Prefix and Postfix</h3><p><strong>Prefix increment (</strong><code><strong>++a</strong></code><strong>):</strong></p><p>The variable is increased first, and then its updated value is used.</p><p><strong>Postfix increment (</strong><code><strong>a++</strong></code><strong>):</strong></p><p>The original value is used first, and then the variable is increased.</p><p>The same distinction applies to prefix and postfix decrement.</p><h2>8. Comma Expressions in C++</h2><p>The comma operator <code>,</code> allows multiple expressions to be evaluated from left to right.</p><p>The result of a comma expression is the result of its rightmost expression.</p><h3>Example: Comma Expression</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span><span class="js-op">,</span> <span class="js-id">b</span><span class="js-op">,</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-id">result</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">,</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">,</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Result: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ol start="1"><li><p><code>a = 10</code> is evaluated first.</p></li><li><p><code>b = 20</code> is evaluated next.</p></li><li><p><code>a + b</code> is evaluated last.</p></li><li><p>The final result is assigned to <code>result</code>.</p></li></ol><p>The comma operator is different from the commas used to separate function arguments or variable declarations.</p><h2>What Is Expression Evaluation in C++?</h2><p>Expression evaluation is the process by which C++ determines the value or effect of an expression.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span> <span class="js-op">*</span> <span class="js-num">2</span><span class="js-op">;</span></code></pre></div><p>C++ does not simply evaluate every operation from left to right. It follows language rules such as operator precedence, associativity, and sequencing.</p><p>In this example, multiplication has higher precedence than addition.</p><p>Therefore:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span> <span class="js-op">*</span> <span class="js-num">2</span>
<span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">10</span>
<span class="js-num">20</span></code></pre></div><p>The final result is 20.</p><p>Understanding these rules helps you predict how expressions behave.</p><h2>Operator Precedence in C++</h2><p>Operator precedence determines which operators are grouped first when an expression contains multiple operators.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span> <span class="js-op">*</span> <span class="js-num">2</span><span class="js-op">;</span></code></pre></div><p>Multiplication has higher precedence than addition, so <code>5 * 2</code> is grouped first.</p><h3>Common Operator Precedence Table</h3><p>The following table shows common C++ operators from higher precedence to lower precedence.</p><table><tbody><tr><th>Priority</th><th>Operators</th><th>Description</th></tr><tr><td>1</td><td><code>()</code> <code>[]</code> <code>.</code> <code>-&gt;</code></td><td>Primary and member access</td></tr><tr><td>2</td><td><code>++</code> <code>--</code> <code>!</code> <code>~</code> <code>+</code> <code>-</code> <code>*</code> <code>&amp;</code></td><td>Unary operators</td></tr><tr><td>3</td><td><code>*</code> <code>/</code> <code>%</code></td><td>Multiplication, division, modulus</td></tr><tr><td>4</td><td><code>+</code> <code>-</code></td><td>Addition, subtraction</td></tr><tr><td>5</td><td><code>&lt;&lt;</code> <code>&gt;&gt;</code></td><td>Bitwise shifts</td></tr><tr><td>6</td><td><code>&lt;</code> <code>&lt;=</code> <code>&gt;</code> <code>&gt;=</code></td><td>Relational operators</td></tr><tr><td>7</td><td><code>==</code> <code>!=</code></td><td>Equality operators</td></tr><tr><td>8</td><td><code>&amp;</code></td><td>Bitwise AND</td></tr><tr><td>9</td><td><code>^</code></td><td>Bitwise XOR</td></tr><tr><td>10</td><td>`</td><td>`</td><td>Bitwise OR</td></tr><tr><td>11</td><td><code>&amp;&amp;</code></td><td>Logical AND</td></tr><tr><td>12</td><td>`</td><td></td><td>`</td><td>Logical OR</td></tr><tr><td>13</td><td><code>?:</code></td><td>Conditional operator</td></tr><tr><td>14</td><td><code>=</code> <code>+=</code> <code>-=</code> <code>*=</code> <code>/=</code> <code>%=</code></td><td>Assignment operators</td></tr><tr><td>15</td><td><code>,</code></td><td>Comma operator</td></tr></tbody></table><p>This is a simplified reference for commonly used operators. Some operators share precedence levels, and C++ has additional operators and details.</p><h2>What Is Operator Associativity in C++?</h2><p>Operator associativity determines how operators of the same precedence are grouped when they appear together.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">20</span> <span class="js-op">-</span> <span class="js-num">5</span> <span class="js-op">-</span> <span class="js-num">3</span><span class="js-op">;</span></code></pre></div><p>The subtraction operator has left-to-right associativity.</p><p>Therefore, the expression is grouped as:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-op">(</span><span class="js-num">20</span> <span class="js-op">-</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">-</span> <span class="js-num">3</span>
<span class="js-num">15</span> <span class="js-op">-</span> <span class="js-num">3</span>
<span class="js-num">12</span></code></pre></div><h3>Left-to-Right Associativity</h3><p>Most arithmetic operators use left-to-right associativity.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">100</span> <span class="js-op">/</span> <span class="js-num">10</span> <span class="js-op">/</span> <span class="js-num">2</span><span class="js-op">;</span></code></pre></div><p>Evaluation grouping:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-op">(</span><span class="js-num">100</span> <span class="js-op">/</span> <span class="js-num">10</span><span class="js-op">)</span> <span class="js-op">/</span> <span class="js-num">2</span>
<span class="js-num">10</span> <span class="js-op">/</span> <span class="js-num">2</span>
<span class="js-num">5</span></code></pre></div><h3>Right-to-Left Associativity</h3><p>Assignment operators use right-to-left associativity.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">a</span><span class="js-op">,</span> <span class="js-id">b</span><span class="js-op">,</span> <span class="js-id">c</span><span class="js-op">;</span>

<span class="js-id">a</span> <span class="js-op">=</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-id">c</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span></code></pre></div><p>This is grouped as:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">a</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">b</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">c</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">));</span></code></pre></div><p>All three variables receive the value 10.</p><p><strong>Important:</strong> Associativity determines grouping, not necessarily the order in which every operand or function call is evaluated. C++ has separate sequencing rules. Avoid writing expressions that modify the same variable multiple times without a clear sequencing relationship.</p><h2>Difference Between Operator Precedence and Associativity</h2><p>These two concepts work together, but they have different purposes.</p><table><tbody><tr><th>Feature</th><th>Operator Precedence</th><th>Operator Associativity</th></tr><tr><td>Meaning</td><td>Determines which operators are grouped first</td><td>Determines grouping when operators share precedence</td></tr><tr><td>Purpose</td><td>Establishes operator priority</td><td>Resolves grouping within the same priority</td></tr><tr><td>Example</td><td><code>10 + 5 * 2</code></td><td><code>20 - 5 - 3</code></td></tr><tr><td>Result</td><td>20</td><td>12</td></tr></tbody></table><h2>How to Control Expression Evaluation Using Parentheses</h2><p>Parentheses are used to control how an expression is grouped.</p><p>They make complex expressions easier to understand and help avoid mistakes.</p><h3>Example Without Parentheses</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span> <span class="js-op">*</span> <span class="js-num">2</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Result: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">20</span></code></pre></div><h3>Example With Parentheses</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">*</span> <span class="js-num">2</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Result: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><p>Parentheses change the grouping of the expression, so the addition is performed before multiplication.</p><h2>Complete C++ Program Using Different Expressions</h2><p>Now let's combine several expression types in one practical program.</p><h3>C++ Code</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>

<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>

<span class="js-cm">// Arithmetic expression</span>
<span class="js-id">int</span> <span class="js-id">sum</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span>

<span class="js-cm">// Relational expression</span>
<span class="js-id">bool</span> <span class="js-id">isGreater</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">&gt;</span> <span class="js-id">b</span><span class="js-op">;</span>

<span class="js-cm">// Logical expression</span>
<span class="js-id">bool</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&gt;</span> <span class="js-num">5</span> <span class="js-op">&amp;&amp;</span> <span class="js-id">b</span> <span class="js-op">&lt;</span> <span class="js-num">10</span><span class="js-op">);</span>

<span class="js-cm">// Assignment expression</span>
<span class="js-id">a</span> <span class="js-op">+=</span> <span class="js-num">2</span><span class="js-op">;</span>

<span class="js-cm">// Conditional expression</span>
<span class="js-id">string</span> <span class="js-id">status</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">&gt;</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">?</span> <span class="js-str">"Greater"</span> <span class="js-op">:</span> <span class="js-str">"Smaller"</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Sum: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sum</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Is Greater: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">boolalpha</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">isGreater</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Logical Result: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">result</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Updated A: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">a</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Status: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">status</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>Output</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Sum</span><span class="js-op">:</span> <span class="js-num">15</span>
<span class="js-id">Is</span> <span class="js-id">Greater</span><span class="js-op">:</span> <span class="js-num">true</span>
<span class="js-id">Logical</span> <span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">true</span>
<span class="js-id">Updated</span> <span class="js-id">A</span><span class="js-op">:</span> <span class="js-num">12</span>
<span class="js-id">Status</span><span class="js-op">:</span> <span class="js-id">Greater</span></code></pre></div><h3>Explanation</h3><p>This program demonstrates five different expression types:</p><ul><li><p>Arithmetic expression calculates the sum of two values.</p></li><li><p>Relational expression compares two variables.</p></li><li><p>Logical expression checks multiple conditions.</p></li><li><p>Assignment expression updates the value of a variable.</p></li><li><p>Conditional expression selects a result based on a condition.</p></li></ul><p>These expression types are used regularly in real-world C++ applications.</p><h2>Common Mistakes When Using Expressions in C++</h2><p>Beginners sometimes make mistakes when writing expressions. Here are some common problems and how to avoid them.</p><h3>1. Confusing Assignment and Equality Operators</h3><p>Incorrect assumption:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Equal"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The <code>=</code> operator assigns a value. It does not compare two values.</p><p>Correct example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">a</span> <span class="js-op">==</span> <span class="js-num">10</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Equal"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Use <code>==</code> when you want to check whether two values are equal.</p><h3>2. Ignoring Operator Precedence</h3><p>Consider:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10</span> <span class="js-op">+</span> <span class="js-num">5</span> <span class="js-op">*</span> <span class="js-num">2</span><span class="js-op">;</span></code></pre></div><p>The result is 20, not 30, because multiplication has higher precedence than addition.</p><p>Use parentheses when you want a different grouping.</p><h3>3. Performing Integer Division</h3><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10</span> <span class="js-op">/</span> <span class="js-num">3</span><span class="js-op">;</span></code></pre></div><p>The result is 3 because both operands are integers.</p><p>Use a floating-point operand if you need a decimal result:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">double</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-num">10.0</span> <span class="js-op">/</span> <span class="js-num">3</span><span class="js-op">;</span></code></pre></div><h3>4. Dividing by Zero</h3><p>Incorrect example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">/</span> <span class="js-id">b</span><span class="js-op">;</span></code></pre></div><p>Integer division by zero has undefined behaviour.</p><p>Always check the divisor before performing division.</p><h3>5. Writing Confusing Expressions</h3><p>Avoid writing very long expressions that are difficult to understand.</p><p>Instead, break complex calculations into smaller steps using meaningful variable names.</p><h3>6. Confusing Logical and Bitwise Operators</h3><p>Remember:</p><ul><li><p><code>&amp;&amp;</code> means logical AND.</p></li><li><p><code>&amp;</code> means bitwise AND.</p></li><li><p><code>||</code> means logical OR.</p></li><li><p><code>|</code> means bitwise OR.</p></li></ul><p>These operators have different purposes and should not be used interchangeably.</p><h2>Best Practices for Writing Expressions in C++</h2><p>Follow these simple practices when working with C++ expressions:</p><ul><li><p>Use meaningful variable names.</p></li><li><p>Use parentheses to make complex expressions easier to understand.</p></li><li><p>Understand operator precedence before combining multiple operators.</p></li><li><p>Avoid unnecessary complicated expressions.</p></li><li><p>Use <code>==</code> for comparison and <code>=</code> for assignment.</p></li><li><p>Check for division by zero.</p></li><li><p>Use floating-point values when decimal calculations are required.</p></li><li><p>Understand the difference between logical and bitwise operators.</p></li><li><p>Avoid modifying the same variable multiple times in an unsequenced expression.</p></li><li><p>Break complicated calculations into smaller statements.</p></li></ul><h2>Frequently Asked Questions About Expressions in C++</h2><h3>1. What is an expression in C++?</h3><p>An expression is a valid combination of operands, operators, and sometimes function calls that produces a value or performs an operation.</p><h3>2. What are operands in C++?</h3><p>Operands are the values or variables on which operators perform operations. For example, in <code>10 + 5</code>, the numbers 10 and 5 are operands.</p><h3>3. What are operators in C++?</h3><p>Operators are symbols or keywords that perform specific operations on operands. Examples include <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>==</code>, and <code>&amp;&amp;</code>.</p><h3>4. What are the different types of expressions in C++?</h3><p>Common expression categories include arithmetic, relational, logical, assignment, conditional, bitwise, increment, decrement, and comma expressions.</p><h3>5. What is expression evaluation in C++?</h3><p>Expression evaluation is the process of determining the value or effect of an expression according to C++ language rules.</p><h3>6. What is operator precedence in C++?</h3><p>Operator precedence determines how operators are grouped in an expression. Operators with higher precedence are grouped before operators with lower precedence.</p><h3>7. What is operator associativity in C++?</h3><p>Operator associativity determines how operators with the same precedence are grouped. For example, subtraction uses left-to-right associativity.</p><h3>8. Why are parentheses used in C++ expressions?</h3><p>Parentheses control the grouping of an expression. They help specify the intended calculation order and improve code readability.</p><h3>9. What is the difference between logical and relational expressions?</h3><p>Relational expressions compare values and produce a Boolean result. Logical expressions combine or reverse Boolean conditions.</p><h3>10. What is a conditional expression in C++?</h3><p>A conditional expression uses the ternary operator <code>?:</code> to select one of two expressions based on a condition.</p><h3>11. Can an expression contain multiple operators?</h3><p>Yes. An expression can contain multiple operators and operands. C++ uses precedence and associativity rules to determine how the expression is grouped.</p><h3>12. What is the difference between an expression and a statement?</h3><p>An expression produces a value or performs an operation. A statement is a complete instruction executed by the program. An expression can form part of a statement.</p>

Code Example

#include <iostream>
#include <string>
#include <utility>

void processValue(const std::string& lval) {
    std::cout << "Lvalue bind: " << lval << "
";
}

void processValue(std::string&& rval) {
    std::cout << "Rvalue / Move bind: " << rval << "
";
}

int main() {
    std::string message = "Persistent lvalue";
    processValue(message); // Calls lvalue overload

    processValue("Temporary prvalue"); // Calls rvalue overload
    processValue(std::move(message));  // std::move converts to xvalue -> rvalue overload

    // Short-circuit evaluation demonstration
    int* ptr = nullptr;
    if (ptr != nullptr && *ptr == 10) {
        // Safe: *ptr is never evaluated because left side is false
    } else {
        std::cout << "Short-circuit evaluation prevented null pointer dereference.
";
    }
    return 0;
}

Interview Question & Tips

Q: What is the difference between an lvalue and an rvalue in C++?
A: An lvalue has an identifiable memory address that can be inspected with the address-of operator `&` and persists beyond a single expression. An rvalue is a temporary value that does not persist and can be moved from.

Q: Why is short-circuit evaluation critical for pointer safety?
A: It allows testing pointer validity `ptr != nullptr && ptr->isValid()` in a single boolean expression without risking segmentation faults when `ptr` is null.

Best Practices

1. Rely on short-circuit evaluation for safe guard checks.
2. Use `std::move` to explicitly cast lvalues to xvalues when ownership transfer is intended.
3. Avoid expressions with side-effects on the same variable without sequence points (e.g. `i = i++`).
Academic Standards & Verified Citations

This study reference for Expressions in C++: Operands, Operators, and Evaluation is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming