Enhance Your C++ Skills with Conditional Statements and Loops

Vineet RajVineet Raj
5 min read

Hello readers in let’s deep dive for conditional statements and loops syntax is mostly same in all the programming language so no just try to understand the logic here

Conditional Statements

These are just few statements that says if x is true then only do y else no need to perform any action for example : First check percentile of JEE Main if it cleared the cutoff for JEE Advance then only think JEE Advance.

Yep that’s it nothing fancy here it just syntax and even that is same in modt of the programming language

If Else Statements

so let’s directly dive into syntax and then will explain the code

  bool flag = true; // just a boolean datatype

  if (flag)
  {
    cout << "Hello World"; // will only be printed if flag is true
  }
  else
  {
    cout << "Bye World";
  }

In above code we declared a boolean variable with name as flag and checking that if a flag is true then print “Hello World” otherwise it prints “Bye World“

Also you can write nested if else statements aka if-else-if-else

Here is an example of nested if else

 int marks = 64;

  if (marks >= 90)
  {
    cout << "Grade A" << endl;  // if marks is more then 90 then this block
  }
  else if (marks >= 70)
  {
    cout << "Grade B" << endl; // if marks is more then equalto 70 but lesserthen 90 then this block
  }
  else if (marks >= 50)
  {
    cout << "Grade C" << endl;  // ...
  }
  else if (marks >= 35)
  {
    cout << "Grade D" << endl;
  }
  else
  {
    cout << "Fail" << endl;  // if none of the above blocks are satisfied just run this block
  }

Now if else statements are surely robust and readable if written properly also if else statement can easily handle any range type condition bt if we know that we just need to do something at specific value (not a range means not greater then or lesser then 70 just 70.

Then we use something called Switch Case Statement.

Switch Case Statement

As name suggest they are just switches that turns on and does something and if it’s off it’s doing nothing no middle case right ?

Same here will dive right into syntax then explain it

  int day = 5;

  switch (day)
  {
  case 1:
    cout << "Monday" << endl;
    break;
  case 2:
    cout << "Tuesday" << endl;
    break;
  case 3:
    cout << "Wednesday" << endl;
    break;
  case 4:
    cout << "Thursday" << endl;
    break;
  case 5:
    cout << "Friday" << endl;  // only this block will run.
    break;
  case 6:
    cout << "Saturday" << endl;
    break;
  case 7:
    cout << "Sunday" << endl;
    break;

  default:
    cout << "Invalid Day";
    break;
  }

Wondering about why added break after each block try running this without break statements and then read further.

Hope you tried this so let’s discuss.

So in above example for day we have 8 code blocks in the switch case block so they show various cases or you can say various possible values of day or any expression like a*b + c in that switch statement at place of day.

And for each of those various discrete values of expression ( here a variable day ) we have separate 7 blocks representing 7 days

and now what about default case so it’s just like else if none of the above blocks run then default case will run here it says invalid day because there are only 7 possible day

Have you tried running the above code without break statements ?

If yes that’s amazing if not no worries will discuss it here

First of all what do you think or mean it when you hear “break” ? it like stopping a flow , making dividing something in smaller parts right ?

So just like that our c++ compiler take switch case block as a whole statement here so when we use break keyword in it it breaks the flow of that code.

Ahh may be it’s confusing !

So let’s keep it simple if you are not using break keyword then in above code it will print ”Friday” “Saturday” “Sunday” Invalid Day” as Friday case is satisfied and our compiler thinks it just one body it will execute all consecutive statements

But if we use break it explicitly says that hey break the flow after executing current block and move on to next portion of code out of switch case statement so it just prints “Friday”

Hope you are able to understand Switch Case Statement if not please reach out to me via comments or my social profile i will try my best to clear your doubt

Loops

Now think of doing a task like simple a + 1 guess you can do it easily now add 1 twice to a a + 1 + 1 cool and easy but now if we try to add 1 to a 1000 times it is easy as you have to add 1 but may take time but what if you have to perform complex operation on a

So to solve this n times problem we have Loops

So you have to keep three things in mind while writing a loop

  1. Condition when loop will stop Most important

  2. Make sure whatever expression you are providing as condition it must change it’s value else loop will go in infinite loop.

  3. What code you have to repeat.

For Loops

Just three important stuff remember ?

int a = 0;
for(int i = 0; i <  100;i++){
  a = a + 1;
}

Here

  1. Condition is i < 100, i starting from 0

  2. i is updating by one after each iteration

    • Here i++ means i = i + 1
  3. a = a + 1 is expression which will be repeated

Here loop will run 100 times and eventually 1 will be added to a 100 times


now will give you syntax only just explore how they satisfy above three condition

While Loop

  int a = 0;
  int i = 0;

  while (i < 100)
  {
    a = a + 1;
    i++;
  }

Do While Loop

 int a = 0;
 int i = 0;

  do
  {
    a = a + 1;
    i++;
  } while (i < 100);

Thank you for reading till here hope i cleared your most of the doubts related to Loops And Conditional Statements

0
Subscribe to my newsletter

Read articles from Vineet Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vineet Raj
Vineet Raj