LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2013, 01:25 AM   #1
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
What's wrong with this simple code


Code:
#!/usr/bin/perl
=cut
Program to sum the squares of first 10 odd nos.
=cut

$count=0;
$i=0;
$sum=0;
while(1){
	
	$i++;
	if($i/2!=0){
		print "$i\n";
		$sum+=$i**2;
		$count++;
	}
	if($i/2==0){
		print "Ok\n";
	}
	if($count==10){
		last;
	}
}
print "The sum of first 10 odd nos. = $sum";
The o/p I get from this code is -
Code:
1
2
3
4
5
6
7
8
9
10
The sum of first 10 odd nos. = 385
I am not able to figure it out. Why is the if statement
not working.

Last edited by kedarp; 03-12-2013 at 01:36 AM.
 
Old 03-12-2013, 01:42 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

I don't code in perl, but would be very surprised if its "/" operator is modulus division. Perhaps you want "%"?

Evo2.
 
2 members found this post helpful.
Old 03-12-2013, 02:48 AM   #3
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Quote:
Originally Posted by evo2 View Post
Hi,

I don't code in perl, but would be very surprised if its "/" operator is modulus division. Perhaps you want "%"?

Evo2.
Oops. Actually these scripting language can really make you forget programming.
I just thought this was a syntax error.

Last edited by kedarp; 03-12-2013 at 02:50 AM.
 
Old 03-12-2013, 06:03 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I know many disagree with me, but I do think it is lazy programming to write an infinite loop to be broken out of when you could simply
test your count variable in the while clause, or better yet, use a for loop as you know then end point.
 
1 members found this post helpful.
Old 03-12-2013, 10:13 AM   #5
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Quote:
Originally Posted by grail View Post
I know many disagree with me, but I do think it is lazy programming to write an infinite loop to be broken out of when you could simply
test your count variable in the while clause, or better yet, use a for loop as you know then end point.
That was my first try to the program.
 
Old 03-12-2013, 10:15 AM   #6
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Quote:
Originally Posted by grail View Post
I know many disagree with me, but I do think it is lazy programming to write an infinite loop to be broken out of when you could simply
test your count variable in the while clause, or better yet, use a for loop as you know then end point.
That was my first try to the program. But thanks for the suggestion.
 
Old 03-12-2013, 10:16 AM   #7
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Quote:
Originally Posted by grail View Post
I know many disagree with me, but I do think it is lazy programming to write an infinite loop to be broken out of when you could simply
test your count variable in the while clause, or better yet, use a for loop as you know then end point.
That was my first try to the program. But thanks for the suggestion.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple script - What Am I doing wrong? Mouse750 Programming 2 09-14-2011 09:01 PM
[SOLVED] What's Wrong with the RegEx [Simple] thund3rstruck Programming 2 04-04-2011 11:04 AM
Whats wrong with this simple code Boffy Programming 6 10-31-2004 01:16 PM
whats wrong with this simple code?? laclac01 Programming 3 08-23-2004 03:52 PM
what's wrong with rh 9.0 - i only have a simple pc spyghost Linux - Software 3 10-20-2003 09:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration