2. Quiz
• Write a program that takes three
letters as input and display them in
reverse order.
3. Quiz
• Write a program to print the output of
multiplication of three numbers which
will be entered by the user.
• Answer:
Input the first number: 2
Input the second number: 3
Input the third number: 6
Expected Output:
2 x 3 x 6 = 36
4. Test yourself!
• Write a program which will receive
height, width and radios as inputs,
and will find the Blue Area.
• The area of Square:
Area = Height*Width
• The area of Circle:
Area = r2 π
Width
Height
Radios
5. Conditions
• Can you solve this problem?
If X is greater than Y:
then Z = X/Y
Otherwise Z = 1 ?
It is impossible without using
Conditions.
6. Conditions
To create a condition in C#:
if ( condition )
statement;
Or
if ( condition )
{
statement;
statement;
statement;
}
7. Conditions
• For Example
int x = 14;
if (x > 10)
Console.WriteLine(x);
The above line will print 14 because
the condition is true.
8. Conditions
• For Example
int x = 8;
if (x > 10)
{
Console.WriteLine(x);
Console.WriteLine("Hello");
x = 10;
}
The above statements will not run
9. Conditions
• IF is followed by ( ) which has a condition inside it.
• The condition value is either true or false.
• So, the condition expression is Boolean (bool).
• It will run the next statement, if condition is true.
• If the condition is false, it will skip the next
statement.
• The if condition affects only one statement after it.
• If there is need of more than one statement, use { }
11. Conditions
IF Statement - Example
int x;
x = 14 ;
if (x > 10)
x = 18;
Console.Write(x);
int x;
x = 14 ;
if (x < 10)
x = 18;
Console.Write(x);
true false
14
18
12. Conditions
IF Statement – Example using { }
int x;
x = 14 ;
if (x > 10)
{
x = 18;
Console.Write(x);
}
int x;
x = 14 ;
if (x < 10)
{
x = 18;
Console.Write(x);
}
true false
18
13. Conditions
IF Statement – find p and d
int x = -3;
char p = ‘F’, d;
if ( x > -5)
d = p;
x = 18;
if (x < 18)
d = ‘h’;
if (x-10 > -5)
p = ‘k’;
14. Condition Expressions
• In C# we have these comparing expressions:
• X > Y : if X is greater than Y
• X < Y : if X is smaller than Y
• X == Y : if x is equal to Y
• X <= Y : if x is less than and equal to Y
• X >= Y : if x is greater than and equal to y
15. if (1 == 9) Console.WriteLine(“Soran”);
if (18 > 4) Console.WriteLine(“Dohuk”);
if (9 <= -10) Console.WriteLine(“Zakho”);
if (1+6 > 4+2) Console.WriteLine(“Hawler”);
if (1*5 == 10-5) Console.WriteLine(“Ammedi”);
if (3 > 4/2 ) Console.WriteLine(“Akre”);
if (17%2 == 1) Console.WriteLine(“Halabja”);
if (-1 < -3) Console.WriteLine(“Suleimani”);
if (3*6+1 > 1+9*2) Console.WriteLine(“Karkuk”);
if (true) Console.WriteLine(“Rania”);
Condition Expressions
16. • Simple Quick Question
Write a program which:
if X is greater than 10 print X
If not then print “ERROR”
There are two ways to solve this
Conditions
17. Conditions
IF Statement – Way 1
int x;
x = 18 ;
if (x > 10)
Console.Write(x);
If (x <= 10)
Console.Write(“Error”);
18. Conditions
IF Statement – Way 2
int x;
x = 18 ;
if (x > 10)
Console.Write(x);
else
Console.Write(“Error”);
19. • Else grammar
if (conditional expression) // run if true
statement;
else // run if false
statement;
For example
if ( x > 10 )
Console.Write ( “X is greater than 10”);
else
Console.Write ( “X is not greater than 10”);
Conditions
20. Conditions
Else Statement
• else Statement does not need Condition.
• Else will run the next statement if condition is false.
• The else statement does not have ; at the end of it.
• Each if condition can have only one else statement
• The else statement affects only one statement after it.
• If there is need of more than one statement, use { }
21. Conditions
Example – find the output
double g = 14;
double x = 18;
g = g + x;
if (g < x)
Console.Write(“ g < x“);
else
Console.Write(“ g > = x“);
If
(g<x)
If Code
Else
Code
true
false
continue
22. Conditions
Example – find the output
int g= 8;
int x= -3;
g = g + x;
if (g > x)
Console.Write(“ g > x“);
else
Console.Write(“ g <= x“);
If
(g>x)
If Code
Else
Code
true
false
continue
23. Conditions
Statement
static void main()
{
int z;
float b = 28;
double c = b * b;
if (b>c)
z=1;
else
z=2;
}
How many Statements will run from above code?
Statement 1
Statement 2
Statement 3
Statement 4
24. Conditions
Statement – Which one is Correct
static void main()
{
int z,x;
float b = 28;
double c = b * b;
if (b>c)
{
z=1;
}
else
z=2;
}
static void main()
{
int z,x;
float b = 28;
double c = b * b;
if (b>c)
z=1;
else
z=2;
b=0;
}
25. Conditions
Example – find variable values
Double x = 1;
Double y = 4;
if (x * y == y * x)
if ( x> y)
x = 0;
else
y = 0;
else
x = y;
If
(xy ==
yx)
X = y
continue
If X>y
X = 0 y = 0
false
true
true
false
26. Conditions
Boolean Algebra
• Boolean values are either true or false
• They construct base of computing 1 and 0
• Conditions uses Boolean values to run
• For example:
bool a = true;
if (a)
Console.WriteLine(“ This is True”);
else
Console.WriteLine(“This is false”);
27. Conditions
Example 1
Write a program which will show if two
strings are equal
string a = Console.ReadLine();
string b = Console.ReadLine();
if ( a==b)
Console.WriteLine(“strings are equal”);
else
Console.WriteLine(“strings are not equal”);
28. Conditions
Example 2
Write a program which will indicate if
a number is negative or positive.
int number = -5;
if ( number < 0)
Console.WriteLine(number +” is negative”);
else
Console.WriteLine(number +” is positive”);
29. Conditions
Example 3
Write a program which will indicate if
a number is odd or even.
int number = 18;
if ( number %2 == 1)
Console.WriteLine(number +” is odd”);
else
Console.WriteLine(number +” is even”);
30. Lab Plan
• Let’s create a calculator
• Let’s check the IF
• Enjoy