12/30/2023 0 Comments For loop kotlin with indexLet’s discuss an example of for loop using an array with an Index. The above program will take the array and will print the output inserted in the loop as below: Var language = arrayOf("Loop1", "Loop2", "Loop3", "Loop4") The output of the above program will be:. Val newArray = arrayOf("fo", "r", "lo", "op")įor the above program, the output will be the string mentioned in the array and enclosed in “ ” Let’s explore a few examples using an Array: In the above example, the loop will run from 6 to 1 range as the method here is downTo which runs from higher to lower value and will skip the value because of step 2 and will print 642 Print("loop prints(a in 6 downTo 1 step 2) print(a) = ") In the above program, the loop will start running and will print the value from 4 to 1 as follows: Print("loops prints (x in 4 downTo 1) print(x) = ") There are different ways to use for loop a few are the below examples:- Example #1 So 1.50 would mean a number from 1 to 50.Kotlin for loop allows you to rerun the same lines of code but is easier for the developers.Kotlin only reduces the code with the same for loop functionality.Kotlin for loop does the same thing as other languages for loop.Once a value is initialized in the for loop, the loop works until the final value is filled, and then it prints the value.For loop are typically used when the number of iterations is known beforehand.For loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly.So when you run the program, the output will be: In the above example, the for loop will run and return eight times as the range given is from 1 to 8, so it will print numbers in the range. In Kotlin, for loop is used to perform through ranges, arrays, etc.įor example, if you want to create a for loop to print numbers from 1 to 8, your program will be:- fun main(args:Array)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |