Using if - else
if (our condition)
{
// Operations if true (if block)
}
else
{
//Operations if false (else block)
}
While writing a program, we often need to check certain situations and perform different actions in different situations. What allows us to do this is the if-else structure.
With the if (if) command, we can check whether a condition is true or not, and we can perform different operations if the condition is met, and different operations if it is not.
After the if command, the parenthesis is opened and the condition to be checked is written.
Then an if block is created with curly braces. Between these curly brackets, the if block, the operations to be performed are written if the condition is true.
For the operations to be performed when the condition is false, the else command is written and the else block is opened with curly brackets.
Else part is optional . If the condition is false, the operations to be performed are written between the parenthesis after the else command.
In the example below, the if block is used, but the else block is not. If the given condition is true, that is, if the variable a is equal to 34, "You Know" is written in label1. If the condition is false, no action will be taken.
if ( a == 34 )
{
label1.Text = "You know.";
}
In the example below, using both the if block and the else block, different operations are provided depending on whether the number is greater than 0 or not.
if ( a < 0 )
{
label1.Text = "Number is Negative.";
}
else
{
label1.Text = "Number Positive.";
}
If and/or else blocks consist of only 1 line, curly braces may not be used:
In the example below, curly braces are not used because the if and else blocks consist of one line each.
if ( a < 0 )
label1.Text = "Number is Negative.";
else
label1.Text = "Number Positive.";
c# if examples, if else tutorials, else if examples, use of elseif if not, if else explanation and if the condition is true or false, then taking action accordingly
EXERCISES
-
Program to find out if the entered number is odd or even
if (a % 2 == 0 )
label1.Text = "Even number";
else
label1.Text = "Odd number";
-
If the entered number is divisible by both 2 and 3, “OK”, otherwise “ERROR” message.
if (a % 2 == 0 && a % 3 == 0 )
label1.Text = "OK";
else
label1.Text = "ERROR";
-
"Multiple of 2 and 3" if the entered number is divisible by both 2 and 3, "multiple of 2" if it is only divisible by 2, "multiple of 3" if it is only divisible by 3, neither 2 nor 3' If it is not divisible by e, the program that gives the message "not a multiple of 2 or 3"
-
If the entered number is divisible by both 4, 5 and 6, the program that gives the message "Number OK", otherwise "Number Not Available".
-
If the entered number is divisible by at least one of the numbers 4, 5 and 6, the program that gives the message "Number OK", otherwise "Number Not Available".
-
If the entered number is between 0 and 100 and is even, the program displays "Number OK", otherwise "Number Not Available".
-
If at least one of the 3 numbers entered is greater than 50, the program giving the message "Sufficient", otherwise "Insufficient"
int a = int.Parse( TextBox1.Text );
int b = int.Parse( TextBox2.Text );
int c = int.Parse( TextBox3.Text );
if (a > 50 || b > 50 || c > 50 )
label1.Text = "OK";
else
label1.Text = "HATA";
-
The program that gives "Successful Login" if all 3 entered numbers are greater than 0 and even, otherwise "Failed login" message
int a = int.Parse( TextBox1.Text );
int b = int.Parse( TextBox2.Text );
int c = int.Parse( TextBox3.Text );
if (a > 0 && b > 0 && c > 0 && a % 2 == 0 && b % 2 == 0 && c % 2 == 0 )
label1.Text = "Succesful login";
else
label1.Text = "Login failed";
Whether a number is divisible by another number
|
|
The program that will answer the question of whether the number entered in TextBox1 can be divided by the number entered in TextBox2:
int bolunen = int.Parse( TextBox1.Text );
int bolen = int.Parse( TextBox2.Text );
if ( bolunen % bolen == 0 )
label1.Text = "Divisible";
else
label1.Text = "Not divisible";
Read 753 times.