Académique Documents
Professionnel Documents
Culture Documents
IF Ubaya
(Infor+SI+MM+ITDD)
1
Introduction
Sometimes in our life, we want to do some
actions based on one or several conditions.
For example, we want to buy food (this is
action) if we are hungry and we have
money (these are conditions)
In C# like format (not really C#), the above
sentence can be written as:
If (I am hungry and I have money)
I will buy some food
Else
I will not by any food 2
Introduction
Which action that you really do, depend on
the facts that happen at that time.
For example, what action that you will do if
the facts show that:
a. You are not hungry and you dont have money
b. You are not hungry but you have money
c. You are hungry but you dont have money
d. You are hungry and you have money
3
Writing Condition in C#
Because C# dont understand the human
language like hungry, have money, etc.,
then C# will use mathematical comparison
to implement a condition.
For example, to say that you have money
or not, C# will compare the value inside a
certain variable (for example, variable
named money) with a certain value
Example: if you want to say that if your
money is more than 100000, in C# you
4
have to write: if (money > 100000)
More example
Previously, you wrote code to find the
amount of discount.
In real life, it is possible for the user to enter
any number for the discount in percentage
If the user accidentally enter a negative
number, then the amount of discount will be
negative, that is incorrect.
To avoid that problem, you can set the
discount to become 0% if the user
accidentally enter a negative number. 5
More example
Code fragment without condition
:
double disc = double.Parse(textBoxDisc.Text);
double amountOfDisc = price*disc/100.0;
:
8
And for this code?
Version Y
:
double disc = double.Parse(textBoxDisc.Text);
double amountOfDisc = 0;
if (disc > 0)
{
amountOfDisc = 0;
}
else
{
amountOfDisc = price*disc/100.0;
}
9
Another comparison operator
17
Example of User Interface (UI)
19
Exercise 1
project name: your short name findChange
The error
message that is
displayed
21
Example of User Interface (UI)
Application with
an example of
data. Text box
change contains
the change that is
given to the
customer.
22
Exercise 2
project name: your short name Discount
Create an application to show how much discount
can be received by a customer of a shop. The
shop will give a 10% discount if the total
purchasing is greater than 1.000.000, otherwise
the shop will give only 5% discount.
Input:
Total purchasing
Output:
Discount in percentage.
Amount of discount in Rupiah.
Total price after discount 23
Exercise 2
project name: your short name Discount
Formula:
Discount in percentage
(use if to display 10 or 5 as a discount. Dont give percent sign)
Amount of discount in Rupiah
(amountOfDiscount = totalPurchasing * discInPercent / 100.0)
Total price after discount
(totalAfterDisc = totalPurchasing amountOfDiscount)
24
Try to create the User
Interface yourself
If you have no Idea about it, Ask me
25