Computer Science

For Loop in C

A for loop in C is a control flow statement that allows for repeated execution of a block of code. It consists of three parts: initialization, condition, and increment/decrement. The loop continues to execute as long as the condition is true. This construct is commonly used for iterating through arrays, performing calculations, and implementing other repetitive tasks in C programming.

Written by Perlego with AI-assistance

3 Key excerpts on "For Loop in C"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Using LEDs, LCDs and GLCDs in Microcontroller Projects
    • Dogan Ibrahim(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)

    ...The general format of this statement is: where expression 1 sets the initial value of the loop count and this variable is evaluated just once on entry into the loop. Expression 2 is the condition that keeps the loop continuing. This expression is evaluated at each iteration, to check whether or not the loop should continue. Expression 2 is usually a logical or relational operator. Expression 3 is also evaluated at the end of each iteration and this expression changes value that is tested for loop termination. Expression 3 is usually an increment of the loop counter value. The for loop is commonly used with more than one statement. An example is given below, where the loop is repeated 10 times. The initial value of the loop counter i is 0, it is incremented by 1 at each iteration, and the loop continues as long as i is less than 10: Some examples of using the for statement are given below. Example 3.7 Write a program to calculate the squares of numbers between 1 and 49 and store the results in an integer array called Mult. Solution 3.7 The required program is given below. The for loops can easily be nested, as shown in the following example. Here, all elements of a two-dimensional array MyArray are initialised to 0 (assuming that the array dimension is 10). In this example, the inner loop is repeated 10 times for each i value of the outer loop, that is the total number of iterations is 100: The parameters in a for loop can be omitted. There are several variations of the for loop, depending upon which parameter is omitted. Some examples are given below: To create an infinite loop: Setting the initial loop condition outside the loop: To terminate the loop from inside the loop: 3.2.18.7 Premature Termination of a Loop There are cases where we may want to terminate a loop prematurely. This is done using the break statement. When the program encounters this statement, it forces an unconditional jump to outside the loop...

  • TradeStation Made Easy!
    eBook - ePub

    TradeStation Made Easy!

    Using EasyLanguage to Build Profits with the World's Most Popular Trading Software

    • Sunny J. Harris(Author)
    • 2011(Publication Date)
    • Wiley
      (Publisher)

    ...Chapter 11 Loops In This Chapter Introduction For While INTRODUCTION A loop is just as it sounds; it goes around and around until you tell it to stop. A loop defines a group of instructions that will continue executing until a condition is met. The condition being met will cause the loop to stop. A FOR loop will execute a predefined number of times. The predefined number of times is the condition. A WHILE loop will continue to execute while (as long as) the condition is true. Loops allow you to calculate something over a range of values, such as from 1 to 10. Or to take action only while a condition is met, such as: while it is the month of October, only take short trades. Loops allow you to calculate averages, and summations and even count backward. As your programming skills become more proficient, you will find yourself discovering more and more uses for loops. FOR TEMPLATE 1 FOR Value1 = m TO n BEGIN … END; TEMPLATE 2 FOR Value1 = m DOWNTO n BEGIN … END; The FOR command always appears with a BEGIN … END block and is sometimes referred to as a FOR … BEGIN command. The FOR command is always followed by a set of variables that define the starting and ending number of steps to execute the loop. This structure is pretty complex until you get accustomed to it; then it will become second nature, so don’t start worrying yet. Templates for these concepts are shown above. You will specify m and n, and provide code for your task in place of the... s. FOR is always followed by a variable name (Value1 in Figure 11.1). The command tells the computer to perform a set of actions a specified number of times. The variable counts the number of times as it steps through the loop...

  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    ...However, conditionals can also be modeled effectively through the use of flowcharts or unplugged programming activities. Loops Loops are a mainstay of programming, allowing far more efficiency in programming tasks that must be repeated until a desired result is achieved. For example, if a programmer wants the program to perform a task repeatedly a certain number of times but wants the user to determine that number at runtime, the code would utilize a “for-loop”, accepting user input to set the number of intervals. In Scratch terminology, the coder could use an “ask” block to get user input on how many times the sprite should follow a certain sequence. In that case, the code would contain a “repeat” block to encapsulate the loop containing the blocks in the sequence. This not only prevents the need for repetitive code, but also avoids restricting the loop to some predetermined maximum number of iterations. While loops provide additional power and flexibility to the programmer, they also present a source of potential errors if not used properly. Research has shown the use of endless loops in the Scratch environment as a source of many programming errors, where programs get stuck in never-ending loops (Zhang & Nouri, 2019). Varied understanding of different types of loops, particularly nested loops, have often caused confusion, but this confusion has been less prevalent for Scratch users as compared to text-based languages, since loops are easier to identify in a block-based environment (Mladenovi et al., 2017). Perhaps the best analogy for loops in math is summation notation (∑ 2 i, e.g., meaning the summation of all values of 2 i, starting with 1 and ending with 10); this concept is usually not covered until secondary school...