5. What we will see…
9/27/2018
Dr.AtifShahzad
Comparison and Logical Operators
The if Statement
The if-else Statement
Nested if Statements
The switch-case Statement
13. Increment (Decrement) Operator
x=6;
x = x + 1;
x = x++;
x += 1; //CompoundAssignment
9/27/2018
Dr.AtifShahzad
13
x /= 4; // equivalent to x=x/4;
x *= 8; // equivalent to x=x*8;
x %= 2; // equivalent to x=x%2;
14. Problem
Write a program that enters the
coefficients a, b and c of a quadratic
equation
𝑎𝑎𝑥𝑥2
+ 𝑏𝑏𝑏𝑏 + 𝑐𝑐 = 0
and calculates and prints its real roots.
Note that quadratic equations may have 0, 1 or
2 real roots.
9/27/2018
Dr.AtifShahzad
14
16. Increment (Decrement) Operator
x=6;
x = x + 1;
x = x++;
x += 1; //CompoundAssignment
9/27/2018
Dr.AtifShahzad
17
x /= 4; // equivalent to x=x/4;
x *= 8; // equivalent to x=x*8;
x %= 2; // equivalent to x=x%2;
17. Prefix & Postfix form
++x; //prefix
x++; //postfix
9/27/2018
Dr.AtifShahzad
18
int x = 3;
int y = ++x;
// x is 4 and y is 4
int x = 3;
int y = x++;
// x is 4 and y is 3
18. Prefix & Postfix form
++x; //prefix
x++; //postfix
9/27/2018
Dr.AtifShahzad
19
int x = 3;
int y = ++x;
// x is 4 and y is 4
int x = 3;
int y = x++;
// x is 4 and y is 3
21. if statement
9/27/2018
Dr.AtifShahzad
22
if (condition)
{
statements;
}
static void Main()
{
Console.WriteLine("Enter two numbers.");
int biggerNumber = int.Parse(Console.ReadLine());
int smallerNumber = int.Parse(Console.ReadLine());
if (smallerNumber > biggerNumber)
{
biggerNumber = smallerNumber;
}
Console.WriteLine("The greater number is: {0}",
biggerNumber);
}
22. if else statement
9/27/2018
Dr.AtifShahzad
23
if (expression)
{
statement1;
}
else
{
statement2;
} string s = Console.ReadLine();
int number = int.Parse(s);
if (number % 2 == 0)
{
Console.WriteLine("This number is even.");
}
else
{
Console.WriteLine("This number is odd.");
}
23. Nested if else statements
9/27/2018
Dr.AtifShahzad
24
if (expression)
{
if (expression)
{
statement;
}
else
{
statement;
}
}
else
statement;
if (first == second)
{
Console.WriteLine(
"These two numbers are equal.");
}
else
{
if (first > second)
{
Console.WriteLine(
"The first number is bigger.");
}
else
{
Console.WriteLine("The second is bigger.");
}
}
24. if else if statements
9/27/2018
Dr.AtifShahzad
25
int ch = 'X';
if (ch == 'A' || ch == 'a')
{
Console.WriteLine("Vowel [ei]");
}
else if (ch == 'E' || ch == 'e')
{
Console.WriteLine("Vowel [i:]");
}
else if …
else …
25. Switch case statement
9/27/2018
Dr.AtifShahzad
26
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
case 4: Console.WriteLine("Thursday"); break;
case 5: Console.WriteLine("Friday"); break;
case 6: Console.WriteLine("Saturday"); break;
case 7: Console.WriteLine("Sunday"); break;
default: Console.WriteLine("Error!"); break;
}
26. Switch case statement
9/27/2018
Dr.AtifShahzad
27
switch (animal)
{
case "dog" :
Console.WriteLine("MAMMAL");
break;
case "crocodile" :
case "tortoise" :
case "snake" :
Console.WriteLine("REPTILE");
break;
default :
Console.WriteLine("There is no such animal!");
break;
}