0

"While" recursion not working

Digital Sum

8th Oct 2022, 6:03 PM
coder- N
11 odpowiedzi
0
I will have a look.Thank you very much.
9th Oct 2022, 6:32 AM
coder- N
+ 3
This is a loop, not recursion. You need to put your println statement in a new line (add a line break after the braces). In Kotlin the end of line has significance for the syntax. And you probably want to print the sum variable, because num will always end up being zero.
8th Oct 2022, 6:39 PM
Tibor Santa
Tibor Santa - avatar
+ 1
That depends on the specific code. I prefer functional coding style which involves a lot of method chaining, and programming with expressions rather than statements. in this case, to make the code shorter, sometimes I can combine several things in a single line. but if I have a complex method chain, I prefer to put each step in a new line, just to make it easier to read. If you code in imperative style and you use statements, you can only put multiple statements in the same line if you use semicolon between them. But really the point of clean Kotlin code is that you can avoid semicolons altogether. And your code is also much more readable , if it is nicely structured in separate lines.
8th Oct 2022, 6:47 PM
Tibor Santa
Tibor Santa - avatar
+ 1
I tend to be quite fussy about how I arrange and format my code, so I encourage you to check some of my Kotlin codes and see for yourself how I style them. Maybe you'll get some ideas :)
8th Oct 2022, 6:52 PM
Tibor Santa
Tibor Santa - avatar
0
fun main(args: Array<String>) { var num = readLine()!!.toInt() var sum: Int =0 while (num>0){ sum+=num%10; num=num/10; }println(num) }
8th Oct 2022, 6:04 PM
coder- N
0
Wow.it worked thank you. Loop*Sorry bout that
8th Oct 2022, 6:41 PM
coder- N
0
Quick qn.So in Kotlin...it's safer putting code in different lines.?
8th Oct 2022, 6:43 PM
coder- N
0
Mucha Gracias 👊🤝
8th Oct 2022, 6:48 PM
coder- N
0
int i = 5; while(i--){ recursiveFunction(); }
9th Oct 2022, 12:04 PM
filibuster
filibuster - avatar
0
I can't actually tell
9th Oct 2022, 12:25 PM
delight samuel
delight samuel - avatar
0
Опоаовоаьаочооаоаоаосокловов
10th Oct 2022, 1:20 PM
Валера Кравченко