Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Looping

By Sripriya R | on September 4, 2007 |
C++ Tutorials

In object-oriented programming language, whenever a block of statements has to be repeated a certain number of times or repeated until a condition becomes satisfied, the concept of looping is used.

The following commands used in C++ for achieving looping:

  • for loop
  • while loop
  • do-while loop

for loop:

The syntax of for loop is

  1. for (initialization; condition; increase or decrease) 
  2. {
  3. statement block;
  4. }

Initialization is the primary value set for a variable. Initialization is only executed one time. After initialization is performed, it is not executed again. The condition specified is checked and if the condition returns a value of true, (if the condition is satisfied), the loop is continued for execution. If the condition returns a value of false, (the condition is not satisfied), then the statements following the loop block are not executed and the control is transferred to the end of the loop. The statement specified in the for loop can be a statement block or more than one statement. These statements must be enclosed within flower brace symbols{ }. The statement block can also be a single statement. For a single statement, the flower brace symbols are not needed and may be specified as:

  1. for (initialization; condition; increase or decrease) 
  2. statement;

If the condition becomes satisfied, all the statements in the for block are executed, the increase or decrease is executed on the variable and again the condition is checked, so the loop continues with these commands until the condition becomes false.

It is important for the programmer to remember that the optional attributes are initialization and increase/decrease where the for loop must be written as:

  1. for (;condition;)

or as

  1. for(initialization ; condition;)

It is also possible to initialize more than one variable in a for loop and to perform this the programmer uses the comma operator denoted as “,”.

For example:

To initialize two variables namely a and b with value 5 and 10 respectively and increment a and decrement b this can be done as follows;

  1. for (a=5,b=10;a!=b; a++, b--)

Let us see a small example using for loop:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. for (int x=5; x>0; x--) 
  6. {
  7. cout << x << "; ";
  8. }
  9. cout << "EXFORSYS";
  10. }

The output of this program is

while loop:

Statement of while statement is:

  1. while (expression) 
  2. { 
  3.      statement block; 
  4. }

Until the condition represented in the expression is true (satisfied), the set of statements in while block is executed. As in for loop, when the number of statements to be executed is more than one (if the condition is satisfied), then it is placed inside the flower braces as shown in the example above. If the number of statements to be executed is one, if the condition is satisfied, then the flower braces may be removed.

For Example:

  1.  #include <iostream>
  2. using namespace std;
  3. void main ()
  4. {
  5. int x;
  6. cout << "Input the number:";
  7. cin >> x;
  8. while (x>0) 
  9. {
  10. cout << x << "; ";
  11. --x;
  12. }
  13. cout << "EXFORSYS";
  14. }

Here the output of the above program is

In this example, the input number is 5 and the while loop is executed starting from 5 until the number reaches a value <0.Whilethe value of x>0 the statement inside the while block prints the value of x. When this is completed, the numbers 5, 4, 3, 2, 1 are printed. After the value of x reaches less than zero, the control passes to outside the while block and EXFORSYS is printed.

do-while loop:

The syntax of do-while loop is

  1. do 
  2. { 
  3.   statement block; 
  4. } while (condition);

The do while loop functionality is similar to while loop. The main difference in do-while loop is that the condition is checked after the statement is executed. In do-while loop, even if the condition is not satisfied the statement block is executed once.

For Example:

Suppose a programmer wishes to output the input number until the user types the number 5. This can be done using a do..while loop structure as follows:

  1.  #include <iostream>
  2. using namespace std;
  3. void main ()
  4. {
  5. int x;
  6. do 
  7. {
  8. cout << "Input the number:";
  9. cin >> x;
  10. cout << "The number is:" << x << "n";
  11. } while (x != 5);
  12. cout << "End of program";
  13. }

The output of the above program is:

« « C++ Multidimensional Arrays
Can RFID Help Your Supply Chain? » »

Author Description

Avatar

Free Training

RSSSubscribe 401 Followers
  • Popular
  • Recent
  • C++ Decision Making Statements

    September 1, 2007 - 0 Comment
  • C++ Functions with Arguments

    September 21, 2006 - 0 Comment
  • Object Oriented Programming Paradigm

    June 10, 2006 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Type Conversions

    August 13, 2006 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment
  • Elements of Object Oriented Programming

    June 10, 2006 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Class Constructors and destructors

    August 17, 2006 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled