Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help in For loops and while loops
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sbuhari




PostPosted: Wed Jul 27, 2011 12:28 pm   Post subject: Need help in For loops and while loops

OK here is the thing I have a while loop and I want to know how I can count the number of times the loop has occured.

here is my code:

while(CusId){
printf("Please Enter Your Customer ID Number Or 0 To Exit: ");
scanf("%d",&CusId);
if(CusId==0){
}
else{
printf("Please Enter Your Current Balance: ");
scanf("%d", &CurBal);
printf("Your New Charges: ");
scanf("%d", &NewChar);
printf("The Available Credit: ");
scanf("%d", &AvaCred);
printf("The Credit Limit: ");
scanf("%d", &CreLim);
}
}

please help me
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Wed Jul 27, 2011 1:51 pm   Post subject: RE:Need help in For loops and while loops

Do you know how to:
- Declare a variable and initialize it?
- Increment an integer?

Smile
sbuhari




PostPosted: Wed Jul 27, 2011 2:40 pm   Post subject: Re: Need help in For loops and while loops

yes I do incrementing involves for loops
Insectoid




PostPosted: Wed Jul 27, 2011 4:17 pm   Post subject: RE:Need help in For loops and while loops

Well, all you really need to do is make an int initialized to zero, and inside the while loop add 1.

c:

//the following loops function exactly the same
int i;
for (i = 0; i < 10; i++);

i = 0;
while (i<10){
    i++;
}

There is a nicer (well, cooler) way to do this though, with a for loop. See, a for loop doesn't have to look like the one above. A for loop has 3 parts- an initialization (i = 0), a condition (i < 10), and an expression (i++). These don't necessarily need to be related, or even present. Here's a for loop that does nothing, and runs forever.
code:
for (;;);

This for loop has no initialization, no condition, and no expression. It runs forever, and does nothing. Here's a loop that alternates true and false, ten times.
code:
bool a;
int i = 0
for (a = false, i = 0; i < 10; a = !a)
    print a;

This loop initially sets a to false and i to 0, checks if i < 10, and then negates a.

You can do a LOT with a for loop (I think I had one once that must have been a hundred characters long, with no loop body).

What do you want to do? You want to start your counter at zero, increment by 1 every loop, and quit when CusID is zero. I'll leave the rest to you Razz.

Oh, and compiling in C99 (or C++) will let you declare the counter in the loop for even moar awesomeness.
code:
for (int i = 0; i < 10; i++);

The difference between this, and the first example, is that the counter (i) does not exist outside the loop, so if you need to know how many times your loop has executed after your loop finishes, don't do this!
sbuhari




PostPosted: Wed Jul 27, 2011 9:21 pm   Post subject: Re: Need help in For loops and while loops

Thanks guys your replies helped me alot
mirhagk




PostPosted: Thu Jul 28, 2011 2:56 pm   Post subject: Re: RE:Need help in For loops and while loops

Insectoid @ Wed Jul 27, 2011 4:17 pm wrote:
The difference between this, and the first example, is that the counter (i) does not exist outside the loop, so if you need to know how many times your loop has executed after your loop finishes, don't do this!


I was under the impression that this was a point of ambiguity between compilers, where some have it local to the loop, and some have it local to the scope outside of the loop.
Insectoid




PostPosted: Thu Jul 28, 2011 5:01 pm   Post subject: RE:Need help in For loops and while loops

It would be ridiculous to have the variable outside the scope of the loop. You'd end up with name clashes. What if I run a loop with an iterator named 'i', and after that loop executes I declare a string named 'i'? The variable may be stored on the heap (for optimization) but I doubt it's available to anything but the loop at runtime and for all intents and purposes might as well live in the stack.
Nick




PostPosted: Thu Jul 28, 2011 11:39 pm   Post subject: RE:Need help in For loops and while loops

why would you name a string i? I don't think you'd run into this problem if you use proper conventions, the only time when you should have a shorthand for a variable name (ex: i) is an iterator in a for loop, if you wanna use i in one for loop, then use i in another for loop in the same scope, merely reset the value of i between loops
Sponsor
Sponsor
Sponsor
sponsor
QED




PostPosted: Tue Oct 25, 2011 11:21 am   Post subject: Re: RE:Need help in For loops and while loops

mirhagk @ Thu Jul 28, 2011 2:56 pm wrote:

I was under the impression that this was a point of ambiguity between compilers, where some have it local to the loop, and some have it local to the scope outside of the loop.


Thankfully, that's not the case. Compiler ambiguity at the language parsing stage would be quite disastrous to program portability. The C99 standard explicitly allows for loop variable declaration in the initializer. Scope for variables declared this way is local to the loop body.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: