Jan 08, 2013

Massive Tracing Quiz


Conditionals and Loops

The codes used for these quizzes consist of the combination of loops and conditionals.

QuizCodeDescription
1-2a1conditionals with comparison ">"
3-4a2conditionals with comparison "=="
5-6a3loop using no control variables (see Figures 1 and 3)
7-8a4loop using control variables
9-10a5conditional with comparison ">" using no control variables (see Figure 3)
11-12a6conditional with comparison "==" using no control variables
13-14a7conditional with comparison ">" using control variables (see Figure 3)
15-16a8conditional with comparison "==" using control variables
17-18a9loop using no control variables in conditional with comparison ">"
19-20a10loop using no control variables in conditional with comparison "=="
def a1(a)
  ans = 0
  if a > 3
    ans = ans + a
  else
    ans = ans - a
  end
  p ans
end
def a2(a)
  ans = 0
  if a == 3
    ans = ans + a
  else
    ans = ans - a
  end
  p ans
end
def a3(a)
  ans = 0
  for i in 1..a
    ans = ans + a
  end
  p ans
end
def a4(a)
  ans = 0
  for i in 1..a
    ans = ans + i
  end
  p ans
end
def a5(a)
  ans = 0
  for i in 1..a
    if a > 3
      ans = ans + a
    else
      ans = ans - a
    end
  end
  p ans
end
def a6(a)
  ans = 0
  for i in 1..a
    if a == 3
      ans = ans + a
    else
      ans = ans - a
    end
  end
  p ans 
end
def a7(a)
  ans = 0
  for i in 1..a
    if i > 3
      ans = ans + i
    else
      ans = ans - i
    end
  end
  p ans
end
def a8(a)
  ans = 0
  for i in 1..a
    if i == 3
      ans = ans + i
    else
      ans = ans - i
    end
  end
  p ans
end
def a9(a)
  ans = 0
  if a > 3
    for i in 1..a
      ans = ans + a
    end
  else
    ans = ans - a
  end
  p ans 
end
def a10(a)
  ans = 0
  if a == 3
    ans = ans + a
  else
    for i in 1..a
      ans = ans - a
    end
  end
  p ans 
end

Permanent Link | Posted at 19:46