Skip to main content

C++ Loops (For, While and Do while)

                                                  The for statement

The C++ for statement has the following form:

Syntax:

for  (expression1;Condition;expression2)
	statement;

for  (expression1;Condition;expression2) {
	block of statements
}
Image result for c++ loops
Image result for c++ loops
expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);
NOTE: C/C++ basically treats for statements as while type loops

For loop example program:

/*  Example Program For for Loop In C++ Programming Language          */

// Header Files
#include<iostream> 
using namespace std;

//Main Function
int main()
{
     // Variable Declaration
    int x=3;

     //for loop
	for (x=3;x>0;x--)
	{
		cout<<"x="<<x<<endl;
	}
     //Main Function return Statement
     return 0;
}

Output:

x=3
x=2
x=1

                                   The while statement

The while statement is similar to those used in other languages although more can be done with the expression statement -- a standard feature of C.
The while has the form:

Syntax:

while (expression)
	statement;

while (expression)
	block of statements
}
Image result for c++ loops

While statement example program

/*  Example Program For while Loop In C++ Programming Language */

// Header Files
#include<iostream> 
using namespace std;

//Main Function
int main()
{
     // Variable Declaration
    int x=3;

     //while loop
	while (x>0)
	{
		cout<<"x="<<x<<endl;
		x--;
	}
 //Main Function return Statement
     return 0;
}

Output:

x=3
x=2
x=1

                          The do-while statement

Syntax:

C's do-while statement has the form:
do {
   statement;
}while (expression);
Image result for c++ loops
It is similar to PASCAL's repeat ... until except do while expression is true.

do while Loop example:

/*  Example Program For Do While Loop In C++ Programming Language */

// Header Files
#include<iostream>
using namespace std;

//Main Function
int main()
{
     // Variable Declaration
    int x=3;

     //do while loop
  	 do {
	   cout<<"x="<<x<<endl;
	   x--;
	 }while (x>0);
     //Main Function return Statement
     return 0;
}

outputs:

x=3
x=2
x=1
NOTE: The postfix x- operator which uses the current value of x while printing and then decrements x.

Comments

Popular posts from this blog

Introduction To C++

       Lets learn The C++ Programming Language C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.                    What is C++? “C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.” In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Srroutrup in 1979. Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today. Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.                             ...

Structure of Java Program

Structure of Java Program Structure of a java program is the standard format released by Language developer to the Industry programmer. Sun Micro System has prescribed the following structure for the java programmers for developing java application. A  package  is a collection of classes, interfaces and sub-packages. A sub package contains collection of classes, interfaces and sub-sub packages etc. java.lang.*; package is imported by default and this package is known as default package. Class  is keyword used for developing user defined data type and every java program must start with a concept of class. "ClassName"  represent a java valid variable name treated as a name of the class each and every class name in java is treated as user-defined data type. Data member  represents either instance or static they will be selected based on the name of the class. User-defined  methods represents either instance or static they are meant for performing t...