Basics of Programming :- Loops and Conditions

With examples

ยท

2 min read

Basics of Programming :- Loops and Conditions

Designing the algorithm is one of the jobs of a programmer.

The algorithm is nothing but the sequence of program execution.

Conditional statements help us to take various decisions on the go while designing the algorithm.

    If my cat is hungry, then she will rub my leg.
    If a polygon has exactly four sides, then it is a quadrilateral.

These are some of the examples of conditional statements, we use something similar in the Programming as well.

if (the number is even) then print "Even number"

else print "Odd number"

it is one example of a conditional statement where we check whether the number is even or odd.

We can code it using different programming languages.

Suppose there is a requirement to write certain instructions repetitively then we use loops,

Let's understand by using an example:- Suppose your are programing a bot, say Panzer , to navigate through a known path in a maze. The directions for getting to the end is

  1. Take a right
  2. Go Down
  3. Take a right
  4. Go Down
  5. Take a Right
  6. Go Down
  7. Take a right
  8. Go Down

And you have reached the end. ๐Ÿฅณ
But what if the maze is of 1000 steps, what will you do ?
Write the program/instructions 1000 time

NO

Here we use the loops to perform this repetitive task.

For 4 times :
   Go right
   Go down
End

Untitled.png

See in the example we used a loop to perform 4 sets of right and down moves.

We can code this similar thing in any programming language.

I tried to give you a sense of conditionals and loops because Programming is learning how to think instead of writing code.

See how easy it is to understanding and learn to Program... ๐Ÿ˜

For more such posts don't forget to follow...

BY :- Puneesh Gogia

Click here to get redirected to Puneesh Writes

ย