473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Trying to understand if and do/while

momotaro
357 100+
I don't understand how this function works!!! and specialy the "if condition" and it's return!!!!
please help!!!!

Expand|Select|Wrap|Line Numbers
  1. Boolean UserSaysYes(void)
  2. {
  3.    int c;
  4.    printf("(y,n)?");
  5.    do{
  6.         while((c = getchar()) == '\n')
  7.             ;
  8.         if(c == 'y' || c == 'Y' || c == 'n' || c == 'N')
  9.              return(c == 'y' || c == 'Y');
  10.         printf("please respond by typing one of the letters y or n\n");
  11.       }while(1) // this one also !!!!!
  12.  
May 31 '07 #1
2 1280
pradeep kaltari
102 Expert 100+
I don't understand how this function works!!! and specialy the "if condition" and it's return!!!!
please help!!!!

Expand|Select|Wrap|Line Numbers
  1. Boolean UserSaysYes(void)
  2. {
  3.    int c;
  4.    printf("(y,n)?");
  5.    do{
  6.         while((c = getchar()) == '\n')
  7.             ;
  8.         if(c == 'y' || c == 'Y' || c == 'n' || c == 'N')
  9.              return(c == 'y' || c == 'Y');
  10.         printf("please respond by typing one of the letters y or n\n");
  11.       }while(1) // this one also !!!!!
  12.  
Hi momotaro,
The "if" condition checks whether the character entered is one among y,Y,n,N. If the character is one among the specified ones then the return statement is executed, otherwise the printf statement is executed.

Coming to the return statement: here the character entered is compared with y and Y. If it is one among the two then true(1) is returned, otherwise false(0) is returned to the calling function.
i.e., If the character entered is y or Y then it'll be "return 1", and if its n or N then it'll be "return 0".

I hope this helps you.

Regards,
Pradeep
May 31 '07 #2
AdrianH
1,251 Expert 1GB
I don't understand how this function works!!! and specialy the "if condition" and it's return!!!!
please help!!!!

Expand|Select|Wrap|Line Numbers
  1. Boolean UserSaysYes(void)
  2. {
  3.    int c;
  4.    printf("(y,n)?");
  5.    do{
  6.         while((c = getchar()) == '\n')
  7.             ;
  8.         if(c == 'y' || c == 'Y' || c == 'n' || c == 'N')
  9.              return(c == 'y' || c == 'Y');
  10.         printf("please respond by typing one of the letters y or n\n");
  11.       }while(1) // this one also !!!!!
  12.  
Any non-zero or non-NULL value is considered to be true when put in the context of being a Boolean. Given that, you should be able to figure out what the while does.


Adrian
May 31 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: HolaGoogle | last post by:
hi all, can you please tell me what i should do to avoid session timeout when displaying my database info in my asp form (DisplayUserDatabase.asp)??? ** actualy it does load and display the...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
13
by: cj | last post by:
Stephany Young provided me with the following code to count threads created by my program. Public Class MyThreadCount Private Shared m_lock As New Object Private Shared m_threadcount As Int32...
17
by: Sam Malone | last post by:
I am trying to get details from a database. I really want to use only native VS.NET managed code "stuff" (just cuz I want to) and avoid any interop stuff. So, I'm trying to do this without using...
4
by: dvir | last post by:
Hi I have a runing error while trying to initialize a class. this is my code: main.C ------------- #include "tdagent.H" #include "simple_env.H" #include <vector>
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
4
by: sandy | last post by:
I am trying to upper case a string, so I have this method: string FileSystem::toupper(string S) { for (int i=0; i<S.length(); ++i) { S=toupper(S); } return S;
5
by: Alan Silver | last post by:
Hello, Server configuration: Windows 2003 Server SP2 SQL Server 2000 SP4 ..NET v2.0.50727 just built up a new server using the same configuration as my current one. I even used the same CDs...
3
by: Robert Dunlop | last post by:
I am using Microsoft Visual Studio 2005 for development of an ASP.NET site, and I have a problem that greatly effects my workflow at times. It seems that after I upload new files anywhere within...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.