<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Somansh Writes]]></title><description><![CDATA[Hello👋. Welcome to my Blog! I'm navigating the exciting world of computers and technology and I'm here to share my journey with the world. Hop in!!]]></description><link>https://www.somanshv.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1670257192315/_CgSgnmpX.png</url><title>Somansh Writes</title><link>https://www.somanshv.com</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 01:41:57 GMT</lastBuildDate><atom:link href="https://www.somanshv.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Flow of Control (Selection Statements)]]></title><description><![CDATA[Hi, Welcome to the 3rd article in this series. In this article, I will talk about Selection statements in C++. But before that let's explore different types of statements.
What are statements?
Statements are instructions given to the computer to perf...]]></description><link>https://www.somanshv.com/flow-of-control-selection-statements</link><guid isPermaLink="true">https://www.somanshv.com/flow-of-control-selection-statements</guid><category><![CDATA[C++]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[if-else]]></category><category><![CDATA[Switch case]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Somansh V]]></dc:creator><pubDate>Mon, 12 Dec 2022 14:37:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670843389275/XKev-TdO2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, Welcome to the 3rd article in this series. In this article, I will talk about Selection statements in C++. But before that let's explore different types of statements.</p>
<h1 id="heading-what-are-statements">What are statements?</h1>
<p>Statements are instructions given to the computer to perform any kind of action. They form the smallest executable unit within a C++ program. Statements are terminated with a semicolon ( ; ).</p>
<p>It can be a simple statement such as this.👇</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">int</span> profit = SellingPrice - CostPrice ;
</code></pre>
<p>or, it can be a sequence of simple statements enclosed by a pair of braces {}. For instance,</p>
<pre><code class="lang-cpp">{
   statement1;
   statement2;
   .
   .
   .
}
</code></pre>
<h1 id="heading-statement-flow-control">Statement Flow Control</h1>
<p>In a program, statements may be executed <em>sequentially, selectively or iteratively</em>. Let us see what these mean.</p>
<ol>
<li><strong><mark>Sequence</mark></strong></li>
</ol>
<p>The sequence construct means the statements are being executed sequentially i.e, one after the another. This represents the default flow of the statement. Every C++ program begins with the first statement of <em>main()**</em>.<em>* When the last statement of </em>main()* is executed, the program is done.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670844608150/U0Suc9XDQ.png" alt class="image--center mx-auto" /></p>
<ol>
<li><strong><mark>Selection</mark></strong></li>
</ol>
<p>The <em>selection construct</em> means the execution of statement(s) depending upon a <em>condition-test.</em> If the condition evaluates to true, a course of action (a set of statements) is followed otherwise another course of action (set of statements) is followed. This construct is also called <em>the decision construct</em>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670845453638/SMUWgbou_.png" alt class="image--center mx-auto" /></p>
<ol>
<li><strong><mark>Iteration</mark></strong></li>
</ol>
<p>The iteration construct means the repetition of a set-of-statements depending upon a condition-test. Till the time a condition is true (or false depending upon the loop), a set-of-statements are repeated again and again. As soon as the condition becomes false(or true), the repetition stops. Such iteration construct is called <em>a looping construct.</em> The condition on which the execution or exit of the loop depends is called the <strong>exit condition</strong> or <strong>test-codition</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670846017577/v--hzP5rT.png" alt class="image--center mx-auto" /></p>
<p>As you have seen selection and iteration constructs depend upon a conditional expression that determines what course-of-action is to be taken. A conditional expression evaluates to either <em>true</em> or <em>false.</em> <mark>In C++, any nonzero value is a true value including negative numbers. A 0 (zero) is treated as false.</mark></p>
<p>In this article, we will look at selection statements and in the upcoming article we will learn about iterative statements.</p>
<h1 id="heading-selection-statements">Selection Statements</h1>
<p>C++ provides two types of selection statements: <strong>if</strong> and <strong>switch</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670855297614/b9VxoItlb.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-the-if-statement-of-c">The if Statement of C++</h2>
<p>An if statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed i.e, a statement or set of statements is executed. Otherwise, the course of action is ignored. The general syntax of an if statement is as follows:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (expression)
statement;
</code></pre>
<p>where a statement may consist of a single statement, a compound statement or nothing. The expression must be enclosed within parentheses. If the <em>expression</em> evaluates to true, the <em>statement</em> is executed, otherwise ignored.</p>
<p>Consider this example:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (x)
{
   <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"x is non-zero"</span>;
}
<span class="hljs-keyword">if</span> (!x)
<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"!x is zero this time"</span>;
</code></pre>
<p>If the value of x is zero, The above code will display :</p>
<blockquote>
<p>!x is zero this time</p>
</blockquote>
<p>And for non-zero values of x, it will display :</p>
<blockquote>
<p>x is non -zero</p>
</blockquote>
<p>The <em>expression</em> can contain a variety of test-conditions, such as:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(grade == <span class="hljs-string">'A'</span> || grade == <span class="hljs-string">'a'</span>)     
   <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"Excellent"</span>;

<span class="hljs-keyword">if</span>(a&gt;b)
  <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"A has more than B has"</span>;

<span class="hljs-keyword">if</span> ((x &gt;= <span class="hljs-number">2</span>) &amp;&amp; (x&lt;= <span class="hljs-number">10</span>))
   {                     <span class="hljs-comment">//The if body contains a compound statement</span>
      <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"x is greater than 2 and less than 10"</span>;
       x = x+<span class="hljs-number">20</span>;
    }
</code></pre>
<p>Now, what if there is another course of action to be followed if the expression evaluates to false? There is another form of if that allows for this kind of either-or condition by providing the <strong>else</strong> clause. The syntax of <strong>an if-else</strong> statement is:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(expression)
   statement <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
   statement <span class="hljs-number">2</span>;
</code></pre>
<p>If the expression evaluates to true, statement-1 is executed, otherwise, statement-2 is executed. Statement-1 and statement-2 can be a single statement, compound statement or null statement.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670848175017/ZV5Ty8_xt.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (<span class="hljs-number">100</span> &gt; x)
   <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">" 100 is greater than x"</span>;<span class="hljs-comment">// if 100&gt;x evaluate to true</span>
<span class="hljs-keyword">else</span>
   <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">" 100 is smaller than x"</span>; <span class="hljs-comment">//if 100&gt;x evaluate to false</span>
</code></pre>
<p><strong><mark>Nested ifs:</mark></strong></p>
<p>A nested if is an if that has another if in its <em>if's body</em> or in its <em>else's body. I</em>t can have one of three forms:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670850881672/eLY9JS2ar.png" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://github.com/som-ansh/CPP_Scripts/blob/master/flowOfControl/SimpleCalculator.cpp">CLICK HERE</a> for an example program which illustrates the use of nested ifs.</p>
<h2 id="heading-the-dangling-else-problem">The Dangling-Else Problem</h2>
<p>The nested if-else statement introduces a source of potential ambiguity referred to as a dangling-else problem. This problem arises when in a nested if statement, the number of ifs is more than the number of else clauses. The question then arises, with which if does the additional else clause property matchup? For instance,</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(ch&gt;= <span class="hljs-string">'A'</span>)
  <span class="hljs-keyword">if</span>(ch &lt;= <span class="hljs-string">'Z'</span>)
       ++upcase;
<span class="hljs-keyword">else</span>
  ++others;
</code></pre>
<p>The indentation in the above code fragment indicates that we want the else to be with the outer if. However, <mark>C++ matches an else with the preceding unmatched if.</mark> In this case, the actual evaluation happens something like this:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(ch&gt;= <span class="hljs-string">'A'</span>)
  <span class="hljs-keyword">if</span>(ch&lt;= <span class="hljs-string">'Z'</span>)  <span class="hljs-comment">//---the else clause matches up with this if statement</span>
      ++upcase;
  <span class="hljs-keyword">else</span>
      ++others;
</code></pre>
<p>That is, if the inner expression is false i.e, zero then the else clause gets executed.</p>
<p>What if you want to override the default dangling-else matching? i.e, consider:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(exp1)
   <span class="hljs-keyword">if</span>(exp2)
      statement1;
<span class="hljs-keyword">else</span>
      statement2;
</code></pre>
<p>you want the else to go with the other if. (By default, it will go with the inner if).</p>
<p>One method of overriding the default dangling-else matching is to place the last occurring unmatched if in a compound statement, as shown below:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(exp1)
{
    <span class="hljs-keyword">if</span>(exp2)
       statement1;
}
<span class="hljs-keyword">else</span>               <span class="hljs-comment">//Now wlse matches up with the outer if.</span>
       statement2;
</code></pre>
<p><a target="_blank" href="https://github.com/som-ansh/CPP_Scripts/blob/master/flowOfControl/SalesCommission.cpp">CLICK HERE</a> to view the program illustrating the use of dangling-else matching.</p>
<h2 id="heading-the-if-else-if-ladder">The if-else-if Ladder:</h2>
<p>It takes the following general form:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span>(expression <span class="hljs-number">1</span>) 
   statement <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(expression <span class="hljs-number">2</span>) 
   statement <span class="hljs-number">2</span>;
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(expression <span class="hljs-number">3</span>)
   statement <span class="hljs-number">3</span>;
          .
          .
<span class="hljs-keyword">else</span>
   statement <span class="hljs-number">4</span>;
</code></pre>
<p>The expressions are evaluated from the top downward. As soon as an expression evaluates to true, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expressions is true, the final else gets executed. If the final else is missing, no action takes place if all other conditions are false.</p>
<p><a target="_blank" href="https://github.com/som-ansh/CPP_Scripts/blob/master/flowOfControl/QuadraticRoots.cpp">CLICK HERE</a> for a program which illustrates the if-else-if ladder.</p>
<h2 id="heading-the-switch-statement">The Switch Statement</h2>
<p>C++ provides a multiple-branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integers or character constants. When a match is found, the statements associated with that constant are executed.</p>
<p>The syntax of switch is as follows:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">switch</span> (expression)
{
  <span class="hljs-keyword">case</span> constant1 :   statement sequence <span class="hljs-number">1</span>;
                     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> constant2 :   statement sequence <span class="hljs-number">2</span>;
                     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> constant3 :   statement sequence <span class="hljs-number">3</span>;
                     <span class="hljs-keyword">break</span>;
  .
  .
  .
  <span class="hljs-keyword">case</span> constant n<span class="hljs-number">-1</span> :statement sequence m<span class="hljs-number">-1</span>;
                     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">default</span>        : statement sequence n;
</code></pre>
<p>The <em>expression</em> is evaluated and its values are matched against the values of the constants specified in the <strong>case statements.</strong> When a match is found, the <em>statement sequence</em> associated with that case is executed until the <strong>break</strong> statement or the end of the switch statement is reached. <mark>If the case statement does not include a break statement, then the control continues right on the next case statement(s) until either a break is encountered or the end of switch is reached</mark>. This situation is called a <strong><mark>fall-through.</mark></strong></p>
<p>The <strong>default</strong> statement gets executed when no match is found. The default statement is optional and, if it is missing, no action takes place if all matches fail. The <strong>break</strong> statement is one of C++ jump statements. When a break statement is encountered in a switch statement program execution jumps to the line of code following the switch statement i.e, outside the body of switch statement.</p>
<p><a target="_blank" href="https://github.com/som-ansh/CPP_Scripts/blob/master/flowOfControl/AreaOfShapes.cpp">CLICK HERE</a> to view the program which illustrates the switch statement.</p>
<p>Now, you may be confused by seeing the switch statement. It does a similar job to if-else. So, let us see the key differences between the two.</p>
<h2 id="heading-the-switch-vs-if-else">The Switch Vs If-Else</h2>
<ol>
<li><p><strong>Switch</strong> can only test for equality whereas <strong>if</strong> can evaluate a relational or logical expression i.e, multiple conditions.</p>
</li>
<li><p>The <strong>switch</strong> statement selects its branches by testing the value of same variable against a set of constants whereas, the <strong>if-else</strong> construction lets you use a series of expressions that may involve unrelated variables and complex expressions.</p>
</li>
<li><p>The <strong>if-else</strong> statement can handle floating-point tests also apart from handling integer and character tests whereas a <strong>switch</strong> cannot handle floating-point tests. The case label of switch must be an integer ( which includes char also)</p>
</li>
<li><p>The <strong>switch</strong> statement is a more efficient choice in terms of code used in a situation that supports the nature of switch operation (testing a value against a set of constants)</p>
</li>
</ol>
<p><strong><mark>The Nested-Switch:</mark></strong></p>
<p>Like if statements, a switch statement can also be nested. There can be a switch as part of the statement sequence of another switch. For example:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">switch</span>(a)
{
   <span class="hljs-keyword">case</span> <span class="hljs-number">1</span> : <span class="hljs-keyword">switch</span> (b)
             {
                 <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> : <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"inside nested-switch case 0"</span>;
                          <span class="hljs-keyword">break</span>;
                 <span class="hljs-keyword">case</span> <span class="hljs-number">1</span> : x= (a+b)/<span class="hljs-number">2</span>;
                          <span class="hljs-keyword">break</span>;
              }
             <span class="hljs-keyword">break</span>;
   <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>   :
  .
  .
  .
}
</code></pre>
<h2 id="heading-some-important-things-about-switch-statement">Some Important Things About Switch Statement</h2>
<ol>
<li><p>A switch statement can only work for equality comparisons.</p>
</li>
<li><p>No two case labels in the same switch can have identical values. But, in the case of nested switch statements the case constants of the inner and outer switch can contain common values.</p>
</li>
<li><p>If character constants are used in the switch statement, they are automatically converted to their integers. (i.e, their equivalent ASCII codes)</p>
</li>
<li><p>Remember to always put the break statement after the last case statement in a switch.</p>
</li>
</ol>
<p>Thats it folks. If you have followed along until the end, you must have a basic understanding of C++ selection statements. So, stop not! Pickup a few questions on these and start coding🧑‍💻.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670855330838/J5LhBFhOp.gif" alt class="image--center mx-auto" /></p>
<p>Please feel free to start a discussion in the comments section, react to this article and stay tuned for more C++.🤠</p>
]]></content:encoded></item><item><title><![CDATA[Increment/Decrement operators in C++]]></title><description><![CDATA["Don't let what you cannot do interfere with what you can do." - John R. Wooden

In this article, I will be talking about the increment/decrement operator in C++. This is a type of operator that may get a little bit confusing for beginners in C++, es...]]></description><link>https://www.somanshv.com/incrementdecrement-operators-in-c</link><guid isPermaLink="true">https://www.somanshv.com/incrementdecrement-operators-in-c</guid><category><![CDATA[C++]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Developer]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Somansh V]]></dc:creator><pubDate>Tue, 06 Dec 2022 18:06:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670345820803/Mxku3GaLh.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>"Don't let what you cannot do interfere with what you can do." - John R. Wooden</p>
</blockquote>
<p>In this article, I will be talking about the increment/decrement operator in C++. This is a type of operator that may get a little bit confusing for beginners in C++, especially postfix/prefix distinction which we will soon see. Let's hop in and make this easy for you.</p>
<p>C++ includes two useful operators not generally found in other programming languages (except C). These are the :</p>
<ul>
<li><p><strong>Increment operator ++</strong></p>
</li>
<li><p><strong>Decrement operator --</strong></p>
</li>
</ul>
<p>The C++ name itself is influenced by the increment operator ++. In simple words, these operators add/subtract 1 from their operands. i.e</p>
<pre><code class="lang-cpp">a = a + <span class="hljs-number">1</span>;  is same as ++a; <span class="hljs-keyword">or</span> a++;

