Comments
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 3 of 93.
Comments are used to add explanatory notes within the code. They are ignored by the compiler and are meant for human understanding.
In Kotlin, there are two ways to write comments:
Single-line comments using //:
fun main() {
// This is a comment
println("Hello!") // This prints "Hello!"
// You can use comments to explain your code
println("Kotlin is fun!") // This will show on the screen
}Multi-line comments using /* */:
fun main() {
/*
This is a multi-line comment.
You can write multiple lines
of explanation here.
*/
println("Welcome!")
}Common Comment Practices:
1. TODO Comments - Mark future tasks:
fun main() {
// TODO: Add error handling for invalid inputs
println("Processing...")
}2. Disabling code temporarily:
fun main() {
// println("This line is disabled")
println("This line will execute")
}3. Explaining complex logic:
fun main() {
// Calculate the average of the list
val total = 100
val count = 5
val average = total / count
println(average)
}Challenge
BeginnerFix the code so that only Hello, Kotlin! is printed.
- Replace the
?with the symbols that turn a line into a comment - The line printing
Goodbye!should become a comment so it does NOT run - Only change the line that starts with
?
Try it yourself
fun main() {
// Type your code below
? println("Goodbye!")
println("Hello, Kotlin!")
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground