Microcontroller Embedded C Programming Lecture 117| Looping in ‘C’

  • Post author:
  • Post category:Blog

 

Loops in C

 

 

 

Loops in C

First, let’s understand why exactly we need looping. 

Take a look at the below program to print numbers from 1 to 10.  

Loops in C
Figure 1. Program

 

On the left-hand side, you can see that I have used a couple of print statements with different arguments to achieve that goal. 

On the right-hand side, they write the same code in a smarter way, which saves your code space and avoids code redundancies. So, I can modify the same code using loop statements of ‘C’. It is looks a lot better than the without loop statement.

 

Looping helps you to execute a certain set of statements again and again until some condition is met.

So, usage of loop statements is very much common in programming, and use it generously whenever you need it in the programming. 

 

In ‘C’ there are 3 types of loop statements.

  1. while loop 
  2. for loop 
  3. do…while loop.

Here while, for, and do all these are reserved keywords of ‘C’ standard. 

 

  • while loop means, repeat execution of code inside the loop body until expression evaluates to false(0).  It only has one part: the condition.
  • do..while loop says, repeat the execution of code inside the “do” body until the expression evaluates to false(0).

The difference between a while loop and a do..while loop is,  do..while loop statements are always executed first and then the expression will be checked.

  • for loop is one of the famous and most widely used looping statements in ‘C’. The for loop is used to execute a block of code for a specified number of times. It has three parts: the initialization, condition, and increment/decrement.
  • Nested Loops: Loops can be nested, meaning a loop can contain another loop inside it. This allows for complex operations and algorithms.
  • Breaking Out of a Loop: The break statement can be used to break out of a loop and exit the loop immediately.
  • Continuing a Loop: The continue statement can be used to skip the current iteration of a loop and continue with the next iteration.
  • Infinite Loops: Infinite loops are loops that run indefinitely and can cause an application to freeze or crash. They should be avoided by ensuring a proper exit condition is provided.
  • Loop Control Variables: Loop control variables are used to control the number of iterations of a loop. They are usually incremented or decremented within the loop.
  • Use of Loops: Loops are widely used in programming for tasks such as iteration through arrays, performing repetitive operations, and controlling the flow of program execution.

Loops are an essential part of programming in C language and are used to simplify complex operations. Proper use of loops and control statements can improve the efficiency and accuracy of the code.

In the following article, let’s understand the while loop.

 

FastBit Embedded Brain Academy Courses

Click here:https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply