Geeks Talk

Prepare for your Next Interview




How many times the while loop execute

This is a discussion on How many times the while loop execute within the C and C++ forums, part of the Software Development category; define void main(),int main()?what are the differences between void main() and int main(). int main() { int x=0; while(x<10) { printf("%d", x); x++; } ...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-20-2007
Junior Member
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
chandanavemulapally is on a distinguished road
How many times the while loop execute

define void main(),int main()?what are the differences between void main() and int main(). int main()
{
int x=0;
while(x<10)
{
printf("%d", x);
x++;
}
how many times the while loop execute
Reply With Quote
The Following User Says Thank You to chandanavemulapally For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 12-22-2007
Contributing Member
 
Join Date: Dec 2007
Posts: 40
Thanks: 1
Thanked 8 Times in 7 Posts
sk_seeker is on a distinguished road
Re: How many times the while loop execute

Quote:
Originally Posted by chandanavemulapally View Post
define void main(),int main()?what are the differences between void main() and int main(). int main()
{
int x=0;
while(x<10)
{
printf("%d", x);
x++;
}
how many times the while loop execute
You have two questions. Lets tackle this one by one.

1.
main() is just like any other function in C. This means that it can take input arguments and also return a result value. So, the real declaration of main() is:
<return value > main (arg1, arg2, arg3...)

By specifying void main(), you are actually saying that main() will not return any value. By specifying int main(), you are saying that main() will return an integer value. The UNIX convention is that a return value of zero is success and a non-zero value indicates some failure code. Also, this return code from main can be examined by the shell to see if the program was successful (ret value of zero) or failure (non-zero return value). The non-zero return value can be successfully used as a debugging aid if the program returns different failure codes for different errors.

2.
The while loop will execute 10 times: from z=0 to x=9. When x becomes 10, it will bail out of the while loop. Why do you ask this question?? I mean to say: what doubt did you have that you asked this question??
Reply With Quote
  #3 (permalink)  
Old 12-22-2007
Contributing Member
 
Join Date: Nov 2007
Location: bangalore
Posts: 54
Thanks: 6
Thanked 3 Times in 3 Posts
rahulvegi is on a distinguished road
Re: How many times the while loop execute

void main()
the above method doesnot return any value.
int main()
the above method must return int value.
int main()
{
//statements
......
......
return 0;(may be placed or not)
}
the above program 10 times to be executed
Reply With Quote
  #4 (permalink)  
Old 12-23-2007
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 447
Thanks: 20
Thanked 54 Times in 54 Posts
susarlasireesha is on a distinguished road
Re: How many times the while loop execute

main() must return int. Not void, not bool, not float.Some compilers accept void main(), but that is non-standard and shouldn't be used. Instead use int main(). The while loop execute 10 times.
Reply With Quote
  #5 (permalink)  
Old 01-14-2008
Junior Member
 
Join Date: Oct 2007
Location: India
Posts: 5
Thanks: 0
Thanked 2 Times in 1 Post
yadvharry is on a distinguished road
Exclamation Re: How many times the while loop execute

you have posted two questions so i want to answer one by one.
1:the difference between void main() and int main()
first of all we want to differentiate between both of them.
both of these are return type which are return some value to the os.

Void is used as nothing is return to os.

int to return anint valu to the os.

2->the while loop is executed ten times from 0to 9.that is 10 times.
Reply With Quote
  #6 (permalink)  
Old 02-14-2008
Junior Member
 
Join Date: Feb 2008
Location: MUMBAI
Posts: 14
Thanks: 3
Thanked 3 Times in 2 Posts
steph47 is on a distinguished road
Re: How many times the while loop execute

yes the loop will be executed for x= 0 to x=9 and for 10 it will teminate

so it will execute 10 time

well we write void main() on the assumption that our main function isnt goin to return any value and in case of int main() the main function is supposed to return an integer value

thus within an int main() function we shud have the habbit of writing return 1 at last which is an indication that the fucntion will return an intger value
Reply With Quote
  #7 (permalink)  
Old 02-15-2008
Junior Member
 
Join Date: Feb 2008
Location: bangalore
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
suvendranj is on a distinguished road
Re: How many times the while loop execute

Quote:
Originally Posted by chandanavemulapally View Post
define void main(),int main()?what are the differences between void main() and int main(). int main()
{
int x=0;
while(x<10)
{
printf("%d", x);
x++;
}
how many times the while loop execute


it will execute for 10 times from 0to9
Reply With Quote
  #8 (permalink)  
Old 02-26-2008
Junior Member
 
Join Date: Feb 2008
Location: india
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
crazyaboutc is on a distinguished road
Re: How many times the while loop execute

As sumone above has given an answer tat main can be declared as like as other function den it's contradictory coz declaration of main is done by the compiler called by the operating system definition is given by the programmer . Diff btw void n int main is tat in the former no value will be returned where as in the latter value will be returned if v don mention ny return type den a garbage value will be returned......
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Run report without resolving loop sonies Data Warehousing 2 12-13-2007 05:00 AM
Write loop results to database Geek_Guest QTP 1 10-26-2007 07:07 AM
Implement FOR Loop Geek_Guest QTP 1 10-23-2007 06:26 AM
Store Value using loop in procedure talha_saeed SQL 2 10-23-2007 03:03 AM
Difference between different times fred Unix/Linux 1 07-17-2006 05:33 PM


All times are GMT -4. The time now is 12:56 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved