Skip to content

Instantly share code, notes, and snippets.

@tatey
Created November 9, 2008 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatey/23223 to your computer and use it in GitHub Desktop.
Save tatey/23223 to your computer and use it in GitHub Desktop.
// Enhanced for loop (For each)
public int sum(int[] nums)
{
int sum = 0;
for (int num : nums)
{
sum += num * num
}
return sum;
}
// For loop
public int sum(int[] nums)
{
int sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i] * nums[i]
}
return sum;
}
// While loop
public int sum(int[] nums)
{
int sum = 0;
int i = 0;
while (i < nums.length)
{
sum += nums[i] * nums[i]
i++;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment