Visual Programming Lectures using Visual Studio 2015 C# Windows Form Application
Lecturer: Saman M. Almufti / Kurdistan Region, Nawroz University
facebook: https://www.facebook.com/saman.malmufti
3. C# IF ELSE STATEMENTS
The conditional statement if.. else in C# is using for check the conditions that we
provided in the head of if statement and making decision based on that condition.
The conditional statement examining the data using comparison operators as well as
logical operators. The else statement is optional , so we can use the statement in two
ways ;
if (condition)
{
statement;
}
else if (condition)
{
statement;
}
else
{
statement;
}
if (condition)
{
statement;
}
if (condition)
{
statement;
}
else
{
statement;
}
5. int a;
a = Convert.ToInt16(textBox1.Text);
if(a >=50)
{
textBox3.Text = “PASS”;
}
else {
textBox3.Text = “Fail”;
}
EXAMPLE 1
Using if statement to check if the student are passed or
failed
6. int a;
a =Convert.ToInt16 (textBox1.Text);
if(a<50)
{ textBox2.Text = "Fail"; }
else if (a >= 50&& a<60)
{ textBox2.Text = "Pass"; }
else if (a >= 60 && a < 70)
{ textBox2.Text = "Fair"; }
else if (a >= 70 && a < 80)
{ textBox2.Text = "Good"; }
else if (a >= 80 && a < 90)
{ textBox2.Text = "Vary Good";}
else if (a >= 90 && a <= 100)
{ textBox2.Text = "Excellent"; }
Using if statement for evaluation of Grades
EXAMPLE 2
7. int a, b, c=0;
string op;
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
op = comboBox1.Text;
if(op =="+")
{c = a + b;}
else if(op =="-")
{ c = a - b;}
else if(op ==“/")
{ c = a / b;}
else if(op =="*")
{ c = a * b;}
else {MessageBox.Show("please select an operation");}
textBox3.Text = Convert.ToString(c);
EXAMPLE 3
Using if statement for evaluation of mathematical
expression
Note: Select operation from combobox
8. int a, b, c=0;
string op;
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
op = comboBox1.Text;
switch (op)
{ case "+":
{ c = a + b;
break; }
case "-":
{ c = a - b;
break; }
case "/":
{ c = a / b;
break; }
case "*":
{c = a * b;
break;}
default:
{MessageBox.Show("please select an operation");
break;}
} textBox3.Text = Convert.ToString(c);
EXAMPLE 4
Using switch case statement for evaluation of mathematical expression
Note: Select operation from combobox
9. int day;
day =Convert.ToInt16 (textBox1.Text);
switch (day)
{
case 1: textBox2.Text = "Sunday";
break;
case 2: textBox2.Text = "Monday";
break;
case 3: textBox2.Text = "Tuesday";
break;
case 4: textBox2.Text = "Wednesday";
break;
case 5: textBox2.Text = "Thursday";
break;
case 6: textBox2.Text = "Friday";
break;
case 7: textBox2.Text = "Saturday";
break;
default: textBox2.Text = "Enter valid number";
break;
}
EXAMPLE 5