1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15declare function print(arg: number): string; 16 17function foo(n: number) { 18 let x: number = 0; 19 for (let i: number = 0; i <= n; i++) { 20 x = (i + 0.5) * (i + 0.5); 21 } 22 return x; 23} 24 25print(foo(300)); 26 27function forLoop(n: number): number { 28 let sum = 0; 29 for (let i = 0; i < n; i++) { 30 sum++; 31 } 32 return sum; 33} 34 35function forLoopWithBreak(n: number): number { 36 let sum = 0; 37 for (let i = 0; i < n; i++) { 38 if (sum >= 5) { 39 sum++; 40 } else { 41 sum--; 42 break; 43 } 44 } 45 return sum; 46} 47 48function forLoopWithContinue(n: number): number { 49 let sum = 0; 50 for (let i = 0; i < n; i++) { 51 if (sum >= 5) { 52 sum++; 53 } else { 54 sum--; 55 continue; 56 } 57 } 58 return sum; 59} 60 61function forNestedLoop0(n: number): number { 62 let sum = 0; 63 a: for (let i = 0; i < n; i++) { 64 for (let j = 0; j < n; j++) { 65 if (sum >= 5) { 66 sum++; 67 break a; 68 } 69 sum--; 70 } 71 sum--; 72 } 73 return sum; 74} 75 76function forNestedLoop1(n: number): number { 77 let sum = 0; 78 a: for (let i = 0; i < n; i++) { 79 for (let j = 0; j < n; j++) { 80 if (sum >= 5) { 81 sum++; 82 continue a; 83 } 84 } 85 sum--; 86 } 87 return sum; 88} 89 90function forNestedLoop2(n: number): number { 91 let sum = 0; 92a: 93 for (let i = 0; i < n; i++) { 94 if (i % 2 == 0) { 95 for (let j = 0; j < n; j++) { 96 if (sum >= 5) { 97 sum++; 98 continue a; 99 } 100 } 101 return sum; 102 } 103 sum--; 104 } 105 return sum; 106} 107 108function forNestedLoop3(n: number): number { 109 let sum = 0; 110 let j = 0; 111 for (let i = 0; i < n; i++) { 112 if (i % 2 == 0) { 113 while (j < n) { 114 j++; 115 sum++; 116 } 117 return sum; 118 } 119 sum--; 120 } 121 return sum; 122} 123 124let n = 10; 125let ret1 = forLoop(n); 126print(ret1); 127let ret2 = forLoopWithBreak(n); 128print(ret2); 129let ret3 = forLoopWithContinue(n); 130print(ret3); 131let ret4 = forNestedLoop0(n); 132print(ret4); 133let ret5 = forNestedLoop1(n); 134print(ret5); 135let ret6 = forNestedLoop2(n); 136print(ret6); 137let ret7 = forNestedLoop3(n); 138print(ret7); 139let ret8 = foo(20); 140