| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | title: If Else Statement | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | # If...Else Statement 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | The If...Else statement executes different blocks of code depending on the truthfulness of the specified condition. | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 04:30:38 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | ## Syntax
 | 
					
						
							| 
									
										
										
										
											2019-06-20 17:37:09 -07:00
										 |  |  | ```csharp | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | if (boolean expression) | 
					
						
							| 
									
										
										
										
											2018-10-12 04:30:38 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  |    // execute this code block if expression evalutes to true | 
					
						
							| 
									
										
										
										
											2018-10-12 04:30:38 +05:30
										 |  |  | } | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  |    // always execute this code block when above if expression is false | 
					
						
							| 
									
										
										
										
											2018-10-12 04:30:38 +05:30
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-12 04:30:38 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | ## Example
 | 
					
						
							| 
									
										
										
										
											2019-06-20 17:37:09 -07:00
										 |  |  | ```csharp | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | int Price = 30; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | if (Price == 30) | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  |     Console.WriteLine("Price is equal to 30."); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | else  | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  |     Console.WriteLine("Price is not equal to 30."); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | Since we already declared our integer `Price` to be 30, this will be the expected output. | 
					
						
							| 
									
										
										
										
											2019-03-21 17:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | ### Output
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | Price is equal to 30. | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-03-21 17:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | ## Shortened If...Else Statement
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | We can use the ternary `:?` which is great for short if...else statements. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 17:43:45 +00:00
										 |  |  | For example: | 
					
						
							| 
									
										
										
										
											2019-06-20 17:37:09 -07:00
										 |  |  | ```csharp | 
					
						
							| 
									
										
										
										
											2019-04-23 20:34:48 +05:30
										 |  |  | int Price = 30; | 
					
						
							|  |  |  | (Price == 30) ? Console.WriteLine("Price is Equal to 30.") : Console.WriteLine("Price is Not Equal to 30.") | 
					
						
							| 
									
										
										
										
											2019-03-21 17:43:45 +00:00
										 |  |  | ``` |