8 June Notes
8 June Notes
Session Overview
This document provides detailed notes from the Kotlin session conducted on June 08, 2025, as
part of a one-month-long program. The session introduced fundamental Kotlin programming
concepts, including variables, data types, control flow structures (if-else and when statements),
and loops (for, while, and do-while). Below, each topic is explained in detail with examples,
key points, and practical insights from the code demonstrated in class.
1
1.3 Data Types
Kotlin supports several basic data types:
• Int: Represents whole numbers (integers), e.g., 23, 89, 76.
• Double: Represents decimal numbers (floating-point), e.g., 23.8, 78.967.
• Char: Represents single characters, e.g., ’A’, ’B’, ’z’.
• Boolean: Represents logical values, either true or false.
• String: Represents text or a sequence of characters, e.g., "Hello", "hello my name is
shivam".
Key Points:
• Use single quotes (’) for Char and double quotes (") for String.
• Type inference reduces boilerplate, but explicit declaration enhances readability and pre-
vents errors.
• The println() function outputs variable values or text to the console.
• Conditions use comparison operators: >= (greater than or equal), == (equal), etc.
• Multiple conditions can be chained with else if.
2
Explanation:
• age >= 18: Checks if age is 18 or older, prints "person can vote" (true for 19).
• else: Default case, prints "sorry you cannot vote" if all conditions fail.
• Syntax:
1 when (variable) {
2 value1 -> // code
3 value2 -> // code
4 else -> // default code
5 }
6
Explanation:
• Output: "invlaid grade or you have failed" because ’D’ is not ’A’, ’B’, or ’C’.
3
2.5 Key Difference
• If-Else: Evaluates boolean conditions (e.g., age >= 18 checks if true).
• Use if-else for range-based or logical checks, and when for discrete value matching.
3 Loops
Loops allow repeated execution of code. Kotlin offers three main types: for, while, and do-
while.
• Syntax:
1 for (i in 1..5) {
2 // code
3 }
4
Examples:
1 for (i in 1..5) {
2 println(i)
3 }
1 for (i in 5 downTo 4) {
2 println(i)
3 }
• Syntax:
1 while (condition) {
2 // code
3 }
4
4
• Condition is checked before each iteration.
Examples:
1 var i = 5
2 while (i > 2) {
3 println(i)
4 i--
5 }
• Output: 5, 4, 3.
1 var a = 1
2 while (a < 12) {
3 println(a)
4 a++
5 if (a == 8) break
6 }
• Output: 1, 2, 3, 4, 5, 6, 7.
1 var i = 1
2 while (i > 0) {
3 println(i)
4 }
• Caution: Infinite loops can crash programs; always ensure a condition to exit (e.g.,
increment i or use break).
• Syntax:
1 do {
2 // code
3 } while (condition)
4
5
1 do {
2 println(i)
3 i++ // i + 1
4 } while (i < 0)
• Difference from While: do-while guarantees at least one execution, even if the con-
dition is initially false.
• continue (not shown): Skips the current iteration and proceeds to the next.
• The statement println("loop break") was printed after the loop, not tied to loop
control but indicates loop context.
• Control Flow: If-else checks boolean conditions; when matches exact values. Correct
typos like "invlaid" to "invalid".
• Loops:
• Purpose: This session laid the foundation for Kotlin programming, covering variable
declaration, basic data types, control flow, and iteration.