implement for loops
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#include "loop.h"
|
||||
|
||||
void printLoopSuccess(char* name, int x, int expected, int result) {
|
||||
printf("SUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected);
|
||||
printf("\033[0;32mSUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
void printLoopError(char* name, int x, int expected, int result) {
|
||||
printf("ERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected);
|
||||
printf("\033[0;31mERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
int loopTest(char* name, int x, int expected, int result) {
|
||||
@@ -25,6 +25,8 @@ int runLoopTests() {
|
||||
failed += loopTest("while", 5, 5, myWhile(5));
|
||||
failed += loopTest("doWhile", 0, 1, myDoWhile(0));
|
||||
failed += loopTest("doWhile", 1, 1, myDoWhile(1));
|
||||
failed += loopTest("for", 5, 5, myFor(5));
|
||||
failed += loopTest("for", 0, 0, myFor(0));
|
||||
|
||||
return failed;
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
int myWhile(int x);
|
||||
int myDoWhile(int x);
|
||||
int myDoWhile(int x);
|
||||
int myFor(int x);
|
||||
@@ -154,4 +154,12 @@ function myDoWhile(end) {
|
||||
return cond;
|
||||
}
|
||||
|
||||
function myFor(end) {
|
||||
let x = 0;
|
||||
for (let i = 0; (i < end); i = (i + 1)) {
|
||||
x = (x + 1);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
add(1, 1);
|
||||
|
||||
Reference in New Issue
Block a user