Instructions: // Implement the function so that all tests pass using a while loo
ID: 667791 • Letter: I
Question
Instructions: // Implement the function so that all tests pass using a while loop. public static int addIntsInRangeUsingWhile(int start, int finish) {return 0; } .........make sure all the test under this pass!
------------------------------------------------------------------------------------------
@Test
public void testAddIntsInRangeUsingWhile() {
assertEquals(10, HelloJava.addIntsInRangeUsingWhile(0,5));
}
@Test
public void testAddIntsInRangeUsingWhile2() {
int expected = 2 + 3 + 4;
assertEquals(expected, HelloJava.addIntsInRangeUsingWhile(1,5));
}
@Test
public void testAddIntsInRangeUsingWhile3() {
int expected = 3 + 4 + 5;
assertEquals(expected, HelloJava.addIntsInRangeUsingWhile(2,6));
}
Explanation / Answer
The while Loop
The while loop repeats some code until its test condition (another expression) returns false
var count = 0;
while (count < 10)
{
document.writeln("looping away!");
count++;
}
In this example, the condition is represented by the count < 10 expression. With each iteration, our loop increments the count value by 1:
var count = 0;
while (count < 10)
{
document.writeln("looping away!");
count++;
}
var count = 0;
while (count < 10)
{
document.writeln("looping away!");
count++;
}