IIT Delhi COL 100 - Introduction to 'for' Loop in C

Unformatted text preview:

Introduction to for Loop in C Entry Controlled Loops In computer programming loops are used to repeat a block of code until a certain condition is met There are two types of loops in C entry controlled loops and exit controlled loops An entry controlled loop is a loop that checks the loop s condition before executing the block of code If the condition is false the block of code will not be executed The for loop is an example of an entry controlled loop Syntax and Flowchart of for Loop The syntax for a for loop in C is as follows for initialization condition increment decrement code to be executed in the loop The initialization statement is executed only once at the beginning of the loop It is used to initialize the loop counter The condition statement is checked before every iteration of the loop If the condition is true the code inside the loop is executed If the condition is false the loop is terminated The increment decrement statement is executed at the end of every iteration of the loop It is used to modify the loop counter Here is a flowchart for the for loop for loop flowchart Different Ways to Write for Loops Example 1 Count from 1 to 10 for int i 1 i 10 i printf d n i There are different ways to write the initialization condition and increment decrement statements in the for loop Here are some examples Example 2 Count down from 10 to 1 for int i 10 i 1 i printf d n i Example 3 Print the elements of an array int arr 1 2 3 4 5 for int i 0 i 5 i printf d n arr i Example 4 Read data from user int i n printf Enter the number of elements scanf d n for i 0 i n i printf Enter element d i 1 scanf d arr i


View Full Document

IIT Delhi COL 100 - Introduction to 'for' Loop in C

Download Introduction to 'for' Loop in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to 'for' Loop in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to 'for' Loop in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?