Created
December 10, 2016 02:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
{ | |
"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