Вы находитесь на странице: 1из 29

Multiple Condition

(+RadioButton +CheckBox)

IF UBAYA
(Infor+SI+MM+ITDD) 1
MULTIPLE CONDITION
It is very common to have more than 1 condition
before an action can be performed
Remember the previous example:
If (I am hungry and I have money)
I will buy some food
Else
I will not buy any food

Actually there are 2 conditions that must be


fulfilled before an action buy some food can be
performed. What are they? 2
MULTIPLE CONDITION
If you have more than one condition, you have
to combine them with one logical operator.
The logical operator in the previous example is
the word and
There are two logical operators that are
commonly used in C#. They are:
&& : to represent the word and
|| : to represent the word or (the keyboard
button
for character | can be found above the Enter)

3
CHARACTERISTIC OF && AND ||
&&: The combined conditions are said satisfied or true if all
conditions are satisfied (true). [otherwise it is false]
Example:
If (I am hungry and I have money)
I will buy some food
Else
I will not by any food

The combined condition is satisfied/true (so you will buy some


food) if the facts at that time are:
You are hungry [1st condition is satisfied], and
You have money [2nd condition is satisfied]

4
CHARACTERISTIC OF && AND ||
||: The combined conditions are said satisfied or true if
at least one condition is satisfied (true). [otherwise it is false]
Example:
If (You have a driving license or you have an
identity card)
You can open a bank account
Else
You cannot open a bank account
The combined condition is satisfied/true (so you will open a
bank account) if the facts show that at least one of the
conditions is satisfied:
You have a driving license [1st condition is satisfied], or
You have an identity card (KTP) [2nd condition is satisfied].

5
CHARACTERISTIC OF && AND ||
Hierarchy: && has higher operation hierarchy than ||.
This is similar to the hierarchy of multiplication and
addition (i.e. multiplication has higher hierarchy than
addition)
In expression: 1 + 2 * 3, you will do multiplication
first then followed by the addition
So, if you dont give bracket, the combined condition
with && will be processed first than the one with ||.

6
HIERARCHY OF && AND ||: EXAMPLE 1
If (My friend treat me or I am hungry
and I like the food provided)
I will eat the food provided
Else
I will not eat the food provided

Will the combined condition gives value true


(means: you eat the food) if the facts show that:
1. Friend treat: No; Hungry: No; Like the Food: No
2. Friend treat: No; Hungry: No; Like the Food : Yes
3. Friend treat: Yes; Hungry: No; Like the Food : No
4. Friend treat: Yes; Hungry: No; Like the Food : Yes
5. Friend treat: Yes; Hungry: Yes; Like the Food : No
7
CHARACTERISTIC OF && AND ||: EXAMPLE 2
If ( (My friend treat me or I am hungry)
and I like the food provided)
I will eat the food provided
Else
I will not eat the food provided
Will the combined condition gives true value (means: you
eat the food) if the facts show that:
1. Friend treat: No; Hungry: No; Like the Food: No
2. Friend treat: No; Hungry: No; Like the Food : Yes
3. Friend treat: Yes; Hungry: No; Like the Food : No
4. Friend treat: Yes; Hungry: No; Like the Food : Yes
5. Friend treat: Yes; Hungry: Yes; Like the Food : No
Which example represent the condition in your real life
(Example 1 or 2)? 8
COMMON USE OF && IN PROGRAMMING
The operator && is usually used to check if a number is
inside a certain range of number or not.
For example: human will fill comfortable (not too cold, not too
hot), in a range of temperature of 200C 260C. Check if a
certain temperature will make the person comfortable/not.

string result = ;
if (temperature>=20 && temperature<=26)
result = comfortable;
else
result = not comfortable;
9
COMMON USE OF || IN PROGRAMMING
The operator || is usually used to check if a number is
outside a certain range of number or not.
For example: the same example in the previous slide, can be
written as: human will not feel comfortable if the
temperature is below 200C or above 260C.

string result=;
if (temperature<20 || temperature>26)
result = not comfortable;
else
result = comfortable;
10
Example
You were asked to write a program for Starbucks
coffee, to check if a certain customer get a
Rp.20.000 voucher or not.
A customer will get a voucher if the customer
buy coffee and cakes, or the total price of the
order is greater than or equal to 200.000

11
Example User Interface (UI)

