filmov
tv
for Loop in C || Lesson 35 || C Programming || Learning Monkey ||

Показать описание
for Loop in C
In this class, we will try to understand the for Loop in C.
We have already discussed the while loop and do-while loop.
Table of Contents
for Loop
for Loop Syntax
Initialization Expression
Control Expression
Increment/Decrement Expression
for Loop Example
General Constructs of the for Loop
for Loop
The for loops are the best choices to count up (Incrementing) and count down (Decrementing) the variable.
The for loop in C is frequently used.
For a better understanding of for loop in c let’s start with the syntax.
for Loop Syntax
The image below is the syntax for the for loop in C.
for loop in C Syntax
for loop in C Syntax
In the above image for is the keyword and open and closed parenthesis are used for the expressions or statements.
The expressions of the for loop in C are of three types.
Initialization Expression
Control Expression
Increment/Decrement Expression
Every statement is separated by a semi-colon. Which indicates the end of the expression or statement.
Initialization Expression
In the initialization statement, the variable will get initialized.
The initialization is done only at the first iteration of the for a loop.
Control Expression
This expression is going to control the iteration of the loop.
At every iteration, even in the first iteration after initialization, the control expression will get executed.
If the control expression happens to be true then the body of the loop will get executed.
If the control expression happens to be false we will come out of the body of the loop.
Increment/Decrement Expression
After executing the loop's body the Increment/Decrement of the variable is done.
Below the for is the loop's body enclosed within the open and closed flower parentheses.
The loops body contains the statements to e executed repeatedly.
Once the control expression evaluates to false the execution of the loop's body will stop.
for Loop Example
The image below is an example of for loop.
for loop in C Example
for loop in C Example
The above program is an example to print the integers from 1 to n.
The number of integers to be printed is provided by the user of the program and is stored in variable n.
Assume that the user has given the value of n as 5.
In the above program, i is the variable used to print the values.
The variable I initialized to 1 in the first iteration.
After the initialization, the control statement is checked.
This control statement is making the loop iterate for 5 times as the value of n is 5 given by the end-user.
If the control expression evaluates to true the body of the loop will get executed and print the value of i.
After executing the body of the loop the third expression increment of i is done.
This iteration repeats until the control expression evaluates to false.
General Constructs of the for Loop
The image below is the general constructs of the for loop to count up and count down.
General Construct for loop in C
General Construct of for loop in C
The first construct is to count up the variable for n times starting from zero to n-1.
As we are starting from zero and iterating for n times the value of the variable should be n-1.
Therefore i is initialized to zero and the control statement is in.
Similarly, the second construct is to count up the variable for n times from 1 to n.
In the same way, the count down of the variable can also be done by using 3rd and 4th constructs.
We suggest you practice more on the above constructs for better understanding.
#learningmonkey #cprogrammingforbeginners #placements #gatecse #cprogramming #cprogramminglanguage
Link for playlists:
In this class, we will try to understand the for Loop in C.
We have already discussed the while loop and do-while loop.
Table of Contents
for Loop
for Loop Syntax
Initialization Expression
Control Expression
Increment/Decrement Expression
for Loop Example
General Constructs of the for Loop
for Loop
The for loops are the best choices to count up (Incrementing) and count down (Decrementing) the variable.
The for loop in C is frequently used.
For a better understanding of for loop in c let’s start with the syntax.
for Loop Syntax
The image below is the syntax for the for loop in C.
for loop in C Syntax
for loop in C Syntax
In the above image for is the keyword and open and closed parenthesis are used for the expressions or statements.
The expressions of the for loop in C are of three types.
Initialization Expression
Control Expression
Increment/Decrement Expression
Every statement is separated by a semi-colon. Which indicates the end of the expression or statement.
Initialization Expression
In the initialization statement, the variable will get initialized.
The initialization is done only at the first iteration of the for a loop.
Control Expression
This expression is going to control the iteration of the loop.
At every iteration, even in the first iteration after initialization, the control expression will get executed.
If the control expression happens to be true then the body of the loop will get executed.
If the control expression happens to be false we will come out of the body of the loop.
Increment/Decrement Expression
After executing the loop's body the Increment/Decrement of the variable is done.
Below the for is the loop's body enclosed within the open and closed flower parentheses.
The loops body contains the statements to e executed repeatedly.
Once the control expression evaluates to false the execution of the loop's body will stop.
for Loop Example
The image below is an example of for loop.
for loop in C Example
for loop in C Example
The above program is an example to print the integers from 1 to n.
The number of integers to be printed is provided by the user of the program and is stored in variable n.
Assume that the user has given the value of n as 5.
In the above program, i is the variable used to print the values.
The variable I initialized to 1 in the first iteration.
After the initialization, the control statement is checked.
This control statement is making the loop iterate for 5 times as the value of n is 5 given by the end-user.
If the control expression evaluates to true the body of the loop will get executed and print the value of i.
After executing the body of the loop the third expression increment of i is done.
This iteration repeats until the control expression evaluates to false.
General Constructs of the for Loop
The image below is the general constructs of the for loop to count up and count down.
General Construct for loop in C
General Construct of for loop in C
The first construct is to count up the variable for n times starting from zero to n-1.
As we are starting from zero and iterating for n times the value of the variable should be n-1.
Therefore i is initialized to zero and the control statement is in.
Similarly, the second construct is to count up the variable for n times from 1 to n.
In the same way, the count down of the variable can also be done by using 3rd and 4th constructs.
We suggest you practice more on the above constructs for better understanding.
#learningmonkey #cprogrammingforbeginners #placements #gatecse #cprogramming #cprogramminglanguage
Link for playlists: