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

  • SEARCH
  • Home
  • 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 9284701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:58:12+00:00 2026-06-18T18:58:12+00:00

I have an issue understanding the do – while statement in C. Here is

  • 0

I have an issue understanding the “do – while” statement in C.

Here is the complete code: http://pastebin.com/uPRvRscd

The program generates 6 numbers ranged from 0 to 50.
None of the 6 numbers repeats.

This is the do-while loop:

for(c=0;c<BALLS;c++)
{
    /* See if a number has been allready been drawn */
    do
    {
        ball = rand() % RANGE; /* Generate the random ball */
    }
    while(numbers[ball]); /* How is this compare made ? */
    /* Number drawn */
    numbers[ball] = 1; /* What is this for ?!?  */
    printf("%2d ", ball+1); /* add 1 to ball so ball won't be zero */
}
  • How does the logical compare work ?

I know that the DO depends if the WHILE is true or false.

numbers[ball] = 1; What is this supposed to do ? ( if i remove it the result is the same )

Thank you

  • 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-06-18T18:58:13+00:00Added an answer on June 18, 2026 at 6:58 pm

    C has a rule that “anything that is zero” is false, everything else is true. So when you write if(x) it is the same as if (x != 0) and if(!x) means if (x == 0).

    Same wit conditions in for, while and do – while.

    So your code does:

    do
    {
        ball = rand() % RANGE; /* Generate the random ball */
    }
    while(numbers[ball] != 0);
    

    I assume that numbers is an array of 50, that is filled with zero [if it’s of static storage duration, then it’s automatically set to zero if nothing else is stated.]

    When a number has been drawn numbers[ball] = 1; sets that number to non-zero, so if we draw the same number again, the do-while loop will loop again and pick another number [we hope – if the random number generator is really rubbish, it may turn into an infinite loop]

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

Sidebar

Related Questions

I have an issue, while trying few code snippets i came across a code
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue while getting the file name with out extension from a
My fear is that I have a fundamental issue with understanding connection pooling in
I have another issue related to Views, and i've ended up here dealing with
I have an issue with the sliding doors technique here . The heading right
i have an issue with the understanding the arraylist which contain the objects from
I'm having some trouble understanding this issue. I have a class: class StringProperty {
I have run into an issue that is probably due to my mis-understanding of
I am having trouble understanding this issue - I have a sharded cluster in

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.