GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • C++ Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — While Loop in C++ - SyntaxDB - C++ Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Output
      • User Input
      • Commenting
      • Including Header Files
      • Namespaces
      • Read from Text File
      • Write to File
    • Variables
      • Local Variables
      • Constants
      • Array
      • Pointers
      • Structures
      • Type Definition
      • Vectors
      • Smart Pointers
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • While Loop
      • Do While Loop
    • Functions and Objects
      • Class Declaration & Structure
      • Constructor
      • Instantiation
      • Functions
      • Operator Overloading
      • Inheritance
      • Friend Function
      • Friend Class
      • Destructor
    • String Class
      • String Variables
Categories
Language
C++
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: C++14

Categories

  • User Program Communication
    • Output
    • User Input
    • Commenting
    • Including Header Files
    • Namespaces
    • Read from Text File
    • Write to File
  • Variables
    • Local Variables
    • Constants
    • Array
    • Pointers
    • Structures
    • Type Definition
    • Vectors
    • Smart Pointers
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • While Loop
    • Do While Loop
  • Functions and Objects
    • Class Declaration & Structure
    • Constructor
    • Instantiation
    • Functions
    • Operator Overloading
    • Inheritance
    • Friend Function
    • Friend Class
    • Destructor
  • String Class
    • String Variables

Control Flow

While Loop in C++

The while loop executes a block of code while a boolean expression evaluates to true. It terminates as soon as the expression evaluates to false. The boolean expression is evaluated before each iteration.


Syntax
while(booleanExpression) { 
    //block of code to be executed
}

Notes

Because the boolean expression is evaluated before each iteration, the block of code executes ONLY if the booleanExpression evaluates to true. If a guarantee is needed for the block of code to run at least once, use the do-while loop.

booleanExpression results in either a true or false output. It is created using comparing operators (==, >, =, <=, !=).

There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).


Example
using namespace std;

int timer = 0; 
while(timer < 10) { 
    cout << timer << endl;
    timer++; 
}

See Also
Documentation

Statements and flow control - C++ Tutorials

Add to Slack

< For Loop   |   Do While Loop >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.