Vous êtes sur la page 1sur 25

CONDITION

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;
:

Code fragment with condition


:
double disc = double.Parse(textBoxDisc.Text);
if (disc < 0)
{
disc = 0;
}
double amountOfDisc = price*disc/100.0;
6
:
Other version
You can also add the keyword: else
The same purpose code from the previous one,
but with different approach
:
double disc = double.Parse(textBoxDisc.Text);
double amountOfDisc = 0; //Give initial value 0
if (disc < 0)
{
amountOfDisc = 0;
}
else
{
amountOfDisc = price*disc/100.0;
}
7
:
Any comment for this code?
Version X
:
double disc = double.Parse(textBoxDisc.Text);
double amountOfDisc = price*disc/100.0;
if (disc < 0)
{
disc = 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

You can use the following comparison operators


in C#:
< (less than)
<= (less than or equal to)
> (greater than)
>= (greater than or equal to)
== (equal)
!= (not equal)
The operator must be written without space and
cannot be reversed (i.e. < = is incorrect, as well as
=<) 10
Writing the condition clearly
1. Start the comparison with the variable that
you want to inspect (check) its value
2. Use the same comparison operator as the one
stated in the description
Example: if the payment from the user is
enough (greater or equal to the purchasing)
then find the change for the payment. A good
condition comparison can written as follow:
payment var. is written first
:
and comparison operator
if (payment >= purchasing)
used is >= (greater or equal)
{
change = payment purchasing;
11
}
Compare it with the following
Payment is written in the end of the condition and
the comparison operator is not the one as stated
in the description:
:
if (purchasing <= payment)
{
change = payment purchasing;
}

Although both of them are correct, the first


version can be understood better and easily
than the second version because it is similar to
the problem description. 12
Compare Again the following
conditions. Which one is better?
Version A.
if the distance is greater than 2 km then go by
a taxi, otherwise just walk :
:
if (2 < distanceInKM)
{
transportation = Taxi;
}
else
{
transportation = Walk;
}
13
Compare Again the following
conditions. Which one is better?
Version B.
if the distance is greater than 2 km then go by
a taxi, otherwise just walk :
:
if (distanceInKM <= 2)
{
transportation = Walk;
}
else
{
transportation = Taxi;
}
14
Compare Again the following
conditions. Which one is better?
Version C.
if the distance is greater than 2 km then go by
a taxi, otherwise just walk :
:
if (distanceInKM > 2)
{
transportation = Taxi;
}
else
{
transportation = Walk;
}
15
Example
Create an application to check if a certain
applicant is eligible to get a driver license or not.
An applicant is eligible if he/she is at least 17
years old.
Display a message on the screen that shows the
result of the checking (whether the applicant is
eligible or not)
Use MessageBox.Show(string) to display the
message (the string).
16
Example of User Interface (UI)

The contents inside the text boxes are just examples

17
Example of User Interface (UI)

The message when the


applicant (in this case:
Alice) is not eligible

The message when the


applicant is eligible (for
another case, where
the applicant age is 17
or more)
18
The Code inside button Check

19
Exercise 1
project name: your short name findChange

Create an application to find the change (uang


kembalian) that must be given to the customer
Input:
Total purchasing
Payment
Output:
If payment >= total purchasing display the change
Otherwise clear the textbox change and display the
message Minimum payment must be: followed by
the value of the total purchasing (see the next slide)
20
Example of User Interface (UI)
Application with
an example of
data. Text box
Change is
cleared because
payment is less
than total
purchasing.

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.

No error message is displayed

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

Vous aimerez peut-être aussi