Unit 2 Repetition in Programs - C Programming PDF
Document Details
Tags
Summary
This document discusses repetition in programs, focusing on counting loops and the while statement in C. The document provides examples of for loops, including initialization, conditionals, and increments, and demonstrates how to print numbers from 1 to 10.
Full Transcript
UNIT 2 Repetition in Programs, Counting Loops, and the while Statement Repetition, or iteration, is a fundamental concept in programming where a set of instructions is executed repeatedly based on a condition. This is essential for tasks that require repeated actions, such as counting, summing a se...
UNIT 2 Repetition in Programs, Counting Loops, and the while Statement Repetition, or iteration, is a fundamental concept in programming where a set of instructions is executed repeatedly based on a condition. This is essential for tasks that require repeated actions, such as counting, summing a series of numbers, or iterating over arrays. Counting Loops A counting loop is a type of loop that runs a specific number of times. It often uses a counter variable to keep track of iterations. The for Loop in C The for loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly. The syntax of the for loop includes initialization, condition, and increment/decrement expressions, all in one line. Syntax for (initialization; condition; increment) { // Code to be executed } Initialization: Initializes the loop control variable. Condition: The loop continues as long as this condition is true. Increment: Updates the loop control variable after each iteration. Example: Printing Numbers from 1 to 10 Below is a simple C program that uses a for loop to print numbers from 1 to 10: #include void main() { // Using a for loop to print numbers from 1 to 10 for (int i = 1; i