12
Example
Input:
Customer name
Customer gender
Information if the customer buy coffee/not
Information if the customer buy cakes/not
Total price of order (note: to simplify the example, in this
case we input the total price of the order. Usually, this
value is calculated by the program)

Output: display the text in the list box such as


shown in the next slide
13
Example 1 of input - output
Input:
Customer name: Budi
Customer gender: Male
Check box order coffee is checked? Yes
Check box order cakes is checked? Yes
Total price of order: 150 000
Display the following text in the List Box:
Mr. Budi
Your total order is: 150000
You order coffee
You order cakes
You get a free Rp.20000 voucher
14
Example 2 of input - output
Input:
Customer name: Abigail
Customer gender: Female
Check box order coffee is checked? Yes
Check box order cakes is checked? No
Total price of order: 250 000
Display the following text in the List Box:
Miss/Mrs/Ms. Abigail
Your total order is: 250000
You order coffee
You get a free Rp.20000 voucher

15
Example 3 of input - output
Input:
Customer name: Jennifer
Customer gender: Female
Check box order coffee is checked? Yes
Check box order cakes is checked? No
Total price of order: 70 000
Display the following text in the List Box:
Miss/Mrs/Ms. Jennifer
Your total order is: 70000
You order coffee
Enjoy your day

16
The Code

See the demonstration


by your teacher in class

17
Radio Button
Place the radio buttons into a container
(GroupBox or Panel control) to make them toggle
automatically when it is selected by the user.
Check the value inside property Checked, to see
if a radio button is selected or not. If it is selected,
property Checked will contain value true,
otherwise it is false.
The default characteristic of radio button: one
radio button must be selected when the program
starts. You can do this in Design View or by the
program.
18
Radio Button
Check the value inside property Checked
// version 1
if (radioButtonMale.Checked == true)
{
:
Can be read as: Is the content of
}
radioButtonMale.Checked is equal to true?

Or Make it short by removing == true (C# will


assume that you want to check with true value)
// version 2
if (radioButtonMale.Checked)
{
:
}
19
Change the check on a radio button
You can Set/Change the value inside
property Checked with true or false.
Example:
radioButtonFemale.Checked = true;

Effect when the code is executed:

20
Check Box
Check box will toggle individually. Therefore you
dont need to place them into a container to make
it works.
Check the value inside property Checked, to see
if a check box is selected or not. If it is selected,
property Checked will contain value true,
otherwise it is false.
There is no default characteristic of check box.
You can set or clear any check box that you like.

21
Check the value inside a check box
Check the value inside property Checked
if (checkBoxCoffee.Checked == true)
{
:
}

Or make it short by removing == true (C# will


assume that you want to check with true value)
if (checkBoxCoffee.Checked)
{
:
}

22
Change the check on a check box

You can Set/Change the value inside


property Checked with true or false.
Example:
checkBoxCoffee.Checked = false;
checkBoxCakes.Checked = false;
Effect after the code is executed:

23
Exercise
project name: yourName-GraduatesChecker
Create an application that can check if an a
certain student can be graduated or not.
A student can be graduated if:
The student already took at least 144 credits
The student already passed the following course:
Algorithm and Programming
Internship
Final Project
Display a certain message in the list box, such as
shown by the examples in the next slides

24
Example of User Interface (UI)

25
Example 1 of Input-Output
Example of inputs:
Name: Budi Hartanto
Study Program: Informatics Engineering
Total Credits Taken: 145
Compulsory courses taken: ALL are checked

Ouput: display the following text in the List Box:


Budi Hartanto is a student in Informatics Engineering
Total credits that have been passed: 145
Compulsory courses taken:
* Algorithm and Programming
* Internship
* Final Project
The student can be graduated.
26
Example 2 of Input-Output
Example of inputs:
Name: Jeremiah
Study Program: Multimedia
Total Credits Taken: 140
Compulsory courses taken: Algorithm and programming
and Internship are checked

Ouput: display the following text in the List Box:


Jeremiah is a student in Multimedia
Total credits that have been passed: 140
Compulsory courses taken:
* Algorithm and Programming
* Internship
The student cannot be graduated yet.
27
Example 3 of Input-Output
Example of inputs:
Name: Florentia
Study Program: Information System
Total Credits Taken: 146
Compulsory courses taken: none is checked

Ouput: display the following text in the List Box:


Florentia is a student in Information System
Total credits that have been passed: 146
Compulsory courses taken:
The student cannot be graduated yet.

28
Upload your works in uls

29

Вам также может понравиться