0% found this document useful (0 votes)
2 views6 pages

8 June Notes

The document contains notes from a Kotlin session held on June 08, 2025, covering fundamental programming concepts such as variables, data types, control flow structures, and loops. Key topics include the declaration of mutable (var) and immutable (val) variables, the use of if-else and when statements for control flow, and the implementation of for, while, and do-while loops for iteration. The session emphasized best practices, such as type consistency and avoiding infinite loops.

Uploaded by

shivamtawar075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

8 June Notes

The document contains notes from a Kotlin session held on June 08, 2025, covering fundamental programming concepts such as variables, data types, control flow structures, and loops. Key topics include the declaration of mutable (var) and immutable (val) variables, the use of if-else and when statements for control flow, and the implementation of for, while, and do-while loops for iteration. The session emphasized best practices, such as type consistency and avoiding infinite loops.

Uploaded by

shivamtawar075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Kotlin Session Notes - June 08, 2025

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 Variables and Data Types


1.1 Variable Declaration
Kotlin provides two types of variables:
• var: Mutable variables whose values can be changed after initialization.
• val: Immutable variables (constants) whose values cannot be reassigned once set.
Key points:
• Kotlin uses type inference to automatically determine the data type of a variable based
on its initial value.
• Explicit type declaration is optional but useful for clarity or when type inference might
be ambiguous.
• Example: var age = "25" infers age as a String, while var age: String
= "25" explicitly declares it.

1.2 Code Example


1 fun main() {
2 var age = "25" // Inferred as String
3 var age2 = "2" // Inferred as String
4 val year_of_birth = 2009 // Inferred as Int, immutable
5 age = 21 // Attempted reassignment (note: type mismatch)
6
7 var age: String = "25" // Explicitly defined as String
8 println(age) // Printing age
9 }
Observation: The reassignment age = 21 causes a type mismatch because age was inferred
as a String, but 21 is an Int. To fix, use age = "21" to maintain type consistency.

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.

2 Control Flow: If-Else and When Statements


2.1 If-Else Statements
If-else statements control program flow based on boolean conditions (true or false).
• Syntax:
1 if (condition) {
2 // code to execute if condition is true
3 } else {
4 // code to execute if condition is false
5 }
6

• Conditions use comparison operators: >= (greater than or equal), == (equal), etc.
• Multiple conditions can be chained with else if.

2.2 If-Else Example


1 fun main() {
2 val age = 19
3

4 if (age >= 18) {


5 println("person can vote")
6 } else if (age == 0) {
7 println("invalid age")
8 } else {
9 println("sorry you cannot vote")
10 }
11 }

2
Explanation:

• val age = 19: Immutable variable set to 19.

• age >= 18: Checks if age is 18 or older, prints "person can vote" (true for 19).

• age == 0: Checks if age is 0, prints "invalid age" if true.

• else: Default case, prints "sorry you cannot vote" if all conditions fail.

• Output: "person can vote" because 19 is greater than or equal to 18.

2.3 When Statements


The when statement is a powerful alternative to if-else, matching a variables value against
multiple options.

• Syntax:
1 when (variable) {
2 value1 -> // code
3 value2 -> // code
4 else -> // default code
5 }
6

• Matches the exact value of the variable, not a boolean condition.

• The else branch handles any unmatched cases.

2.4 When Example


1 fun main() {
2 val grade = ’D’
3
4 when (grade) {
5 ’A’ -> println("Excellent")
6 ’B’ -> println("Good Job")
7 ’C’ -> println("We need to do some improvements")
8 else -> println("invlaid grade or you have failed")
9 }
10 }

Explanation:

• val grade = ’D’: Immutable variable set to character ’D’.

• when checks grade against ’A’, ’B’, ’C’.

• If no match, the else branch executes.

• Output: "invlaid grade or you have failed" because ’D’ is not ’A’, ’B’, or ’C’.

• Note: Typo in "invlaid" should be corrected to "invalid".

3
2.5 Key Difference
• If-Else: Evaluates boolean conditions (e.g., age >= 18 checks if true).

• When: Matches exact values (e.g., grade is ’A’, ’B’, etc.).

• 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.

3.1 For Loop


• Purpose: Iterates over a range, array, or collection.

• Syntax:
1 for (i in 1..5) {
2 // code
3 }
4

• 1..5 creates a range from 1 to 5 (inclusive).

• downTo reverses the direction of the range.

Examples:
1 for (i in 1..5) {
2 println(i)
3 }

• Output: Prints 1, 2, 3, 4, 5 (one per line).

1 for (i in 5 downTo 4) {
2 println(i)
3 }

• Output: Prints 5, 4 (descending order).

• Note: Useful for countdowns or reverse iteration.

3.2 While Loop


• Purpose: Executes as long as a condition is true.

• 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 }

• Explanation: Starts with i = 5, prints i, decrements by 1, stops when i > 2 is false.

• Output: 5, 4, 3.

1 var a = 1
2 while (a < 12) {
3 println(a)
4 a++
5 if (a == 8) break
6 }

• Explanation: Starts with a = 1, prints a, increments by 1, breaks when a == 8.

• Output: 1, 2, 3, 4, 5, 6, 7.

• break: Exits the loop immediately when condition a == 8 is met.

1 var i = 1
2 while (i > 0) {
3 println(i)
4 }

• Explanation: Starts with i = 1, condition i > 0 is always true, no modification to


i.

• Output: Prints 1 repeatedly (infinite loop).

• Caution: Infinite loops can crash programs; always ensure a condition to exit (e.g.,
increment i or use break).

3.3 Do-While Loop


• Purpose: Executes code at least once, then checks the condition.

• Syntax:
1 do {
2 // code
3 } while (condition)
4

• Condition is checked after each iteration.

Example (Commented Out):

5
1 do {
2 println(i)
3 i++ // i + 1
4 } while (i < 0)

• Explanation: Would print i, increment it, then check i < 0.

• If i = 1 initially, condition i < 0 is false, so loop runs only once.

• Note: Code was commented out in class, not executed.

• Difference from While: do-while guarantees at least one execution, even if the con-
dition is initially false.

3.4 Loop Control


• break: Terminates the loop immediately, as seen in the a == 8 example.

• 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.

4 Key Takeaways and Notes


• Variables: Use var for mutable, val for immutable. Watch for type mismatches (e.g.,
age = 21 should be "21" for String).

• Control Flow: If-else checks boolean conditions; when matches exact values. Correct
typos like "invlaid" to "invalid".

• Loops:

– for: Ideal for known ranges (e.g., 1..5).


– while: Runs while condition is true; beware of infinite loops.
– do-while: Runs at least once, useful for initial execution needs.

• Caution: The while (i > 0) loop is infinite without a break or modification to i.


Always test loops for termination.

• Purpose: This session laid the foundation for Kotlin programming, covering variable
declaration, basic data types, control flow, and iteration.

You might also like