|
| |||||||
![]() | Welcome to iWEBTOOL Talk, where you talk about
webmaster-related stuff.
1 Register
2 Browse the board
3 Discuss whatever may interest you! | |||||||||||||
![]() |
| | Thread Tools | Search this Thread | Display Modes |
| | #1 |
| Member Join Date: Dec 2005
Posts: 132
![]() | 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:
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 12:25 PM. |
| |
|
| |||||||
| | #3 |
| Member Join Date: Dec 2005
Posts: 132
![]() | 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. |
| |
| | #4 |
| Retired Member Join Date: Dec 2005
Posts: 139
![]() | Yeah that would just keep looping...
__________________ Retired Moderator/Member/Friend/Helper - it was great working with all of you! |
| |
| | #5 |
| Regular Member | 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
__________________ |
| |
| | #6 |
| Member Join Date: Dec 2005
Posts: 132
![]() | 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. |
| |
| | #7 |
| Regular Member | 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!?
__________________ |
| |
| | #8 | |
| Member Join Date: Dec 2005
Posts: 132
![]() | Quote:
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. | |
| |
| | #9 |
| Regular Member | infact i ran this code in java... Code:
and the output is Code:
that means, not even once does it enter in the loop ![]() now y is that so? Code:
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
__________________ |
| |
| | #10 | |
| Junior Member | Quote:
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 | |
| |
| | #11 |
| Member Join Date: Dec 2005
Posts: 132
![]() | oopssy... i screwed up an excellent question.. the loop should be: Code:
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. |
| |
| | #12 |
| Retired Member Join Date: Dec 2005
Posts: 139
![]() | Lol, nice save after 6 months No wonder I got confused!
__________________ Retired Moderator/Member/Friend/Helper - it was great working with all of you! |
| |
| | #13 |
| Member Join Date: Dec 2005
Posts: 132
![]() | 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. |
| |
(Threads which have no activity for more than 30 days are automatically closed.) |
| Quick Reply | ||
|
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Learn Programming Free | steveo | General Talk | 0 | 11-22-2006 09:04 AM |
| What programming languages do you know? | compuXP | Advertise your website | 22 | 03-14-2006 10:35 PM |
| Ajax Programming: Web 2.0 | compuXP | Programming | 6 | 01-09-2006 09:59 PM |
| I wanna learn symbian programming | markkk | Programming | 1 | 12-21-2005 04:05 PM |
| Whats your favorite programming code? | josephheskett | Programming | 1 | 12-15-2005 10:08 PM |