a = a - <span class="hljs-number">1</span>;  is same as --a; <span class="hljs-keyword">or</span> a--;
</code></pre>
<p>However, both come in two varieties: they may either precede or follow the operand.</p>
<ul>
<li><p>The <strong><mark>Prefix </mark></strong> version comes before the operand (as in ++a or --a)</p>
</li>
<li><p>The <strong><mark>Postfix</mark></strong> version comes after the operand (as in a++ or a--)</p>
</li>
</ul>
<p>The two versions have the same effect on the operand, but they differ when they take place in an expression.</p>
<h1 id="heading-working-with-the-prefix-version">Working with the Prefix version</h1>
<p>When an increment or decrement operator precedes its operand, C++ performs the increment or decrement operation before using the value of the operand. It follows the <em>change-then-use</em> rule i.e., they first change the value of their operand, then use the new value in evaluating the expression.</p>
<p>For example, consider below assuming the initial values of <em>sum</em> and <em>count</em> are 0 and 10 respectively.</p>
<pre><code class="lang-cpp">sum = sum + (++count);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670347698141/gnyWSdKqS.png" alt /></p>
<p>The expression below will also similarly take place, assuming the initial values of P and N are 4 and 8 respectively.</p>
<pre><code class="lang-cpp">P = P * --N;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670347999017/GHlTVUb8z.png" alt /></p>
<h1 id="heading-working-with-the-postfix-version">Working with the Postfix version</h1>
<p>When an increment or decrement operator follows its operand, C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value. It follows the <em>use-then-change</em> rule i.e., they first use the value of their operand in evaluating the expression and then change the operand's value.</p>
<p>For example, the expression below will take place in the following fashion assuming the initial values of <em>sum</em> and <em>count</em> are 0 and 10 respectively.</p>
<pre><code class="lang-cpp">sum = sum + count++ ;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670348579118/33Ay9M1NL.png" alt /></p>
<p>The expression below will take place similarly, assuming the initial values of P and N are 4 and 8 respectively.</p>
<pre><code class="lang-cpp">P = P * N--
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670348801587/cJLm6hkki.png" alt /></p>
<p>Please note that the overall effect on the operand's value, in both cases of prefix and postfix operators, is the same (the final value of ++count &amp; count++ and --N &amp; N-- are the same). However, they differ in when they take place in the evaluation of an expression.</p>
<p>Now that we have understood the basics, we will see some examples.</p>
<h1 id="heading-example-1">Example 1</h1>
<p>Predict the output of the following code snippet:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">int</span> n = <span class="hljs-number">7</span>;
<span class="hljs-built_in">cout</span>&lt;&lt; <span class="hljs-string">"n++ ="</span> &lt;&lt; n++&lt;&lt;<span class="hljs-built_in">endl</span>; <span class="hljs-comment">//first-use-then-increment rule.</span>
<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"n= "</span> &lt;&lt; n &lt;&lt;<span class="hljs-string">"\n"</span>;    <span class="hljs-comment">//incremented value is shown here</span>
</code></pre>
<p>The output of the above code will be:</p>
<p>n++ = 7</p>
<p>n=8</p>
<h1 id="heading-example-2">Example 2</h1>
<p>Predict the output of the following code snippet:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">int</span> n = <span class="hljs-number">7</span>;
<span class="hljs-built_in">cout</span>&lt;&lt; <span class="hljs-string">"++n ="</span> &lt;&lt; ++n &lt;&lt;<span class="hljs-built_in">endl</span>;
<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"n ="</span> &lt;&lt; n &lt;&lt;<span class="hljs-string">"\n"</span>
</code></pre>
<p>The output of the above code will be:</p>
<p>++n =8</p>
<p>n = 8</p>
<p>Hope this article has cleared up the confusion on prefix and postfix increment/decrement operators.</p>
<p>Follow this space for more articles on C++</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to Version Control and Git.]]></title><description><![CDATA[Hello all 👋. Hop in until the end for a surprise!🎁
In this article, we will be focusing on version control systems, their need and types and finally, we will learn how Git, which is one of the many version control systems available and used by many...]]></description><link>https://www.somanshv.com/introduction-to-version-control-and-git</link><guid isPermaLink="true">https://www.somanshv.com/introduction-to-version-control-and-git</guid><category><![CDATA[Git]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[version control]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Somansh V]]></dc:creator><pubDate>Mon, 05 Dec 2022 19:21:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670261015473/c_5EGgrVX.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello all 👋. Hop in until the end for a surprise!🎁</p>
<p>In this article, we will be focusing on version control systems, their need and types and finally, we will learn how Git, which is one of the many version control systems available and used by many works. This is going to be a long one, so bear with me😀</p>
<h1 id="heading-what-is-version-control">What is version control?</h1>
<p>Version control is a system that records changes ta a file or set of files over time so that you can recall specific versions later. You may think this is only for software source code files, but in reality, you can do this with nearly any type of file on a computer.</p>
<p>If you are a developer, you would want to keep track of the changes made to your source code files, so a Version Control System (VCS) would be a very wise thing to use. It allows you to revert selected files to a previous state, revert the entire project to a previous state, compare changes over time, see who last modified something that might be causing an issue and many more. Sounds magical right?🪄 well it's not! <strong>it's just technology!</strong> 😎</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670261914439/0eYQ9i4_r.gif" alt class="image--center mx-auto" /></p>
<h1 id="heading-types-of-version-control-systems">Types of Version Control Systems</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670262548369/b8BOx3Hmn.png" alt /></p>
<ul>
<li><p><strong>Local version control systems:</strong></p>
<p>Many people's version-control method of choice is to copy files into another directory. A common approach but very error-prone. It is easy to forget which directory you're in and accidentally write to the wrong file or copy over files you didn't mean to. To deal with this programmers developed local VCS that had a simple database that kept all changes to the file under revision.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670262964618/ySS7CE0dB.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p><strong>Centralized version control systems:</strong></p>
<p>The next major issue people encountered is that they needed to collaborate with developers on other systems. To deal with this CVCS was developed having a <strong>single server</strong> that contains all the versioned files and the history of what everyone on the project is doing. However, this setup has some serious downsides. What if the centralized server faces failure or what if the hard disk on the central database becomes corrupted? You lose absolutely everything.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670263834807/OrcsI3VeZ.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Distributed version control systems:</strong></p>
<p>This is where DVCS steps in! In a DCVS, users don't just check out the latest snapshot of the files; rather, they fully mirror the repository (folder), including its full history. Thus, if the server dies, and these systems were collaborating via that server, any of the user's repositories can be copied back to the server to restore it.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670264550368/VwJ89mjD-.png" alt /></p>
<h1 id="heading-what-is-git">What is Git?</h1>
<p>Try to absorb this section effectively, as it will probably be much easier to grasp the further concepts.</p>
<p>Git is a distributed version control system developed by <a target="_blank" href="https://en.wikipedia.org/wiki/Linus_Torvalds">Linus Torvalds</a>. It is an open-source project initiated in 2005.</p>
<p>We will now look at some feature which makes Git a one of its kind technology!</p>
<ol>
<li><p><strong><mark>Snapshots, Not differences</mark></strong></p>
<p>The major difference between Git and other VCS(CVS, Subversion, Perforce etc) is the way Git thinks about data. These other systems think of the information they store as a set of files and the changes made to each file over time.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670265278810/uJ-SN_9dL.png" alt="storing data as changes to a base version of each file." class="image--center mx-auto" /></p>
<p>Git doesn't think of data this way. Instead, it thinks of data more like a series of snapshots of a miniature filesystem. With Git, every time you commit or save the state of your project, it basically takes a picture of what all your files looked like at that moment and stores a reference to that snapshot. More like a <strong>stream of snapshot</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670269423840/dpLulnfiq.png" alt /></p>
<p><strong>s</strong></p>
<ol>
<li><p><strong><mark>Nearly every operation is local</mark></strong></p>
<p>Most operations in Git only need local files and resources to operate. Since the whole codebase and history of your project is available on your local machine, the Git operations are executed in no time.</p>
<p>This also means your work is not hindered if you are offline. If you are travelling somewhere and you wish to get some work done, you can do so without worries as all the magic happens locally. How cool is that!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670265876620/TYYsKjLJs.gif" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong><mark>Git has Integrity</mark></strong></p>
<p>Everything in Git is checksummed before it is stored and is then referred to by that checksum. This means it's impossible to change the content of any file/directory without git knowing it. You can't lose information in transit or get file corruption without Git being able to detect it. It uses the <strong>SHA-1 hash checksumming</strong> method. It's a 40-character string composed of hexadecimal characters which looks something like this:</p>
</li>
</ol>
<pre><code class="lang-plaintext">24b9da6552252987aa493b52f8696cd6d3b00373
</code></pre>
<p>In fact, Git stores everything in its database not by file name but by the value of its content.</p>
<ol>
<li><p><strong><mark>The three states</mark></strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670269968833/UCmyubC7m.png" alt /></p>
<p>Git has three main states that your file can reside in Modified, Staged and Committed.</p>
<ul>
<li><p>Modified means that you have made some changes in the file, but Git has not yet tracked those changes</p>
</li>
<li><p>Staged means that you have tracked the changes in the modified file using Git and are ready for a commit snapshot</p>
</li>
<li><p>Committed means that the data is safely stored in your local database.</p>
</li>
</ul>
</li>
</ol>
<pre><code>The basic Git workflow goes something like <span class="hljs-built_in">this</span> :

