Search results
13 sty 2024 · C# Sharp Conditional Statement [25 exercises with solution] [An editor is available at the bottom of the page to write and execute the scripts. Go to the editor] 1. Write a C# Sharp program to accept two integers and check whether they are equal or not. Test Data : Input 1st number: 5 Input 2nd number: 5 Expected Output: 5 and 5 are equal
The syntax of if...else statement in C# is: if (boolean-expression) { // statements executed if boolean-expression is true } else { // statements executed if boolean-expression is false } For example, if (number < 5) { number += 5; } else { number -= 5; } In this example, the statement. number += 5;
12 mar 2009 · if(...) { // ... } else if (...) { // ... } else { // ... } This is the safest and most comprehensible way to write if-else-blocks. For one liners (true one liners that are comprehensible on one line), you can use the ternary operator.
Examples of C#’s if/else statements To better understand how if/else statements work and how we make them, let’s code a couple of them. Quick example A basic example of an if/else statement is: Console. Write ("How old are you? "); int userAge = Convert. ToInt32 (Console. ReadLine ()); if (userAge >= 18) {Console. WriteLine ("You're old ...
If Else Statements in C# Language: The If-Else block in C# Language is used to provide some optional information whenever the given condition is FALSE in the if block. That means if the condition is true, then the if block statements will be executed, and if the condition is false, then the else block statement will execute.
24 cze 2020 · Syntax: if(condition) { // code block to be executed when if condition evaluates to true. } Example: if Statement. int i = 10, j = 20; if (i < j) { Console.WriteLine("i is less than j"); } . if (i > j) { Console.WriteLine("i is greater than j"); } Try it. Output: i is less than j.
C# has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.