All the snippets below are in the order of posting. Hope these little gems help someone learn π
Happy Hacking π±βπ»
All the snippets below are in the order of posting. Hope these little gems help someone learn π
Happy Hacking π±βπ»
| // #1 - Rust Hello World! | |
| fn main() { | |
| // this is the entry point for the compiled binary | |
| // print to console | |
| println!("Hello World!"); | |
| } |
| # #10 - Python List For Iteration! | |
| # create a list to iterate over | |
| list = [1, 2, 3, 4, 5] | |
| # note the 'in' keyword | |
| # also the iteration is in order | |
| for i in list: | |
| print(i) |
| // #11 - C do-while loop! | |
| // declare a condition variable | |
| int a = 5; | |
| // do stuff until condition false | |
| do { | |
| // do something here | |
| printf("%d\n", a); | |
| // change the condition variable | |
| a--; | |
| }while( a > 0 ); |
| # create a dictionary with some world capitals | |
| # country is 'key' and capital is 'value' | |
| countryAndCapital = { | |
| 'Canada' : 'Ottawa', | |
| 'Spain' : 'Madrid', | |
| 'Austria' : 'Vienna', | |
| 'Switzerland' : 'Bern', | |
| } | |
| # iterate over the keys here | |
| # note this will be unordered! | |
| for country in countryAndCapital: | |
| print(country + ': ' + countryAndCapital[country]) |
| // #13 - Java Print Formatted String! | |
| // using string formatter & print | |
| String output = String.format("%s has %d days.", "January", 31); | |
| System.out.print(output); | |
| // using just printf | |
| System.out.printf(" And %s has %d days.", "April", 30); |
| // #14 - Golang If-else Statement! | |
| // declare something to test on | |
| num := 10 | |
| // 'if' is always the first condition checked | |
| if num == 0 { | |
| fmt.Println("number is zero!") | |
| } else if num > 0 { | |
| fmt.Println(num, "is positive!") | |
| } else { | |
| // if everything else is false this executes | |
| fmt.Println(num, "is negative!") | |
| } |
| // #15 - JavaScript For-Each! | |
| // declare an array to iterate over | |
| const elements = ['these', 'are', 'the', 'elements']; | |
| // iterate over the individual array elements | |
| elements.forEach(element => console.log(element)); |
| # #16 - Python Function! | |
| # define the function here | |
| # 'a' & 'b' are the parameters | |
| def add(a, b): | |
| # do the calculations here | |
| total = a + b | |
| # return the result at the end | |
| return total | |
| # call the function | |
| # '10' & '22' are the arguments | |
| sum = add(10, 22) | |
| # show the result | |
| print(sum) |
| // #17 - Rust Formatted String! | |
| fn main() { | |
| // this is the entry point for the compiled binary | |
| // print the formatted string | |
| println!("{} is {} years old and loves playing {}.", | |
| name = "Michael", age = "22", hobby = "tennis" | |
| ); | |
| } |
| // 18 - C++ Arrays! | |
| // uninitialized int array of size 5 | |
| int array1[5]; | |
| // initialized int array of size 5 with 0s | |
| int array2[5] = {}; | |
| // semi-initialized int array of size 5 | |
| int array3[5] = {1, 2, 3}; | |
| // fully-initialized int array of size 5 | |
| int array4[5] = {1, 2, 3, 4, 5}; | |
| // fully-initialized int array of size 5 | |
| int array5[] = {5, 4, 3, 2, 1}; |
| // #19 - Golang Map! | |
| // make a map of string -> string | |
| m := make(map[string]string) | |
| // fill in some elements | |
| m["name"] = "Matt" | |
| m["city"] = "Chicago" | |
| m["country"] = "USA" | |
| // check the content of the whole map | |
| fmt.Println(m) |
| # #2 - Python List Access! | |
| # create the list | |
| list = ["This", "is", "a", "list", "of", "some", "elements"] | |
| # access individual elements | |
| first = list[0] # "This" | |
| last = list[-1] # "elements" | |
| second = list[1] # "is" | |
| secondLast = list[-2] # "some" | |
| fifth = list[4] # "of" |
| // #3 - JavaScript For Loop! | |
| // loop 10 times - note the start is at 0! | |
| for (let i = 0; i < 10; i++) { | |
| // print the iteration values 0 through 9 | |
| console.log('iteration# ' + i); | |
| } |
| // #4 - C++ Pointer! | |
| // main function where the program enters at | |
| int main() { | |
| // initialize a null pointer using the * operator | |
| // after the type of the variable it points to | |
| int* p = nullptr; | |
| // initialize a variable of the type | |
| // you want to point to | |
| int i = 5; | |
| // use the & operator to assign the address | |
| // of the variable to pointer | |
| p = &i; | |
| // dereference using the * operator before the pointer | |
| // variable to retrieve the value it points to | |
| int j = *p; | |
| return 0; | |
| } |
| // #5 - Golang Switch Case! | |
| // value of fruit determines which | |
| // switch-case branch to execute | |
| fruit := "apple" | |
| fmt.Print("The fruit is: ") | |
| switch fruit { | |
| case "apple": | |
| fmt.Println("π") | |
| case "orange": | |
| fmt.Println("π") | |
| case "mango": | |
| fmt.Println("π₯") | |
| // default case for everything else | |
| default: | |
| fmt.Println("something else!") | |
| } |
| // #6 - C while loop! | |
| // initialize the conditions | |
| int i = 10; | |
| printf("Ready for takeoff...π₯\n"); | |
| // conditional statement | |
| while ( i > 0 ) | |
| { | |
| // do something here | |
| printf("%d...\n", i); | |
| // change the conditions here | |
| i--; | |
| } | |
| printf("Liftoff!!π"); |
| // #7 - Java If-Else! | |
| int number = 10; | |
| // check the number and print accordingly | |
| if (number > 0) { | |
| // first conditional check with if | |
| System.out.println("Number is +ve"); | |
| } else if (number == 0) { | |
| // check any other conditions with else if | |
| System.out.println("Number is 0"); | |
| } else { | |
| // if all else above is false this executes | |
| System.out.println("Number is -ve"); | |
| } |
| // #8 - Python Dictionary Access! | |
| # create the dictionary | |
| person = { | |
| "name": "Max", | |
| "age": 29, | |
| "occupation": "Programmer", | |
| "location": "Canada" | |
| } | |
| # access the dictionary element with [] | |
| pName = person["name"] |
| // #9 - C Enumeration! | |
| // declare an enum for days in a week | |
| // note the index starts at '0' by default | |
| enum day{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; | |
| // declare variable today of enum type day | |
| enum day today; | |
| // assign it Tuesday! | |
| today = Tue; |