CodingBat Interlude

Practice for the fundamentals of coding in Java: logic and loops.

We are studying something easy for the two days at the end of the quarter.

Summary of Logical Operations

  • if - else if - else
  • and(&&), or (||), not (!)
public boolean someFunction (int n)
{
  boolean answer = false;
  
  if ( n > 20 ) {
    answer = true;
  } else {
    answer = ( n < -10);
  }
  
  return answer;
}

Summary of Looping

  • arrayname.length finds the length of the array
  • arrayname[index] gets the the value at the given index
  • for (set starting value; test to continue; what to do between steps) { … }
public int countNines (int[] nums)
{
  int count = 0;
  for (int k = 0; k < nums.length; k++)
  {
    if (nums[k] == 9) 
    {
      count = count + 1;
    }
  }
  return count;
}
Last modified August 18, 2023: 2022-2023 End State (7352e87)