## Jan 08, 2013

### Massive Tracing Quiz

#### Conditionals and Loops

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

Quiz | Code | Description |
---|---|---|

1-2 | a1 | conditionals with comparison ">" |

3-4 | a2 | conditionals with comparison "==" |

5-6 | a3 | loop using no control variables (see Figures 1 and 3) |

7-8 | a4 | loop using control variables |

9-10 | a5 | conditional with comparison ">" using no control variables (see Figure 3) |

11-12 | a6 | conditional with comparison "==" using no control variables |

13-14 | a7 | conditional with comparison ">" using control variables (see Figure 3) |

15-16 | a8 | conditional with comparison "==" using control variables |

17-18 | a9 | loop using no control variables in conditional with comparison ">" |

19-20 | a10 | loop 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