Home » Python

Emulate a do-while loop in Python

Python do while loop: Since, python does not support do-while, here we will emulate a do-while loop and will implement similar in Python.
Submitted by Sapna Deraje Radhakrishna, on February 01, 2020

Python as a language doesn't support the do-while loop. However, we can have a workaround to emulate the do-while loop.

The syntax for do-while is as follows,

    do {
          loop statements;
    } while (condition);

Consider the following example implementation of 'do-while' in Java,

public class DoWhile {

    public static void main(String[] args) {
        int i = 5;
        do {
            System.out.println("Welcome to Include Help");
            i++;
        } while (i < 10);
    }
}

Output

Welcome to Include Help
Welcome to Include Help
Welcome to Include Help
Welcome to Include Help
Welcome to Include Help

The above example can be emulated in python as follows,

i=5
while True:
  print("Welcome to Include Help")
  i = i +1
  if(i>10):
    break

Output

Welcome to Include Help
Welcome to Include Help
Welcome to Include Help
Welcome to Include Help
Welcome to Include Help
Welcome to Include Help





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.