\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{scottstoll2017 (ScottHOC)} \pdfinfo{ /Title (kotlin.pdf) /Creator (Cheatography) /Author (scottstoll2017 (ScottHOC)) /Subject (Kotlin Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{296EA3} \definecolor{LightBackground}{HTML}{F1F5F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Kotlin Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{scottstoll2017 (ScottHOC)} via \textcolor{DarkBackground}{\uline{cheatography.com/45425/cs/13463/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}scottstoll2017 (ScottHOC) \\ \uline{cheatography.com/scotthoc} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 14th November, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{x{5.9045 cm} x{5.3984 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{General Tips}} \tn % Row 0 \SetRowColor{LightBackground} Types are capitalized & You do NOT need ; at the end of lines. & Java uses "switch", Kotlin uses "when". \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Kotlin reads like English. When you see this: & Think this: & Example: \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \{\{ac\}\}: & "Is type" & var age : Int = 10 \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \{\{ac\}\}-\textgreater{} & "returns" & when (x) is 12 -\textgreater{} "It's a dozen" \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Packages and Imports are best understood by reading the info here: \{\{link="https://kotlinlang.org/docs/reference/packages.html"\}\}Packages and Imports\{\{/link\}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.4623 cm} x{8.8077 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} var is a Variable that can be changed. & val is a Value that never changes, like your name. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} val a: Int = 1 & a is initialized and it's type is specified. \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} var b = 2 & b is initialized and it's type is {\bf{{\emph{inferred}}}}. ( You do {\emph{not}} need to specify type when you declare unless the compiler guesses wrong. Normally the compiler is very good at inferring the type.) \tn % Row Count 16 (+ 10) % Row 3 \SetRowColor{white} var c : Int & c isn't initialized so it's type {\emph{must}} be specified. {\bf{This can lead to nulls which are evil JuJu in Kotlin.}} (Bad Dev, no coffee for you.) \tn % Row Count 24 (+ 8) % Row 4 \SetRowColor{LightBackground} var d : String? = "Nullable" & The '?' means this is specified with a {\bf{nullable}} string. (You are forcing the compiler to allow a null value... why?? Don't do this unless you have a compelling reason.) \tn % Row Count 33 (+ 9) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.4623 cm} x{8.8077 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Variables (cont)}} \tn % Row 5 \SetRowColor{LightBackground} var e : String = "notNullable" & This is specified with a non-nullable string. \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Avoid nulls if at all possible. This is because in Kotlin a great deal of effort has gone into trying to eliminate null pointer exceptions and the null safety that is one of Kotlin's greatest assets is undermined if you intentionally let things be nullable by using the '?'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.5427 cm} x{6.4106 cm} x{6.9167 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Number Types}} \tn % Row 0 \SetRowColor{LightBackground} The usual: & {\bf{Type}} & {\bf{Bit Width}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & Double & 64 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} & Float & 32 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} & Long & 64 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} & Int & 32 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} & Short & 16 \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} & Byte & 8 \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{Floating Point Notation:} \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} & Longs are tagged with 'L' & 1000L \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{white} & Floats are tagged with 'f' or 'F' & 1000f\{\{nl\}\}1000F \tn % Row Count 14 (+ 3) % Row 10 \SetRowColor{LightBackground} Literal \seqsplit{Constants:} & Decimal & 100\{\{nl\}\}125.5 \tn % Row Count 17 (+ 3) % Row 11 \SetRowColor{white} & Hex & 0xFF342 \tn % Row Count 18 (+ 1) % Row 12 \SetRowColor{LightBackground} & Binary & 0b101101 \tn % Row Count 19 (+ 1) % Row 13 \SetRowColor{white} & Octal & {\emph{Not Supported}} \tn % Row Count 20 (+ 1) % Row 14 \SetRowColor{LightBackground} Range & range = 1..10 & Contains all Integers from 1 to 10. \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Since Kotlin 1.1 you can make numbers more readable with underscores: \newline val oneMillion = 1\_000\_000 \newline val creditCardNumber = 1234\_5678\_9012\_3456L \newline val socialSecurityNumber = 999\_99\_9999L \newline val hexBytes = 0xFF\_EC\_DE\_5E \newline val bytes = \seqsplit{0b11010010\_01101001\_10010100\_10010010}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Visibility Modifiers:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Public}} can be seen by anyone, "who sees the declaring class".} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Internal}} can only be accessed from within the same module / package. (This is great for library authors and should be the standard for Android apps that have all the code in a single package.)} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Protected}} is visible inside the class AND subclasses.} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Private}} is visible inside the class only.} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\emph{Note:}} In an Android app that has all of it's code in one package there is never any reason to have anything be {\bf{Public}}. Instead, make all of your declarations {\bf{Internal}}, {\bf{Protected}} or {\bf{Private}}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.8801 cm} x{6.4106 cm} x{6.5793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} // Line Comment & /* Block\{\{nl\}\}Comment */ & /** KDoc\{\{nl\}\}Comment */ \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Null Safety}} \tn % Row 0 \SetRowColor{LightBackground} var a : String = null & You know better by now. This won't fly. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var a : String? = null & The '?' says you want to allow for the possibility of a null. It's not recommended unless you {\emph{have}} to make an allowance for a null, such as interoperability with Java code that is already written to allow for nulls. \tn % Row Count 13 (+ 11) % Row 2 \SetRowColor{LightBackground} Some things can cause issues, such as checking the length of a declared but uninitialized array and assigning it to a non-nullable Int: & `var a : Array`\{\{nl\}\}`var b : Int = a.length`\{\{nl\}\}Is a recipe for a crash. \tn % Row Count 20 (+ 7) % Row 3 \SetRowColor{white} Checking for a null first prevents us from shooting ourselves in the foot. We have a few ways to do it. & Using an If:\{\{nl\}\}`if(a != null) a.length else -1`\{\{nl\}\}\{\{nl\}\}Single line if's are easier than the classic if which would read:\{\{nl\}\}`if(a != null)\{\{\{nl\}\}~~~~~return a.length \{\{nl\}\}~~~~~\} else \{ \{\{nl\}\}~~~~~return -1\{\{nl\}\}\}` \tn % Row Count 35 (+ 15) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Null Safety (cont)}} \tn % Row 4 \SetRowColor{LightBackground} & Safe Calls. "Verify that this isn't a null before doing anything."\{\{nl\}\}`a?.length`\{\{nl\}\}In context, this declaration says that lengthOfArray is not allowed to be null but then the code uses a Safe Call (?) to check the length of an array that wasn't initialized:\{\{nl\}\}`var a : Array\{\{nl\}\}lengthOfArray : Int = a?.length` \tn % Row Count 17 (+ 17) % Row 5 \SetRowColor{white} Trying to assign a nullable to a non-nullable causes your computer to leak magic smoke. & `var a =5\{\{nl\}\}var b? : Int = 10\{\{nl\}\}a = b`\{\{nl\}\}//No good. Can't do that {\emph{even though neither one is null}} because 'a' is non-nullable but 'b' is nullable. This is because 'b' could potentially be null and 'a' cannot, so this won't fly even though a value was assigned. *Think of `Int` and `Int?` as two different types and you need to cast an `Int` to an `Int?`, because you do. \tn % Row Count 37 (+ 20) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Null Safety (cont)}} \tn % Row 6 \SetRowColor{LightBackground} And we all know that assigning a non-nullable value to a nullable is fine. & `var a : Int?\{\{nl\}\}var b = 5\{\{nl\}\}a = b`\{\{nl\}\}// We do this all the time \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Flow Control}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{If, Else If, Else}}\{\{nl\}\}\{\{nl\}\}`if (a \textgreater{} b) \{\{\{nl\}\}~~~~~return a \{\{nl\}\}~~~~~\} else if ( b \textless{} c ) \{ \{\{nl\}\}~~~~~return b \{\{nl\}\}~~~~~\} else \{ \{\{nl\}\}~~~~~magicSmokeLeakedOutOfLaptop()\{\{nl\}\}\}`} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{When (It's called Switch in Java and C\#)}}\{\{nl\}\}\{\{nl\}\}`when (x) \{\{\{nl\}\}\{\{nl\}\}~~~~~"Hot Dog" -\textgreater{} print("Mustard")\{\{nl\}\}\{\{nl\}\}~~~~~true -\textgreater{} lie = false\{\{nl\}\}\{\{nl\}\}~~~~~42 -\textgreater{} {\bf{"Secret is that none of the selections are in \{\} code blocks."}}\{\{nl\}\}\{\{nl\}\}~~~~~else -\textgreater{} \{ tip = {\bf{"But notice that the else statement\{\{nl\}\}~~~~~~~~~~~~~~~~~~~~IS in a code block"}}\}\{\{nl\}\}\{\{nl\}\} \}`} \tn % Row Count 19 (+ 12) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Nested For Loops}}\{\{nl\}\}\{\{nl\}\}`fun forRange() \{\{\{nl\}\}\{\{nl\}\} ~~~~~// Run 3 Outer Loops\{\{nl\}\} ~~~~~for (item in 1..3) \{\{\{nl\}\} ~~~~~~~~~~println("Outer loop \#\$item \textbackslash{}n\textbackslash{}nThe inner loop is:")\{\{nl\}\} \{\{nl\}\} ~~~~~~~~~~for (item in 1..10) \{\{\{nl\}\} ~~~~~~~~~~~~~~~// in the inner loop, print the even numbers in the range 1..10\{\{nl\}\} ~~~~~~~~~~~~~~~if (item \% 2 == 0) \{\{\{nl\}\} \seqsplit{~~~~~~~~~~~~~~~println("\$item} is even.")\{\{nl\}\} ~~~~~~~~~~\}\{\{nl\}\} ~~~~~~~~~~\}\{\{nl\}\} ~~~~~~~~~~print("Inner loop finished.\textbackslash{}n\textbackslash{}n")\{\{nl\}\} ~~~~~\}\{\{nl\}\} \}`} \tn % Row Count 42 (+ 23) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Flow Control (cont)}} \tn % Row 3 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{For Each}}\{\{nl\}\}\{\{nl\}\} `fun forArray() \{\{\{nl\}\} ~~~~~var testArray: Array\textless{}String\textgreater{} = arrayOf("First", "Second", "Third", "Fourth", "Fifth")\{\{nl\}\} ~~~~~// For each thing in the array, print each one.\{\{nl\}\} ~~~~~for (each in testArray) \{\{\{nl\}\} ~~~~~~~~~~// Yes, it's this simple\{\{nl\}\} ~~~~~~~~~~println(each)\{\{nl\}\} ~~~~~\}\{\{nl\}\} \}`} \tn % Row Count 11 (+ 11) % Row 4 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Sample While}}\{\{nl\}\} \{\{nl\}\} `fun sampleWhile() \{\{\{nl\}\} ~~~~~var count = 1\{\{nl\}\} \{\{nl\}\} ~~~~~println("\textbackslash{}nSample While:")\{\{nl\}\} \{\{nl\}\} ~~~~~while (count \textless{}= 5) \{\{\{nl\}\} ~~~~~~~~~~println("Count is: \$count")\{\{nl\}\} \{\{nl\}\} ~~~~~~~~~~count++\{\{nl\}\} ~~~~~\}\{\{nl\}\} \{\{nl\}\} \}`\{\{nl\}\} \{\{nl\}\} {\bf{Sample Out of Range While}}\{\{nl\}\} `// A while loop might never execute if it's test fails\{\{nl\}\} fun sampleOutOfRangeWhile() \{\{\{nl\}\} ~~~~~var countIsTooLarge = 10\{\{nl\}\} \{\{nl\}\} ~~~~~println("\textbackslash{}nSample Out of Range While (Won't Print)")\{\{nl\}\} \{\{nl\}\} ~~~~~// This will never execute at all because the condition\{\{nl\}\} ~~~~~// is tested before the code is executed.\{\{nl\}\} ~~~~~while (countIsTooLarge \textless{}= 5)\{\{nl\}\} ~~~~~~~~~~println("Count too large count is: \$countIsTooLarge")\{\{nl\}\} ~~~~~countIsTooLarge++\{\{nl\}\} \{\{nl\}\} \}`\{\{nl\}\} \{\{nl\}\} {\bf{Sample Do While}}\{\{nl\}\} `fun sampleDoWhile() \{\{\{nl\}\} ~~~~~var count = 1\{\{nl\}\} \{\{nl\}\} ~~~~~println("\textbackslash{}nSample Do While")\{\{nl\}\} \{\{nl\}\} ~~~~~do \{\{\{nl\}\} ~~~~~~~~~~println("Do While count is: \$count")\{\{nl\}\} ~~~~~~~~~~count++\{\{nl\}\} ~~~~~\} while (count \textless{}= 5)\{\{nl\}\} \}`\{\{nl\}\} \{\{nl\}\} {\bf{Sample Out of Range Do While}}\{\{nl\}\} `// A Do While will always exectue at least once because\{\{nl\}\} // it's test isn't performed until after the block executes.\{\{nl\}\} fun \seqsplit{sampleOutOfRangeDoWhile()} \{\{\{nl\}\} ~~~~~var count = 1\{\{nl\}\} \{\{nl\}\} ~~~~~println("\textbackslash{}nSample Out of Range Do While\textbackslash{}n(Prints once before condition is checked)")\{\{nl\}\} \{\{nl\}\} ~~~~~do \{\{\{nl\}\} ~~~~~~~~~~println("Do While count is: \$count")\{\{nl\}\} ~~~~~~~~~~count++\{\{nl\}\} ~~~~~\} while (count \textgreater{}= 5)\{\{nl\}\} \}`\{\{nl\}\}} \tn % Row Count 59 (+ 48) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Continue and Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{//Continue:}}\{\{nl\}\}`fun main(args: Array\textless{}String\textgreater{}) \{\{\{nl\}\} \{\{nl\}\} ~~~~~println("Using a not equal test to skip \#4:\textbackslash{}n")\{\{nl\}\} ~~~~~useNotEqualToSkip()\{\{nl\}\} ~~~~~println("\textbackslash{}nUsing continue to skip when count = 4:\textbackslash{}n")\{\{nl\}\} ~~~~~useContinueToSkip()\{\{nl\}\} ~~~~~println("\textbackslash{}nYou should see the exact same output.")\{\{nl\}\}\{\{nl\}\} ~~~~~println("\textbackslash{}nNow let's use break to skip out after \#3:\textbackslash{}n")\{\{nl\}\} ~~~~~useBreakToGetOut()\{\{nl\}\} \{\{nl\}\} \}\{\{nl\}\} \{\{nl\}\} fun useNotEqualToSkip() \{\{\{nl\}\} ~~~~~for (count in 1..5) \{\{\{nl\}\} ~~~~~~~~~~if (count != 4) \{\{\{nl\}\} ~~~~~~~~~~~~~~~println(count)\{\{nl\}\} ~~~~~\}\{\{nl\}\} ~~~~~\}\{\{nl\}\} \}\{\{nl\}\} \{\{nl\}\} fun useContinueToSkip() \{\{\{nl\}\} ~~~~~for (count in 1..5) \{\{\{nl\}\} ~~~~~~~~~~if (count == 4) \{\{\{nl\}\} ~~~~~~~~~~~~~~~continue\{\{nl\}\} ~~~~~~~~~~\}\{\{nl\}\} ~~~~~~~~~~println(count)\{\{nl\}\} ~~~~~\}\{\{nl\}\} \}` \{\{nl\}\}\{\{nl\}\}{\bf{//Break}}\{\{nl\}\} // This code will break you out of the loop after \#3\{\{nl\}\} `fun useBreakToGetOut() \{\{\{nl\}\} ~~~~~for (count in 1..5) \{\{\{nl\}\} ~~~~~~~~~~if (count == 4) \{\{\{nl\}\} ~~~~~~~~~~~~~~~break\{\{nl\}\} ~~~~~~~~~~\}\{\{nl\}\} ~~~~~~~~~~println(count)\{\{nl\}\} ~~~~~\}\{\{nl\}\} \}`} \tn % Row Count 41 (+ 41) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{In short: \newline continue: Go back to the beginning of the block (the for loop) \newline break: Break out of the block completely and move on. (Leave the loop.) \newline \newline For continue there are two examples, one uses a != test to skip over 4 and the other uses an if to check if the count is = 4 and then utilizes a continue to skip the rest of the code block and just go back to the beginning of the loop. \newline \newline The break is simple enough. When the break is called you exit the for loop entirely.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}