From the course: Learning Go

Program conditional logic - Go Tutorial

From the course: Learning Go

Program conditional logic

- (Narrator) I've been using simple conditional logic throughout this course. But it's worth reviewing how Go's syntax for the if statement differs from other C style languages. The if statement in Go looks just like C or Java for the most part, but it doesn't require parentheses around the Boolean condition. I'll start by creating a variable integer that I'll name the answer and I'll give it a value of 42. Next, I need a string that I'll use as a result message. I'll declare it here but I'm not going to assign an initial value. I'll just say, it's going to be a string. Now, I'll add an if statement I'll use this Boolean condition the answer less than zero. Then I'll add braces, and within the braces, I'll set the value of result to a string of less than zero. Now, I can optionally add one or more elseif statements. I'll start with elseif and then I need another Boolean condition. This time, it will be the answer equals zero. And as another C style languages the double equals operator is for equality and I'll set result to equal to zero. I could add more elseif statements if I wanted to but if I don't need them, I can go directly to the else statement. And this will execute if none of the other conditions are true. And this time I'll set result to greater than zero. Now I'll use my print line output command and I'll just output that result variable. I have a misspelling up here. I'll fix that. I'll run the result and there's my expected output. And now again, if this were C or Java you would add parentheses around those Boolean conditions but in Go, you don't need them. There's also an important thing about formatting your code that I've mentioned previously but is worth restating. In other C style languages you have the option of starting your beginning brace of a code block on the next line. But if you do that in Go, you'll get an error. I'll save my changes and visual studio code shows me an error message saying there's an unexpected new line. And if I tried to run this code, it would crash. So you can't do that in Go, the opening brace has to be on the same line as the proceeding statement. Here's another interesting and sometimes useful variation in Go's if syntax. You can include an initial statement that's a part of the, if statement. And one way you can use this is to initialize variables. I'll start with an if statement down here and I'll create a variable that I'll set to a value of negative 42. Then after a semi-colon I'll continue with the rest of the, if statement the conditional expression that I'm evaluating. I'll look for X less than zero. I'll add braces, and I'm going to copy and paste this result statement and put it here. Then I'll add an elseif, and this time it'll be X equals zero, and I'll set the result, and then finally an else statement. And this is just like before I'll set the result to greater than zero. And then I'll use the print line command. and once again, output the result. I'll save the changes and run the code. And I get the result that I expect. So that's the, if, elseif, and else statements in Go. Again they're very similar in syntax to Java and C but you don't need parentheses around the Boolean conditions and you have the option of initializing values as part of the, if statement.

Contents