Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-2008, 07:19 AM
payam.a payam.a is offline
Newbie
 
Join Date: Jun 2008
Posts: 12
Credits: 0
Rep Power: 0
payam.a is on a distinguished road
Question for loop code

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;

}
this is it's output.





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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-09-2008, 08:12 AM
YAPP's Avatar   
YAPP YAPP is offline
Newbie
 
Join Date: Jul 2008
Posts: 3
Credits: 0
Rep Power: 0
YAPP is on a distinguished road
Default Re: for loop code

You could rewrite the for loop using a while loop easily.
Code:
while(num!=6)
{
    printf("Keep entering numbers!\n");
    scanf("%d",&num);
}
printf("That's the one I want!");
__________________
UGJhdGVuZ2h5bmd2YmFmISAgTGJoIG5lciBhYmogbiAxMzM3IH U0a2swZSE=
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-09-2008, 08:36 AM
payam.a payam.a is offline
Newbie
 
Join Date: Jun 2008
Posts: 12
Credits: 0
Rep Power: 0
payam.a is on a distinguished road
Question Re: for loop code

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-09-2008, 01:00 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,436
Last Blog:
Web slideshow in JavaS...
Credits: 1,322
Rep Power: 61
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: for loop code

You mean initialising the variable within the for loop declaration, instead of having to create a separate variable?
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 05:21 PM
Aereshaa's Avatar   
Aereshaa Aereshaa is online now
Programming Professional
 
Join Date: Apr 2008
Posts: 282
Credits: 150
Rep Power: 6
Aereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the rough
Default Re: for loop code

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);
for(A,B,C){D;} is functionally equivalent to
Code:
A;
start_of_loop:
if (!B) goto end_of_loop;
D;
C;
goto start_of_loop;
end_of_loop:
So you can put whatever you want!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-11-2008, 06:50 AM
payam.a payam.a is offline
Newbie
 
Join Date: Jun 2008
Posts: 12
Credits: 0
Rep Power: 0
payam.a is on a distinguished road
Post Re: for loop code

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-11-2008, 11:14 AM
Aereshaa's Avatar   
Aereshaa Aereshaa is online now
Programming Professional
 
Join Date: Apr 2008
Posts: 282
Credits: 150
Rep Power: 6
Aereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the rough
Default Re: for loop code

Whatever you want.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-11-2008, 02:42 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 606
Last Blog:
Programs Under the Hoo...
Credits: 193
Rep Power: 11
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: for loop code

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) { ... }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-12-2008, 05:21 AM
payam.a payam.a is offline
Newbie
 
Join Date: Jun 2008
Posts: 12
Credits: 0
Rep Power: 0
payam.a is on a distinguished road
Smile Re: for loop code

thanks dear friends for your excellent answers specially you dear Aresheaa;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 07:38 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads