About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

Part 2 Control Structures in C++

Part 2 Control Structures in C++

Iteration Statements or Loops in C++

Hi this is Yuvraj from welearncode and welcome to the another C++ programming tutorial in this tutorial we will be discussing rest of the previous tutorial i.e, iteration statements or loops in C++ programming. In the previous tutorial you saw the concept of control structure i.e, Sequence logic, or sequential flow, Selection logic, or conditional flow etc.

The statements that cause a set of statements to be executed again and again either for a specific number of times or until some condition is satisfied are known as iteration statements. That is, as long as the condition is True, the set of statement(s) is executed. The various kind of iteration statements used in C++ programming language are for loop, while loop and do while loop.

Loop is classified into two categories:

1. Entry Controlled Loop (for and while loop)

2. Exit Controlled loop (do-while loop)

1. for Loop:
The for loop is entry controlled loop and one of the most widely used loops in C++ programming. The for loop is a deterministic loop in nature, that is, the number of times the body of the loop or set of statements is executed is programmers known in advance.

The syntax of the for loop is

Example of the for loop is:


for loop using comma operator: for loop authorize multiple variables to control the loop using comma operator. That is, more than two variables can be used in the initialize and the inc/dec parts of the loop. For example, consider this statement.

for (num1=1,num2=50 ;num1<10;num1++, num2--)

This statement initializes two variables, namely num1 and num2 and inc/dec them. Note that for loop can always have only one condition.

2. While Loop:
The while loop is mostly used by the programmers to perform looping or repetitive operations in situations where the number of iterations is not known in advance. That is, alike the for loop, the while loop is non deterministic in nature and this is also entry controlled loop. 

The syntax of the while loop is

These points should be noted about the while loop:

• while loops do not specify any explicit initialize and update expressions or inc/dec. This implies that the control variable must be initialized and declared before the while loop and needs to be updated within the body of the while loop .

• The while loop executes as long as condition evaluates to True. If condition evaluates to False in the first looping, then the statements inside the while loop will never gets executes.

• while loop can have two or more than one expression in its condition. However, such multiple expressions must be separated by commas and are executed in the order of their appearance.

Example of the while loop is:

1 2 3 4 5

3. do-while loop: 
In a while loop, the condition is evaluated at the starting of the loop and if the condition is False then the body of the loop is not executed even once. However, if programmer needs to executes body of the loop at least once, if the initial state of the condition is either True or False, the do-while loop is used. This loop places the condition to be evaluated at the end of the loop. This loop is also called exit control loop.

The syntax of the do-while loops is given here:

Example of the do-while loop is:

1 2 3 4 5 

Post a Comment