473,322 Members | 1,493 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,322 software developers and data experts.

can we declare like this how many times the loop executes while(flag)

flag=1;
while(flag)
{
}
if this is correct please explain me
Jun 18 '13 #1
3 1280
stdq
94 64KB
Using the Open Watcom compiler, the above program worked fine, although it is necessary to declare (and initialize as done above) variable flag as being of a specific data type, for example as an integer:

Expand|Select|Wrap|Line Numbers
  1. int flag = 1;
When the program reaches the while loop, the condition is evaluated to 1, which is true, and it never changes, because there is no statement inside the while that performs such change. Hence, the program loops infinitely.
Jun 18 '13 #2
Nepomuk
3,112 Expert 2GB
In the C language there is no predefined primitives for the boolean values true and false. Instead, the integer 0 is interpreted as false and any other integer is interpreted as true. So, if you have the above code (with stdq's modification) it should work nicely in any C compiler. It wouldn't just work with that value though; here are a few that would also work:
Expand|Select|Wrap|Line Numbers
  1. int flag = 42;
  2. while(flag) {}
or
Expand|Select|Wrap|Line Numbers
  1. int flag = 1337;
  2. while(flag) {}
Or try this:
Expand|Select|Wrap|Line Numbers
  1. int flag = ++9000;
  2. while(flag) {}
It should work too.
You can even go the other way around and actually calculate the result of an expression which you know will be true, such as
Expand|Select|Wrap|Line Numbers
  1. int flag = (7 == 7);
  2. while(flag) {}
or
Expand|Select|Wrap|Line Numbers
  1. int flag = ! (1 > 2);
  2. while(flag) {}
Here, true is always converted to 1 and false always to 0.
Jun 19 '13 #3
donbock
2,426 Expert 2GB
When you ask if this is correct, what are your concerns? Have they been addressed by the preceding responses?
Jun 19 '13 #4

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

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
4
by: Arpan | last post by:
Consider the following sentence: Peter and I play together and also go to the same school and both of us are in class V. How do I find out how many times the string "and" has appeared in the...
2
by: Steve | last post by:
A main form contains ten textboxes and ten subforms. All ten subforms have the same query (Query2) for their recordsource. Query2 is based on a query (Query1). Each subform is linked to a different...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
3
by: Advo | last post by:
I've made a small search function as of here: if (stristr($bleh,$search_for)) { if(preg_match('/<title>(*)<\/title>/i', $bleh, $matches)) { $name = str_replace("./","/",$name); echo "<li><a...
1
by: GS | last post by:
the windows form application works but the eventhandler for binding source postionChanged got executed too many times: 12 times from saveitemclicked for the data navigator many time from additem ...
3
by: sukatoa | last post by:
For example: Cat Dog Cow Rat Cat Cat Rat Cow
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.