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 6672205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:28:50+00:00 2026-05-26T03:28:50+00:00

Today while i was coding , I encounter something which I didn’t expect it

  • 0

Today while i was coding , I encounter something which I didn’t expect it to happen in this way.Below is just an example code for the problem.

The Code 1

while(true)
{
 int i = 1;

 printf("This is %d\n" , i);
 ++i;
 if(i == 10)
   break;
}

The Code 2

for(int i = 1 ; ; i++)
{
 printf("This is %d\n" , i);
 if(i == 10)
    break;
}

The Question :

1.)The first code would causes infinite loop while the latter is not.

2.)I don’t understand , there’s a standard mention variable declare inside while loop can be accessed by any statement inside the while loop , but why the if() can’t access the value of variable i while the latter can??

Thanks for spending time reading my question

  • 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-26T03:28:51+00:00Added an answer on May 26, 2026 at 3:28 am

    It’s very simple:

    for(int i = 1 ; ; i++)
    {
        printf("This is %d\n" , i);
        if (i == 10)
            break;
    }
    

    is equivalent to

    {
        int i = 1;
        while (true)
        {
            printf("This is %d\n" , i);
            if (i == 10)
                break;
            i++;
        }
    }
    

    I.e., the int i = 1 part is executed before the first iteration of the for-loop. for introduces an implicit extra scope block holding any variables declared in the X of for (X;Y;Z).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While coding today, I noticed something odd with timespans and formatting strings. I was
Today while writing some Visual C++ code I have come across something which has
This is a challenging one that got me stumped while I was coding today.
While reading proggit today, I came upon this comment in a post about how
I've experienced something today while I'm building my app. I've declared a protocol in
I found this today while looking at a library for an API . def
Today, while talking with my colleague, something odd came out of his mind. A
Got this error today while trying to build a project with checkstyle. This works
I came across this piece of code today while tutoring some students in a
Today, while I was trying to write code to just add and subtract the

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.