Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/79422508fa2cad7549a78ff16c019df1 to your computer and use it in GitHub Desktop.
Save hygull/79422508fa2cad7549a78ff16c019df1 to your computer and use it in GitHub Desktop.
Increment & decrement operators (and their alternatives) created by hygull - https://repl.it/EmXC/5
/*
{
"created_at" : "Sat Dec 10 07:27:03 IST 2016",
"aim_of_program" : "Using increment & decrement operators(and their alternatives)"
"coded_by" : "Rishikesh Agrawani"
}
*/
package main
import "fmt"
func main() {
var i int=0
fmt.Println(i) //Prints 0
/* Note: i++ is a statement so it can't be used in any expression*/
i++ //i increment by 1
fmt.Println(i) //Prints 1
/*Uncommenting the below line will generate the error -> ./main.go:23: syntax error: unexpected ++, expecting comma or )*/
//fmt.Println(i++)
i+=1 //i increment by 1 (Alternative of i++)
fmt.Println(i) //Prints 2
i-- //i decremented by 1
fmt.Println(i) //Prints 1 as pevious value of i was 2
i-=1 //i decremented by 1 (Alternative of i--)
fmt.Println(i) //Prints 0
}
/*OUTPUT:-
0
1
2
1
0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment