{"id":2088,"date":"2020-05-02T09:23:33","date_gmt":"2020-05-02T09:23:33","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2088"},"modified":"2020-11-25T00:46:01","modified_gmt":"2020-11-24T19:16:01","slug":"understand-if-else-statement-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/understand-if-else-statement-golang\/","title":{"rendered":"Understand If Else Statement in Go (Golang)"},"content":{"rendered":"\n
This is the\u00a0 chapter 12 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series<\/a><\/p>\n\n\n\n Next Tutorial<\/strong>\u00a0\u2013\u00a0Switch<\/a> Now let\u2019s check out the current tutorial. Below is the table of contents for current tutorial.<\/p>\n\n\n\n Go has if-else statement similar to any other programming language to perform the basic conditional logic. Below is the format for if-else statement in golang<\/p>\n\n\n\n Before we move further let’s talk about the condition<\/strong> first. Only a statement or a combination of statements that result in a boolean are allowed for a condition in if. false boolean is treated as false in a condition in go and true boolean is treated as true. As mentioned above, the condition can be composed of multiple statements combined by operators in Go such as &&, ||, >, <, >=, <=, ! etc.<\/p>\n\n\n\n Now let’s look into the if-else statement in detail to understand the small things. Go supports below formats for the if-else statement<\/p>\n\n\n\n if statement alone has below format<\/p>\n\n\n\n If the condition is true then the statement inside the braces is executed. Some points to note about if statement<\/p>\n\n\n\n Let’s see a working example. Below program checks if a number is greater than 5.<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n Let’s see another example of multiple statement in a if condition. Below is a program to check if a number lies in a particular range. Notice that multiple statement in the condition are joined by the && operator.<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n If Else statement has below format<\/p>\n\n\n\n If the condition is true then the statement inside the if block is executed otherwise the statement inside the else block is executed. Some points to note about if-else statement.<\/p>\n\n\n\n Let's see a small example of if else statement. In below program we use if else statement to figure out the max number of 2 numbers<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n If Else ladder has the below format<\/p>\n\n\n\n Some points to note about this if else ladder<\/p>\n\n\n\n Below is a working code example. The code given an age is using a if else ladder to find out weather a person is \"Kid\", \"Young\" or \"Old\".<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Below are some one of the possible format for nested if else.<\/p>\n\n\n\n Only nested if<\/p>\n\n\n\n Nested if else<\/p>\n\n\n\n Below combination is also possible for nested if else<\/p>\n\n\n\n Let's see a working example of nested if else. In below program we print the max of three numbers using nested if else.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n If statement also supports a statement before the condition. This statement will be executed before the condition. There can also be new initialized variable in the statement. Below is the format for that.<\/p>\n\n\n\n The initialization if present in the statement will be a short declaration. Notice that var keyword is not supported in the statement. Let's see a working example<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n The variable that is initialized in if statement is available inside all the branches. As in below example variable a<\/strong> is also available in the else block.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n We mentioned at the start that only boolean values or statement that result in boolean value are allowed in the if condition. Let's see a working code of the error that comes in case of using any else than boolean<\/p>\n\n\n\n Output: <\/strong>Below compiler error is raised<\/p>\n\n\n\n There is no ternary operator in Go, hence you need to use if else statements in place of that.<\/p>\n\n\n\n That is all about if else statement in go. Hope you have liked this article. Please share feedback\/improvements\/mistakes in comments<\/p>\n\n\n\n Next Tutorial<\/strong>\u00a0\u2013\u00a0Switch<\/a> This is the\u00a0 chapter 12 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series Next Tutorial\u00a0\u2013\u00a0SwitchPrevious Tutorial\u00a0\u2013 For Range loop Now…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[16,3,4,17,184,185],"class_list":["post-2088","post","type-post","status-publish","format-standard","hentry","category-tech","tag-complete","tag-go","tag-golang","tag-guide","tag-understand","tag-using"],"yoast_head":"\n
Previous Tutorial<\/strong>\u00a0\u2013 For Range loop<\/a><\/p>\n\n\n\nOverview<\/strong><\/h1>\n\n\n\n
if condition {\n \/\/Do something\n} else if condition {\n \/\/Do something\n} else {\n \/\/Do something<\/code><\/pre>\n\n\n\n
If statement<\/strong><\/h2>\n\n\n\n
if condition {\n \/\/Do something\n}<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n a := 6\n if a > 5 {\n fmt.Println(\"a is greater than 5\")\n }\n}<\/code><\/pre>\n\n\n\n
a is greater than 5<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n a := 4\n if a > 3 && a < 6 {\n fmt.Println(\"a is within range\")\n }\n}<\/code><\/pre>\n\n\n\n
a is within range<\/code><\/pre>\n\n\n\n
If Else Statement<\/strong><\/h2>\n\n\n\n
if condition {\n \/\/Do something\n} else {\n \/\/Do something\n}<\/code><\/pre>\n\n\n\n
syntax error: unexpected else, expecting }<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n a := 1\n b := 2\n\n if a > b {\n fmt.Println(\"a is greater than b\")\n } else {\n fmt.Println(\"b is greater than a\")\n }\n}<\/code><\/pre>\n\n\n\n
b is greater than a<\/code><\/pre>\n\n\n\n
If Else Ladder<\/strong><\/h2>\n\n\n\n
if condition1 {\n \/\/Do something\n} else if condition2 {\n \/\/Do something\n} else {\n \/\/Do something\n}<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n age := 29\n if age < 18 {\n fmt.Println(\"Kid\")\n } else if age >= 18 && age < 40 {\n fmt.Println(\"Young\")\n } else {\n fmt.Println(\"Old\")\n }\n}<\/code><\/pre>\n\n\n\n
Young<\/code><\/pre>\n\n\n\n
Nested If Else<\/strong><\/h2>\n\n\n\n
if condition {\n \/\/Do something\n if condition2 { \n \/\/Do something\n }\n \/\/Do something\n}<\/code><\/pre>\n\n\n\n
if condition1 {\n \/\/....\n if condition2 {\n \/\/...\n } else {\n \/\/...\n }\n \/\/...\n}<\/code><\/pre>\n\n\n\n
if condition1 {\n \/\/...\n} else {\n \/\/...\n if condition2 {\n \/\/...\n } else {\n \/\/....\n }\n \/\/....\n}<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n a := 1\n b := 2\n c := 3\n if a > b {\n if a > c {\n fmt.Println(\"Biggest is a\")\n } else if b > c {\n fmt.Println(\"Biggest is b\")\n }\n } else if b > c {\n fmt.Println(\"Biggest is b\")\n } else {\n fmt.Println(\"Biggest is c\")\n }\n}<\/code><\/pre>\n\n\n\n
Biggest is c<\/code><\/pre>\n\n\n\n
If with short statement<\/strong><\/h2>\n\n\n\n
if statement; condition {\n \/\/Do something\n}<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n if a := 6; a > 5 {\n fmt.Println(\"a is greater than 5\")\n }\n}<\/code><\/pre>\n\n\n\n
a is greater than 5<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n if a := 1; a > 5 {\n fmt.Println(\"a is greater than 5\")\n } else {\n fmt.Println(\"a is less than 5\")\n }\n}<\/code><\/pre>\n\n\n\n
a is less than 5<\/code><\/pre>\n\n\n\n
If Conditions<\/strong><\/h1>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n if 1 {\n fmt.Println(\"a is greater than 5\")\n }\n}<\/code><\/pre>\n\n\n\n
non-bool 1 (type int) used as if condition<\/code><\/pre>\n\n\n\n
Ternary Operator<\/strong><\/h1>\n\n\n\n
Conclusion<\/strong><\/h1>\n\n\n\n
Previous Tutorial<\/strong>\u00a0\u2013 For Range loop<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"