Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

for loop code

for loop loop

  • Please log in to reply
8 replies to this topic

#1 payam.a

payam.a

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 09 July 2008 - 04:19 AM

Hi dear friends;
I have a question about this code. please guide me.
what will happen if we don't write the first expression of the for loop.If we can write another things instead, what should we write?
please help me about this code.
thanks
payam
/* for_show.c */

#include <stdio.h>

int main(void)

{

    int num = 0;



    for (printf("Keep entering numbers!\n"); num != 6;  )

        scanf("%d", &num);

    printf("That's the one I want!\n");

    return 0;

}

this is it's output.





Keep entering numbers!

3

5

8

6

That's the one I want!

Edited by WingedPanther, 12 July 2008 - 03:50 AM.
add code tags

  • 0

#2 YAPP

YAPP

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 09 July 2008 - 05:12 AM

You could rewrite the for loop using a while loop easily.
while(num!=6)
{
    printf("Keep entering numbers!\n");
    scanf("%d",&num);
}
printf("That's the one I want!");

  • 0

#3 payam.a

payam.a

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 09 July 2008 - 05:36 AM

thanks dear friend;
now I am reading the features of for loop. as I wrote in my first post I want to know what can I write in the first expression of a for loop instead intializing a variable.
thanks.
payam
  • 0

#4 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 09 July 2008 - 10:00 AM

You mean initialising the variable within the for loop declaration, instead of having to create a separate variable?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 09 July 2008 - 02:21 PM

I think he means:
'In the expression for(A,B,C){D;}, what are the legal values of A, B, C, and D?'
The answer is that they can be any legal C statements. For example, for a countdown:
int i = 11;
for(printf("Countdown to liftoff:\n"); (i = i - 1) != 0; printf("%d\n", i))sleep(1);

for(A,B,C){D;} is functionally equivalent to
[I]A[/I];
start_of_loop:
if (![I]B[/I]) goto end_of_loop;
[I]D[/I];
[I]C[/I];
goto start_of_loop;
end_of_loop:

So you can put whatever you want! :D
  • 0

#6 payam.a

payam.a

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 11 July 2008 - 03:50 AM

thanks dear friends;
as you know the first expression of a for loop shoyld be initializing a variable.
but in the code that I wrote, there is another thing instead of intializing I have used a printf function. I mean, is it correct to do these jobs? if it is correct whatelse can we write instead of the first expression in a for loop?
thanks
payam
  • 0

#7 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 11 July 2008 - 08:14 AM

Whatever you want.
  • 0

#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 11 July 2008 - 11:42 AM

You can also put multiple expressions or no expressions at all. For example:

//No initializing
    int i = SOMEVALUE;
    ...code...
    for(; i < 5; ++i){ ... }
//Multiple statements
    int i , j;
    for(i = 0, j = 2; (i < 6)&&(j < 10); ++i, j +=5) { ... }


  • 0

#9 payam.a

payam.a

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 12 July 2008 - 02:21 AM

thanks dear friends for your excellent answers specially you dear Aresheaa;
  • 0





Also tagged with one or more of these keywords: for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download