diff --git a/hacking-go/learn04/if/ifelse.go b/hacking-go/learn04/if/ifelse.go index a0fb799..7345387 100644 --- a/hacking-go/learn04/if/ifelse.go +++ b/hacking-go/learn04/if/ifelse.go @@ -3,11 +3,14 @@ package main import "fmt" func main() { - if var1 := 10; var1 > 20 { + if var1 := 5; var1 > 20 { fmt.Println("Value inside if:", var1) } else { - // Can use var1 here - fmt.Println("Value inside else:", var1) + if var1 > 5 { + fmt.Println("Value inside nested if:", var1) // This block executes if var1 <= 20 and var1 > 5 + } else { + fmt.Println("Value inside nested else:", var1) // This block executes if var1 <= 5 + } } } diff --git a/hacking-go/learn04/if/ifelse1.go b/hacking-go/learn04/if/ifelse1.go new file mode 100644 index 0000000..58cc080 --- /dev/null +++ b/hacking-go/learn04/if/ifelse1.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + var1 := 3 + if var1 > 20 { + fmt.Println("Value inside if:", var1) + } else { + if var1 > 5 { + fmt.Println("Value inside nested if:", var1) // This block executes if var1 <= 20 and var1 > 5 + } else { + fmt.Println("Value inside nested else:", var1) // This block executes if var1 <= 5 + } + } + +} diff --git a/hacking-go/learn04/if/ifelse2.go b/hacking-go/learn04/if/ifelse2.go new file mode 100644 index 0000000..18b84f6 --- /dev/null +++ b/hacking-go/learn04/if/ifelse2.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func main() { + var var1 int = 10 // Declare and initialize var1 + + if var1 > 20 { + fmt.Println("var1 is greater than 20") + } else { + if var1 > 15 { + fmt.Println("var1 is greater than 15 but less than or equal to 20") + } else { + if var1 > 10 { + fmt.Println("var1 is greater than 10 but less than or equal to 15") + } else { + fmt.Println("var1 is 10 or less") + } + } + } +} diff --git a/hacking-go/learn04/if/ifelse3.go b/hacking-go/learn04/if/ifelse3.go new file mode 100644 index 0000000..8fd9c83 --- /dev/null +++ b/hacking-go/learn04/if/ifelse3.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func main() { + var var1 int = 10 + var var2 int = 25 + + if var1 > 20 && var2 > 30 { + fmt.Println("var1 is greater than 20 and var2 is greater than 30") + } else if var1 > 15 || var2 > 20 { + fmt.Println("Either var1 is greater than 15 or var2 is greater than 20") + } else if var1 == 10 { + fmt.Println("var1 is exactly 10") + } else { + fmt.Println("None of the conditions were met") + } +} diff --git a/hacking-go/learn04/switch/switch.go b/hacking-go/learn04/switch/switch.go new file mode 100644 index 0000000..91601da --- /dev/null +++ b/hacking-go/learn04/switch/switch.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "math/rand" // This is not cryptographically secure! + "time" +) + +func main() { + // Seeding rand + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + switch num := rand.Intn(3); num { + case 1: + fmt.Println("1") + case 2: + fmt.Println("2") + default: + fmt.Println("3") + } +} diff --git a/hacking-go/learn04/switch/switch1.go b/hacking-go/learn04/switch/switch1.go new file mode 100644 index 0000000..7a379a7 --- /dev/null +++ b/hacking-go/learn04/switch/switch1.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func main() { + // Seeding rand with the current time in nanoseconds to get different results each time + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + + // Generating a random number between 0 and 2 (inclusive) + switch num := rand.Intn(3); num { + case 1: + fmt.Println("1") + case 2: + fmt.Println("2") + default: + fmt.Println("3") // This will handle the case when num is 0 + } +} diff --git a/hacking-go/learn04/switch/switch2.go b/hacking-go/learn04/switch/switch2.go new file mode 100644 index 0000000..db15800 --- /dev/null +++ b/hacking-go/learn04/switch/switch2.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + var num int + fmt.Println("Please enter a number:") + + // Reading input from the user + _, err := fmt.Scanln(&num) + if err != nil { + fmt.Println("Please enter number type int, error reading input:", err) + return + } + + fmt.Println("You entered:", num) + + // Using switch statement to handle different cases + switch num { + case 1: + fmt.Println("You entered one.") + case 2: + fmt.Println("You entered two.") + default: + fmt.Println("You entered a number other than one or two.") + } +} diff --git a/hacking-go/learn04/switch/switch3.go b/hacking-go/learn04/switch/switch3.go new file mode 100644 index 0000000..3583d1d --- /dev/null +++ b/hacking-go/learn04/switch/switch3.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func main() { + var var1 int = 10 // Declare and initialize var1 + + switch { + case var1 > 20: + fmt.Println("var1 is greater than 20") + case var1 > 15: + fmt.Println("var1 is greater than 15 but less than or equal to 20") + case var1 > 10: + fmt.Println("var1 is greater than 10 but less than or equal to 15") + default: + fmt.Println("var1 is 10 or less") + } +} diff --git a/hacking-go/learn04/switch/switch4.go b/hacking-go/learn04/switch/switch4.go new file mode 100644 index 0000000..c0c24bd --- /dev/null +++ b/hacking-go/learn04/switch/switch4.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" +) + +func main() { + var num int + fmt.Println("Please enter a number:") + + // Reading input from the user + _, err := fmt.Scanln(&num) + if err != nil { + fmt.Println("Please enter number type int, error reading input:", err) + return + } + + fmt.Println("You entered:", num) + + // Using a switch statement to handle different cases + switch { + case num < 0: + fmt.Println("You entered a negative number.") + case num == 0: + fmt.Println("You entered zero.") + case num > 0 && num <= 10: + fmt.Println("You entered a positive number between 1 and 10.") + case num > 10 && num <= 20: + fmt.Println("You entered a positive number between 11 and 20.") + fallthrough + case num > 20 && num <= 30: + fmt.Println("You entered a positive number between 21 and 30.") + fallthrough + case num > 30: + fmt.Println("You entered a number greater than 30.") + default: + fmt.Println("This case should never be reached.") + } + + // Example of handling multiple specific cases + switch num { + case 1, 2, 3: + fmt.Println("You entered a small number (1, 2, or 3).") + case 10, 20, 30: + fmt.Println("You entered a round number (10, 20, or 30).") + default: + fmt.Println("You entered a number that is neither small nor round.") + } +} diff --git a/hacking-go/learn04/switch/switch5.go b/hacking-go/learn04/switch/switch5.go new file mode 100644 index 0000000..5b12ff4 --- /dev/null +++ b/hacking-go/learn04/switch/switch5.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" +) + +func main() { + var num int + fmt.Println("Please enter a number:") + + // Reading input from the user + _, err := fmt.Scanln(&num) + if err != nil { + fmt.Println("Error reading input:", err) + return + } + + fmt.Println("You entered:", num) + + // Using a switch statement to handle different cases + switch { + case num < 0: + fmt.Println("You entered a negative number.") + case num == 0: + fmt.Println("You entered zero.") + case num > 0 && num <= 10: + if num == 5 { + fmt.Println("You entered exactly five.") + } else { + fmt.Println("You entered a positive number between 1 and 10.") + } + case num > 10 && num <= 20: + if num%2 == 0 { + fmt.Println("You entered an even number between 11 and 20.") + } else { + fmt.Println("You entered an odd number between 11 and 20.") + } + fallthrough + case num > 20 && num <= 30: + if num%5 == 0 { + fmt.Println("You entered a multiple of 5 between 21 and 30.") + } else { + fmt.Println("You entered a number between 21 and 30.") + } + fallthrough + case num > 30: + if num > 50 { + fmt.Println("You entered a number greater than 50.") + } else { + fmt.Println("You entered a number between 31 and 50.") + } + default: + fmt.Println("This case should never be reached.") + } + + // Example of handling multiple specific cases with nested if-else + switch num { + case 1, 2, 3: + fmt.Println("You entered a small number (1, 2, or 3).") + if num == 2 { + fmt.Println("Two is the only even small number.") + } + case 10, 20, 30: + fmt.Println("You entered a round number (10, 20, or 30).") + if num == 20 { + fmt.Println("Twenty is a nice round number.") + } + default: + fmt.Println("You entered a number that is neither small nor round.") + } +} diff --git a/hacking-go/learn04/switch/switch6-advan.go b/hacking-go/learn04/switch/switch6-advan.go new file mode 100644 index 0000000..b6fad1f --- /dev/null +++ b/hacking-go/learn04/switch/switch6-advan.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "math/rand" // This is not cryptographically secure! + "time" +) + +func main() { + // Seeding rand + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + num := rand.Intn(100) // Generate a random number between 0 and 99 + fmt.Printf("Random number is: %d\n", num) + + // Using switch with advanced conditions and nested if-else + switch { + case num < 20: + fmt.Println("Less than 20") + if num < 10 { + fmt.Println("Less than 10") + } else { + fmt.Println("Between 10 and 19") + } + case num >= 20 && num < 50: + fmt.Println("Between 20 and 49") + if num%2 == 0 { + fmt.Println("Even number") + } else { + fmt.Println("Odd number") + } + case num >= 50 && num < 80: + fmt.Println("Between 50 and 79") + if num%5 == 0 { + fmt.Println("Multiple of 5") + } else { + fmt.Println("Not a multiple of 5") + } + default: + fmt.Println("80 or more") + if num > 90 { + fmt.Println("Greater than 90") + } else { + fmt.Println("Between 80 and 89") + } + } + + // Example of handling multiple specific cases with nested if-else + switch num { + case 1, 2, 3: + fmt.Println("You entered a small number (1, 2, or 3).") + if num == 2 { + fmt.Println("Two is the only even small number.") + } + case 10, 20, 30: + fmt.Println("You entered a round number (10, 20, or 30).") + if num == 20 { + fmt.Println("Twenty is a nice round number.") + } + default: + fmt.Println("You entered a number that is neither small nor round.") + } +} diff --git a/hacking-go/learn04/switch/switch6.go b/hacking-go/learn04/switch/switch6.go new file mode 100644 index 0000000..796b1de --- /dev/null +++ b/hacking-go/learn04/switch/switch6.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "math/rand" // This is not cryptographically secure! + "time" +) + +func main() { + // Seeding rand + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + switch num := rand.Intn(100); { + case num < 50: + fmt.Println("Less than 50") + default: + fmt.Println("More than 50") + } +} diff --git a/hacking-go/learn04/switch/switch7-advan.go b/hacking-go/learn04/switch/switch7-advan.go new file mode 100644 index 0000000..9471875 --- /dev/null +++ b/hacking-go/learn04/switch/switch7-advan.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "math/rand" // This is not cryptographically secure! + "time" +) + +func main() { + // Seeding rand + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + num := rand.Intn(100) // Generate a random number between 0 and 99 + fmt.Printf("Random number is: %d\n", num) + + // Using switch with advanced conditions and nested if-else + switch { + case num < 20: + fmt.Println("Less than 20") + if num < 10 { + fmt.Println("Less than 10") + } else { + fmt.Println("Between 10 and 19") + } + case num >= 20 && num < 50: + fmt.Println("Between 20 and 49") + if num%2 == 0 { + fmt.Println("Even number") + } else { + fmt.Println("Odd number") + } + case num >= 50 && num < 80: + fmt.Println("Between 50 and 79") + if num%5 == 0 { + fmt.Println("Multiple of 5") + } else { + fmt.Println("Not a multiple of 5") + } + default: + fmt.Println("80 or more") + if num > 90 { + fmt.Println("Greater than 90") + } else { + fmt.Println("Between 80 and 89") + } + } +} diff --git a/hacking-go/learn04/switch/switch7.go b/hacking-go/learn04/switch/switch7.go new file mode 100644 index 0000000..8fcaaaf --- /dev/null +++ b/hacking-go/learn04/switch/switch7.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math/rand" // This is not cryptographically secure! + "time" +) + +func main() { + // Seeding rand + rand.Seed(time.Now().UnixNano()) + fmt.Println("Choosing a random number:") + num := rand.Intn(100) + switch { + case num < 50: + fmt.Println("Less than 50") + default: + fmt.Println("More than 50") + } +}