Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:39:22+00:00 2026-05-30T12:39:22+00:00

I want to use the traditional C-style for loop in Python. I want to

  • 0

I want to use the traditional C-style for loop in Python. I want to loop through characters of a string, but also know what it is, and be able to jump through characters (e.g. i =5 somewhere in the code).

for with range doesn’t give me the flexibility of an actual for loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:39:24+00:00Added an answer on May 30, 2026 at 12:39 pm

    There is no simple, precise equivalent of C’s for statement in Python. Other answers cover using a Python for statement with a range, and that is absolutely what you should do when possible.

    If you want to be able to modify the loop variable in the loop (and have it affect subsequent iterations), you have to use a while loop:

    i = 0
    while i < 7:
        if someCondition(i):
            i = 5
        i += 1
    

    But in that loop, a continue statement will not have the same effect that a continue statement would have in a C for loop. If you want continue to work the way it does in C, you have to throw in a try/finally statement:

    i = 0
    while i < 7:
        try:
            if someCondition(i):
                i = 5
            elif otherCondition(i):
                continue
            print 'i = %d' % i
        finally:
            i += 1
    

    As you can see, this is pretty ugly. You should look for a more Pythonic way to write your loop.

    UPDATE

    This just occurred to me… there is a complicated answer that lets you use a normal Python for loop like a C-style loop, and allows updating the loop variable, by writing a custom iterator. I wouldn’t recommend this solution for any real programs, but it’s a fun exercise.

    Example “C-style” for loop:

    for i in forrange(10):
        print(i)
        if i == 3:
            i.update(7)
    

    Output:

    0
    1
    2
    3
    8
    9
    

    The trick is forrange uses a subclass of int that adds an update method. Implementation of forrange:

    class forrange:
    
        def __init__(self, startOrStop, stop=None, step=1):
            if step == 0:
                raise ValueError('forrange step argument must not be zero')
            if not isinstance(startOrStop, int):
                raise TypeError('forrange startOrStop argument must be an int')
            if stop is not None and not isinstance(stop, int):
                raise TypeError('forrange stop argument must be an int')
    
            if stop is None:
                self.start = 0
                self.stop = startOrStop
                self.step = step
            else:
                self.start = startOrStop
                self.stop = stop
                self.step = step
    
        def __iter__(self):
            return self.foriterator(self.start, self.stop, self.step)
    
        class foriterator:
    
            def __init__(self, start, stop, step):
                self.currentValue = None
                self.nextValue = start
                self.stop = stop
                self.step = step
    
            def __iter__(self): return self
    
            def next(self):
                if self.step > 0 and self.nextValue >= self.stop:
                    raise StopIteration
                if self.step < 0 and self.nextValue <= self.stop:
                    raise StopIteration
                self.currentValue = forrange.forvalue(self.nextValue, self)
                self.nextValue += self.step
                return self.currentValue
    
        class forvalue(int):
            def __new__(cls, value, iterator):
                value = super(forrange.forvalue, cls).__new__(cls, value)
                value.iterator = iterator
                return value
    
            def update(self, value):
                if not isinstance(self, int):
                    raise TypeError('forvalue.update value must be an int')
                if self == self.iterator.currentValue:
                    self.iterator.nextValue = value + self.iterator.step
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the Web Browser control within an mono application, but when
I'm using YUI framework to style my radiobuttons and want to use the oncheckedchanged
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components
I want to use the functions exposed under the OpenGL extensions. I'm on Windows,
I want to use the Publish.GacRemove function to remove an assembly from GAC. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.