Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.

Basic Loops

View previous topic View next topic Go down

Basic Loops

Post by Rachael_C on September 13th 2014, 01:21

A loop statement allows us to execute a statement or group of statements multiple times

Loops in C++ are mainly of three types :-

1. 'while' loop -     
Repeats a statement or group of statements while a given condition is true. It tests the 
                          condition before executing the loop body.
2. 'do while' loop - 
Like a while statement, except that it tests the condition at the end of the loop body
3. 'for' loop -         
Execute a sequence of statements multiple times and abbreviates the code that manages the 
                           loop variable.

FOR - for loops are the most useful type. The syntax for a for loop is:
[You must be registered and logged in to see this image.]

a.The variable initialization allows you to either declare a variable and give it a value or give a value to an already     existing variable. 
b.Second, the condition tells the program that while the conditional expression is true the loop should continue to       repeat itself. 
c.The variable update section is the easiest way for a for loop to handle changing of the variable.

Example:
[You must be registered and logged in to see this image.]
This program is a very simple example of a for loop. 
x is set to zero, while x is less than 10 it calls cout<< x << endl ; and it adds 1 to x until the condition is met. 
Keep in mind also that the variable is incremented after the code in the loop is run for the first time.  

WHILE - WHILE loops are very simple. The basic structure is:
[You must be registered and logged in to see this image.]

Example:
[You must be registered and logged in to see this image.]
This was another simple example, but it is longer than the above FOR loop. 
The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block. 

For both examples you will get:
[You must be registered and logged in to see this image.]

DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once. The structure is:
[You must be registered and logged in to see this image.]

The condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. 
If the condition is true, we jump back to the beginning of the block and execute it again.

Example:
[You must be registered and logged in to see this image.]
Keep in mind that you must include a trailing semi-colon after the while in the above example.
Your program should look like:
[You must be registered and logged in to see this image.]

Rachael_C
Beginner
Beginner

Posts : 13
Points : 26201
Reputation : 0
Join date : 2014-04-27

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum