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
C# if...else if (if-then-else if) Statement. When we have only one condition to test, if-then and if-then-else statement works fine. But what if we have a multiple condition to test and execute one of the many block of code. For such case, we can use if..else if statement in C#. The syntax for if...else if statement is:
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.
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.
13 mar 2023 · The if statement checks the given condition. If the condition evaluates to be true then the block of code/statements will execute otherwise not. Syntax: if(condition) { . //code to be executed . } .
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 ...
When an if statement's condition evaluates to true, C# executes all code between its braces ({ and }). This way we code decisions in our C# program.