<span class="hljs-number">1.</span>  You modify files <span class="hljs-keyword">in</span> your working tree.

<span class="hljs-number">2.</span>  You selectively stage those changes you want to be part <span class="hljs-keyword">of</span> your next commit, which adds only those changes to the staging area.

<span class="hljs-number">3.</span>  You <span class="hljs-keyword">do</span> a commit, which takes the file <span class="hljs-keyword">as</span> they are <span class="hljs-keyword">in</span> the staging area and stores that snapshot permanently <span class="hljs-keyword">in</span> your Git directory.
</code></pre><p>To summarize, If a particular version of a file is in the Git directory, it's committed. If it has been modified and added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified.</p>
<h3 id="heading-if-you-have-come-this-far-i-really-appreciate-your-effort-so-leave-with-a-smile-on-your-face">If you have come this far, I really appreciate your effort. So, leave with a smile on your face:</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670267313488/iP5V5rhLV.png" alt /></p>
<p>References:</p>
<ol>
<li>Pro Git by Scott Chacon and Ben Straub, Second edition</li>
</ol>
<p>Stay tuned for more articles on this topic.😎</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to the Voyage]]></title><description><![CDATA[Begin with the end in mind -Dr. Stephen R. Covey

Hi. I welcome you to this series where I will be documenting my journey learning one of my favourite programming languages i.e C++. Although I had studied C++ before in my classes 11 and 12 as a compu...]]></description><link>https://www.somanshv.com/introduction-to-the-voyage</link><guid isPermaLink="true">https://www.somanshv.com/introduction-to-the-voyage</guid><category><![CDATA[programming languages]]></category><category><![CDATA[C++]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[programmer]]></category><dc:creator><![CDATA[Somansh V]]></dc:creator><pubDate>Sun, 04 Dec 2022 18:49:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670177118475/0JgMKVV3H.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>Begin with the end in mind -<strong>Dr. Stephen R. Covey</strong></p>
</blockquote>
<p>Hi. I welcome you to this series where I will be documenting my journey learning one of my favourite programming languages i.e <strong>C++.</strong> Although I had studied C++ before in my classes 11 and 12 as a computer science student, I was not exposed to the world of creating projects, community contributions or any of it. We were just thought from the textbook and the lab sessions were done in TurboC++ 🤧.</p>
<p>As you can see the teaching pedagogy was outdated. It was only when I entered college that I realized tools such as VScode, Git and GitHub etc which make developer's work easy and efficient.</p>
<p>So, that is why <strong>this series</strong> will try to introduce beginners to efficient coding tools and the latest practices all the while learning the programming language C++.</p>
<p>I will be writing about basic to advanced C++ all the while building some cool projects to get hands-on learning. Topics including variables, data types, flow of control, pointers, and data structures will be written. I will be executing programs on each concept and everything related to this series will be shared in this <a target="_blank" href="https://github.com/som-ansh/CPP_Scripts">GitHub Repo</a>. Feel free to look into the repository and any contributions are welcome!</p>
<p>For my journey I will primarily use these textbooks and online resources :</p>
<ul>
<li><p><strong>Computer Science with C++</strong> by Sumita Arora, 9th edition (although outdated, its very beginner friendly)</p>
</li>
<li><p><strong>Object Oriented Programming with C++</strong> by Reema Thareja, Revised first edition</p>
</li>
<li><p>Freecodecamp's Youtube Tutorials.</p>
</li>
</ul>
<p>That is it for this article. I have kept a goal of 3 articles per week😎. I am so excited to share this journey with you and looking forward for your support. Let's Begin.!🗿</p>
]]></content:encoded></item></channel></rss>