for Loop in C - C Programming Tutorial 49

preview_player
Показать описание
Notes for You:: for Loop in C - C Programming Tutorial 49
- is Entry control loop.
- Computer executes body of the for loop, as long as the given conditional expression evaluates to true.
- If the given conditional expression evaluates to false then it terminates the loop.

Syntax for Loop in C:
// head of the for loop
for(initialization; conditional expression; increment/decrement)
{
statement(s); // body of the for loop
}
where:
Initialization part:
- is a place where we initialize one or more counter variables.
Conditional expression part:
- is a place where we write a conditional expression, which determines whether the body of the for loop is executed or terminated.
Increment / decrement part:
- is a place where we increment or decrement initialized counter variables.

Example Code:
- Program to display Hello World 5 times using for loop.
int counter=0;
for(counter=0; counter<5; counter++ ) // head of the for loop
{
printf("Hello World\n"); // body of the for loop
}

Note:
- replace < with less-than symbol.

Note:
- Head of the for loop must be handled carefully so that it does not enter into an infinite loop.
- Initialization, conditional expression, increment or decrement parts are optional.
- If no statement is written in the head of the loop then it is considered as an infinite loop.

Variation 1:
int counter=0
for( ; counter<5; counter++ )
{
printf("Hello World\n");
}

Variation 2:
int counter=0
for( ; counter<5; )
{
printf("Hello World\n");
counter++;
}

Variation 3:
for( ; ; )
{
printf("Hello World\n");
}

Note:
- replace < with less-than symbol.

Note:
- When we know exactly how many # of times the body of the loop is executed, we use for loop.
- When we know exactly how many # of times set of statements need to be executed, we use for loop.

=========================================

Follow the link for next video:

Follow the link for previous video:

=========================================

C Programming Tutorials Playlist:

=========================================
Watch My Other Useful Tutorials:-

Computer Programming Fundamentals Playlist:-

C Practical LAB Exercises Playlist:-

C++ Tutorials Playlist:

=========================================

► Subscribe to our YouTube channel:

► Visit our Website:

=========================================
Hash Tags:-
#ChidresTechTutorials #CProgramming #CProgrammingTutorial
Рекомендации по теме
Комментарии
Автор

Answer the following questions: Let's see who gives the best answer
1. Explain for loop with syntax and example

ChidresTechTutorials