CPLUSPLUS Loops

CPLUSPLUS Loops

loops in C++

The CPLUSPLUS Loops have made programmer’s live too easier.The main purpose of loops is to do something for programmer’s point of view recursively using one thing many times and it is also known as recurssion. Before talking much about them, let’s discuss their types and functionality

For Loops

For loops are used to perform the task for user defined number of times. This is how a for loop is written:

for(initialization; condition; increment or decrement)

For example

#include “stdafx.h”
#include
using namespace std;

int main ()
{
// initialization ; condition ; increment
for( int a = 1; a < 16; a++ ) { cout << a << endl; } return 0; }

Output
1
2
3
4
5

While loops

While loops are used in those situation where our old technique of incrementing up to the certain number with for loop is not needed. Example is: We have a program about dodging car game. Read this clearly ; we don’t know when the fuel will burn because car accidents can occur. Here is the beauty of while loop that it tells the user or gamer that a fuel is low with any condition such as car crashed and fuel spread on the roads, running for miles etc. Here we are not certain when the fuel will get consumed. While is so simple to use

Syntax : while(condition)

Example

# include “stdafx.h”
#include
using namespace std;

int main()
{

int flag=1;

while(flag<10) { cout<< flag; cout<< endl; flag++; } }

Output
1
2
3
4
5
6
7
8
9

Do While loop

The main difference between while and do while is that: Do while executes at least one line of code even it is the indication of termination but While loop does read any line if the condition is not satisfied

#include “stdafx.h”
#include
using namespace std;

int main()
{
int a =1;

do
{

cout << a <

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.