|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 Code:
/* 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;
}
Keep entering numbers! 3 5 8 6 That's the one I want! Last edited by WingedPanther; 07-12-2008 at 06:50 AM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||
|
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 |
|
|||||
|
You mean initialising the variable within the for loop declaration, instead of having to create a separate variable?
|
|
|||||
|
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: Code:
int i = 11;
for(printf("Countdown to liftoff:\n"); (i = i - 1) != 0; printf("%d\n", i))sleep(1);
Code:
A; start_of_loop: if (!B) goto end_of_loop; D; C; goto start_of_loop; end_of_loop: ![]() |
| Sponsored Links |
|
|
|
|||
|
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 |
|
|||
|
You can also put multiple expressions or no expressions at all. For example:
Code:
//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) { ... }
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hi5 tiny slideshow code crap... | Godiva983 | General Programming | 1 | 06-23-2008 04:46 PM |
| Basic Calculator | AfTriX | VB Tutorials | 3 | 02-29-2008 08:53 AM |
| Xav | ........ | 1322.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 830.24 |
| Brandon W | ........ | 733.07 |
| chili5 | ........ | 309.39 |
| Steve.L | ........ | 236.23 |
| dcs | ........ | 214.02 |
Goal: 100,000 Posts
Complete: 82%