Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Do While loop Syntax
Next article icon

While loop Syntax

Last Updated : 14 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

While loop is a fundamental control flow structure (or loop statement) in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. Unlike the for loop, which is tailored for iterating a fixed number of times, the while loop excels in scenarios where the number of iterations is uncertain or dependent on dynamic conditions.

While-Loop-GeeksforGeeks

Table of Content

  • While loop Syntax in C/C++
  • Java While loop Syntax
  • While loop Syntax in Python
  • While loop Syntax in C#
  • While loop Syntax in JavaScript

The syntax of While loop varies slightly depending on the programming language, but the basic structure is similar across many languages.

while (condition) {     // Code block to be executed repeatedly as long as the condition is true }

Here’s a general overview of the Syntax of While loop:

  1. Condition: This is the condition that is evaluated before each iteration of the loop. If the condition is true, the loop body is executed; otherwise, the loop terminates.
  2. Loop Body: This is the block of code that gets executed if the condition is true.
  3. Update: Inside the loop body, there should be some logic to change the variables involved in the condition so that the loop eventually terminates. Otherwise, you risk creating an infinite loop.

While loop Syntax in C/C++:

Syntax:

while (condition) {     // Code block to be executed repeatedly as long as the condition is true }

Explanation of the Syntax:

  • In C and C++, the while loop executes a block of code repeatedly as long as the specified condition evaluates to true.
  • The loop first evaluates the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of While loop Syntax in C/C++:

C++
#include <iostream> using namespace std;  int main() {     int i = 0;     while (i < 10) {         cout << i << " ";         i++;     }     return 0; } 
C
#include <stdio.h>  int main() {     int i = 0;     while (i < 10) {         printf("%d ", i);         i++;     }     return 0; } 

Output
0 1 2 3 4 5 6 7 8 9 

Java While loop Syntax:

Syntax:

while (condition) {     // Code block to be executed repeatedly as long as the condition is true } 

Explanation of the Syntax:

  • In Java, the while loop operates similarly to C and C++.
  • It first evaluates the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of While loop Syntax in Java:

Java
/*package whatever //do not write package name here */  import java.io.*;  class GFG {     public static void main(String[] args)     {         int i = 0;         while (i < 10) {             System.out.print(i + " ");             i++;         }     } } 

Output
0 1 2 3 4 5 6 7 8 9 

While loop Syntax in Python:

Syntax:

while condition:     # Code block to be executed repeatedly as long as the condition is true 

Explanation of the Syntax:

  • Python's while loop follows a similar structure to other languages.
  • It begins by evaluating the condition specified after the while keyword.
  • If the condition is true, the code block indented below is executed.
  • After executing the code block, the loop returns to the while statement and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop ends.

Implementation of While loop Syntax in Python:

Python3
i = 0 while i < 10:     print(i, end=" ")     i += 1 

Output
0 1 2 3 4 5 6 7 8 9 

While loop Syntax in C#:

Syntax:

while (condition) {     // Code block to be executed repeatedly as long as the condition is true } 

Explanation of the Syntax:

  • In C#, the while loop behaves similarly to C, C++, and Java.
  • It starts by evaluating the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of While loop Syntax in C#:

C#
using System;  public class GFG {      static public void Main()     {         int i = 0;         while (i < 10) {             Console.Write(i + " ");             i++;         }     } } 

Output
0 1 2 3 4 5 6 7 8 9 

While loop Syntax in JavaScript:

Syntax:

while (condition) {     // Code block to be executed repeatedly as long as the condition is true } 

Explanation of the Syntax:

  • JavaScript's while loop follows the same pattern as other languages.
  • It evaluates the condition specified within the parentheses () before each iteration.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the loop continues; otherwise, it ends.

Implementation of While loop Syntax in JavaScript:

JavaScript
let i = 0; while (i < 10) {     console.log(i + " ");     i++; } 

Output
0  1  2  3  4  5  6  7  8  9  



Next Article
Do While loop Syntax

C

code_r
Improve
Article Tags :
  • Programming

Similar Reads

  • Do While loop Syntax
    Do while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop’s body, ensuring that the loop’s body is executed at least once. Table of Content Do While loop Synt
    5 min read
  • Rust - While Loop
    Loops in Rust come into use when we need to repeatedly execute a block of statements. Loops are also useful when we need to iterate over a collection of items. In Rust, we have various kinds of loops, including loops, while loops, and for loops. The while loop is the most common loop in Rust. The lo
    3 min read
  • Swift - While Loop
    Just like other programming languages, the working of the while loop in Swift is also the same. It is used to execute a target statement repeatedly as long as a given condition is true. And when the condition becomes false, the loop will immediately break and the line after the loop will execute. Wh
    2 min read
  • R - while loop
    While loop in R programming language is used when the exact number of iterations of a loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the whil
    5 min read
  • Solidity While Loop
    In Solidity, a while loop is a type of loop statement that allows you to execute a block of code repeatedly until a certain condition is met.  Syntax: while (condition) {    statement or block of code to be executed if the condition is True } The condition is an expression that is evaluated before e
    1 min read
  • SAP ABAP | While Loop
    A fundamental control structure in SAP ABAP is the while loop. The while loop in SAP ABAP can be used to iterate a part of code while the given condition remains true. This construct the while loop facilitates iterative running a block of code continues to execute until the specified condition withi
    3 min read
  • PL/SQL While Loop
    Oracle PL/SQL provides various loop structures that help developers execute a block of code multiple times based on certain conditions. The main loop structures include LOOP ... END LOOP, WHILE ... END LOOP, and FOR ... END LOOP. In this article, we will explore the WHILE loop in detail, including i
    5 min read
  • For loop Syntax
    For loop is a control flow statement in programming that allows you to execute a block of code repeatedly based on a specified condition. It is commonly used when you know how many times you want to execute a block of code. Table of Content For loop Syntax in C/C++For loop Syntax in JavaFor loop Syn
    5 min read
  • PostgreSQL - While Loops
    When working with PostgreSQL, knowing how to efficiently use loops can be essential for running iterative operations. PostgreSQL’s WHILE loop allows developers to repeatedly execute a block of code as long as a specified condition remains true. PostgreSQL provides the loop statement which simply def
    4 min read
  • Swift - Repeat While loop
    Sometimes there's a situation that comes when we have to execute a block of many numbers of times so to do this task we use the Repeat While loop. Or we can say that in Swift, we use the Repeat While loop to execute a block of code or a set of statements repeatedly. Repeat...while loop is almost sam
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences