Return to iWEBTOOL

Go Back   iWEBTOOL Talk > The Web and your Website > Programming
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
Welcome to iWEBTOOL Talk, where you talk about webmaster-related stuff.
 
Discuss topics which interest you.
With over thousands of threads (topics), we're sure you'll find something that'll interest you.
Ask for help whenever you need it.
We have thousands of members who are available to help you if you need it.
It's absolutely FREE!

 1  Register           2  Browse the board           3  Discuss whatever may interest you!
 


Closed Thread
 
Thread Tools Search this Thread Display Modes
Old 02-03-2006, 01:23 PM   #1
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Programming BrainScratch

Well, anybody can do normal programming.... Everybody understands the printf() function... But... here are a series of questions that will force every developer to think.. The answer might be obvious... but tax your brains hard for it...

Q1) Under practical conditions, will the following loop break(or stop) anytime:

Code:
for(int i=0; i<-1; i++) { //Some code here }

Try this... Answers are to be backed up by reasons. Right answers, but wrong reasons are not the right solutions Enjoy...
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.

Last edited 02-03-2006 at 01:25 PM.
rohan2kool is offline  
 
This is an Ad Revenue Sharing forum Place your advert here
SEO Checklist
Get yourself better ranking with
our "Do-it-Yourself" SEO Checklist.
Click Here
Old 02-03-2006, 06:42 PM   #2
kenni
Registered Member
 
kenni's Avatar
 
Join Date: Dec 2005
Posts: 1,543
kenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to beholdkenni is a splendid one to behold
Default Re: Programming BrainScratch

Not really sure, i've never come across the "<-"

for(int i=0; i<-1; i++) {
//Some code here
}
__________________
Wanna thank someone? Give 'em a rep. More info.
kenni is offline  
Old 02-04-2006, 04:26 AM   #3
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Re: Programming BrainScratch

it's not <- it is <(-1), it's minus 1.

So, you can call it:

for(int i=0; i< (-1); i++) {
//some code here
}
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.
rohan2kool is offline  
Old 02-05-2006, 03:44 AM   #4
compuXP
Retired Member
 
compuXP's Avatar
 
Join Date: Dec 2005
Posts: 139
compuXP will become famous soon enough
Default Re: Programming BrainScratch

Yeah that would just keep looping...
__________________
Retired Moderator/Member/Friend/Helper - it was great working with all of you!
compuXP is offline  
Old 02-05-2006, 09:47 AM   #5
pairbrother
Regular Member
 
pairbrother's Avatar
 
Join Date: Dec 2005
Location: India
Posts: 222
pairbrother will become famous soon enough
Send a message via Yahoo to pairbrother
Default Re: Programming BrainScratch

under practical conditions, it is an infinite loop.
under impractical conitions, the loop can break if we change the value of i to negative (like -2) inside the code or put a break
pairbrother is offline  
Old 02-05-2006, 01:27 PM   #6
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Re: Programming BrainScratch

Well... supposing the value of 'i' is not changed at all and there is no 'break' statement or any sort of code that may cause the program to crash and thereby cause the program to terminate. Suppose it to be this:

for(int i=0; i< (-1); i++) {
printf("My name is Rohan Prabhu");
}

I repeat my question, Q1) Under practical conditions, will the following loop break(or stop) anytime?
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.
rohan2kool is offline  
Old 02-05-2006, 03:27 PM   #7
pairbrother
Regular Member
 
pairbrother's Avatar
 
Join Date: Dec 2005
Location: India
Posts: 222
pairbrother will become famous soon enough
Send a message via Yahoo to pairbrother
Default Re: Programming BrainScratch

under very practical conditions i think it will not...

but then since u r askin this q, probably it will stop sometime..lol

umm...in C max value of int is 32767 after that if we increment it, it wiil become -32768, which will satisfy the condition and hence the loop will stop!?
pairbrother is offline  
Old 02-06-2006, 09:08 AM   #8
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Re: Programming BrainScratch

Quote:
Originally Posted by pairbrother
under very practical conditions i think it will not...

but then since u r askin this q, probably it will stop sometime..lol

umm...in C max value of int is 32767 after that if we increment it, it wiil become -32768, which will satisfy the condition and hence the loop will stop!?

Your answer is correct, but the reasoning is slightly wrong. Do check the note below

Excellent d00d. Your answer is absolutely correct. When the number reaches 32767 (in C or 2147483647 in 32-bit numbers) the binary would appear as:

0111111111111111b

Adding 1 to it would make the number:

1000000000000000b

which is actually 32768 in unsigned numbers... but in signed numbers it is 0. Actually, it is -0. Then on adding another number to it, it then becomes:

1000000000000001b

which in unsigned numbers is 32769, but in signed number it is -1. Similarly after another iteration, it is converted to -2 (signed) and hence the loop terminates.

Correction: According to your reasoning, the number aftere 32767 turns to -32767. It is not so. It happens only in signed numbers, but due to the binary system not working on the signed principle, it increments the number according to the binary rule, but interprets it as a binary number and signs it and uses it as an integer. And it does not become -32767, it becomes 0, then -1, then -2 and so on...

I hope you liked this part of programming brainScratch. Watch out for the next question...

-Rohan Prabhu
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.
rohan2kool is offline  
Old 02-06-2006, 05:09 PM   #9
pairbrother
Regular Member
 
pairbrother's Avatar
 
Join Date: Dec 2005
Location: India
Posts: 222
pairbrother will become famous soon enough
Send a message via Yahoo to pairbrother
Default Re: Programming BrainScratch

infact
i ran this code in java...

Code:
public class testt { public static void main(String[] args) { System.out.println("entering loop..."); for(int i=0; i<-1; i++) { System.out.println("i: " + i); } System.out.println("out of loop"); } }

and the output is
Code:
entering loop... out of loop

that means, not even once does it enter in the loop

now y is that so?
Code:
for(int i=0; i<-1; i++)
i=0 initializes i to 0
then it checks the condition i < -1 thatis 0 < -1 which gives the result as false and so the loop breaks......

thts wht i cud understand..:p
pairbrother is offline  
Old 02-22-2006, 01:05 PM   #10
xpertcoder
Junior Member
 
xpertcoder's Avatar
 
Join Date: Feb 2006
Location: India
Posts: 38
xpertcoder will become famous soon enough
Send a message via MSN to xpertcoder Send a message via Yahoo to xpertcoder
Post Re: Programming BrainScratch

Quote:
Originally Posted by pairbrother
infact
i ran this code in java...

Code:
public class testt { public static void main(String[] args) { System.out.println("entering loop..."); for(int i=0; i<-1; i++) { System.out.println("i: " + i); } System.out.println("out of loop"); } }

and the output is
Code:
entering loop... out of loop

that means, not even once does it enter in the loop

now y is that so?
Code:
for(int i=0; i<-1; i++)
i=0 initializes i to 0
then it checks the condition i < -1 thatis 0 < -1 which gives the result as false and so the loop breaks......

thts wht i cud understand..:p

I completely agree with what pairbrother has stated ... the logic is perfect and the reasoning is provided with the proper proof.
for(int i=0; i<-1; i++) literally means

int i = 0 --> Initialization of intereger variable "i" as zer0
i <-1 --> until the value of i is less than -1 ( which "0" is defintely not)
i++ --> means Increment i by 1

As the condition of i<-1 itself is not satisfied inthe 2nd step the loop is discontinued.

Brain Scratch
__________________
max pain
make money online :p Make money before getting too old.
outsourcing projects The best outsourcing website
xpertcoder is offline  
Old 08-25-2006, 04:52 PM   #11
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Re: Programming BrainScratch

oopssy... i screwed up an excellent question.. the loop should be:

Code:
for(int i=0;i>=0;i++) { //The code here }

sorry for the goof up guys
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.
rohan2kool is offline  
Old 08-25-2006, 11:16 PM   #12
compuXP
Retired Member
 
compuXP's Avatar
 
Join Date: Dec 2005
Posts: 139
compuXP will become famous soon enough
Default Re: Programming BrainScratch

Lol, nice save after 6 months No wonder I got confused!
__________________
Retired Moderator/Member/Friend/Helper - it was great working with all of you!
compuXP is offline  
Old 08-28-2006, 04:59 PM   #13
rohan2kool
Member
 
rohan2kool's Avatar
 
Join Date: Dec 2005
Posts: 132
rohan2kool will become famous soon enough
Default Re: Programming BrainScratch

just didn't notice it for long... just wanted to read some of my creations... so pondered over the last post and found out the mistake sorry guys
__________________
Eat healthy, Stay Fit, Die Anyway.
--==--
If there is a man who knows everything about women, there is a Windows distribution without bugs.
rohan2kool is offline  
 
This is an Ad Revenue Sharing forum Place your advert here
Webmaster Tools Webmaster Tools Click Here
Closed Thread

(Threads which have no activity for more than 30 days are automatically closed.)



Quick Reply
Message:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Learn Programming Free steveo General Talk 0 11-22-2006 10:04 AM
What programming languages do you know? compuXP Advertise your website 22 03-14-2006 11:35 PM
Ajax Programming: Web 2.0 compuXP Programming 6 01-09-2006 10:59 PM
I wanna learn symbian programming markkk Programming 1 12-21-2005 05:05 PM
Whats your favorite programming code? josephheskett Programming 1 12-15-2005 11:08 PM


All times are GMT. The time now is 11:56 AM.


Powered by vBulletin v3.6.7 © 2008, Jelsoft Enterprises Ltd. SEO by vBSEO © 2006, Crawlability, Inc.