Loops in C...?
Skip to content
Courses
Tutorials
Jobs
Practice
Write
Write
Come write articles for us and get featured
Practice
Practice
Learn and code with the best industry experts
Premium
Premium
Get access to ad-free content, doubt assistance and more!
Jobs
Jobs
Come and find your dream job with us
Geeks Digest
Quizzes
Geeks Campus
Gblog Articles
IDE
Campus Mantri
Sign In
Sign In
Home
Saved Videos
Courses
For Working Professionals
LIVE
Self-Paced
For Students
LIVE
Self-Paced
School Courses
Algorithms
Analysis of Algorithms
Data Structures
Interview Corner
Languages
ML & Data Science
CS Subjects
GATE
Web Technologies
Software Designs
School Learning
Mathematics
Maths Notes (Class 8-12)
NCERT Solutions
RD Sharma Solutions
Physics Notes (Class 8-11)
Chemistry Notes
CS Exams/PSUs
ISRO
UGC NET
Student
Post a Job
Curated DSA Lists
Tutorials
Jobs
Practice
GBlog
Puzzles
What's New ?
Change Language
Data Structures
Algorithms
Interview Preparation
Topic-wise Practice
C++
Java
Python
Competitive Programming
Machine Learning
HTML
SDE Sheet
Puzzles
GFG School
Projects
▲
Related Articles
Python Lists
Python Tuples
Python Sets
Python Dictionary
Python Arrays
Control Flow in Python
loops in python
Loops and Control Statements (continue, break and pass) in Python
range() vs xrange() in Python
Using Else Conditional Statement With For loop in Python
Iterators in Python
Iterator Functions in Python | Set 1
Python __iter__() and __next__() | Converting an object into an iterator
Python | Difference between iterable and iterator
When to use yield instead of return in Python?
Generators in Python
Python Functions
Returning Multiple Values in Python
Python Operators
Ternary Operator in Python
Operator Overloading in Python
Python | a += b is not always a = a + b
Difference between == and is operator in Python
Python | Set 3 (Strings, Lists, Tuples, Iterations)
Python String
Python Lists
Python Tuples
Python Sets
Python Dictionary
Python Arrays
Control Flow in Python
loops in python
Loops and Control Statements (continue, break and pass) in Python
range() vs xrange() in Python
Using Else Conditional Statement With For loop in Python
Iterators in Python
Iterator Functions in Python | Set 1
Python __iter__() and __next__() | Converting an object into an iterator
Python | Difference between iterable and iterator
When to use yield instead of return in Python?
Generators in Python
Python Functions
Returning Multiple Values in Python
Python Operators
Ternary Operator in Python
Operator Overloading in Python
Python | a += b is not always a = a + b
Difference between == and is operator in Python
Python | Set 3 (Strings, Lists, Tuples, Iterations)
Python String
loops in python
Difficulty Level : Easy
Last Updated : 22 Jun, 2022
Python programming language provides the following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.
While Loop:
In python, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.
Syntax :
while expression:
statement(s)
3. All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Example:
Python
# Python program to illustrate
# while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
Using else statement with while loops: As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed.
The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.
If else like this:
Python
if condition:
# execute these statements
else:
# execute these statements
and while loop like this are similar
Python
while condition:
# execute these statements
else:
# execute these statements
Python
#Python program to illustrate
# combining else with while
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
else:
print("In Else Block")
Output:
Hello Geek
Hello Geek
Hello Geek
In Else Block
Single statement while block: Just like the if block, if the while block consists of a single statement then we can declare the entire loop in a single line as shown below:
Python
# Python program to illustrate
# Single statement while block
count = 0
while (count == 0): print("Hello Geek")
Note: It is suggested not to use this type of loops as it is a never ending infinite loop where the condition is always true and you have to forcefully terminate the compiler.
for in Loop: For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i
Comments
Post a Comment