Cpp For Loop - Loops Sequence 3 - Chapter 23 - Cpp Programming

When we talk about C + + loops the simplest and smartest version of the C++ loop is For loop. C++ For loop works the same as a while and do-while loop

and repeat its body until a condition is not satisfied. The only difference between For loop and the Syntax of other looping structures is about the simplicity and

smart syntax. Here is the syntax of CplusPlus For loop. Show here syntax.

As shown in the For Loop Syntax that three statements 1. Counter variable initialization 2. condition and 3. increment and decrement in a counter variable are done in a single statement.

Looping structure as we know that there must be a

  • The counter variable must be initialized to some initial value.
  • This counter variable must be part of a logical condition.
  • And in the body of the loop, there must be a change in the value of a counter variable.

C++ For Loop

For loop is ideal in those types of looping scenarios in which we want to execute a statement or a set of statements for a known number of times. For example.

So, we want to traverse an array of hundred elements we can do it with a while loop and do-while loop

but the best and the smart code to traverse an array of hundred elements would be For loop as given below.

The above code and example of For loop show that for loop contains three statements the first is the initialization

statement. The second one is a logical condition and the third one is the increment in the counter variable associated with the loop of the body.

But we can skip the first and the last part of the for loop Syntax for example if we do not want to

initialize a variable inside the for loop we may skip this statement and put only ; (semi-colon) Before condition.

Similarly, if we do not want to put increment and decrement statements after the condition inside the following it may also be skipped.

and The use of ‘for’ loop in data structures manipulation reduce the line of quotes for a programmer and makes

the logic of a program easy. Like us on FB: TheCodeMaster

Leave a Reply

Your email address will not be published. Required fields are marked *