no slide title - sabraz...2017/07/04 · the if / else statement• an if-else statement will...
TRANSCRIPT
![Page 1: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/1.jpg)
Lesson 04Control Structures I :
Decision MakingMIT 31043, VISUAL PROGRAMMING
By: S. Sabraz NawazSenior Lecturer in MIT
Department of MIT
Faculty of Management and Commerce
South Eastern University of Sri Lanka
![Page 2: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/2.jpg)
Decision Structures
• A control structure is a logical design that
controls the order in which statements execute
• A sequence structure is a set of statements that
execute in the order that they appear
• A decision structure execute statements only
under certain circumstances
o A specific action is performed only if a certain
condition exists
o Also known as a selection structure
By: S. Sabraz Nawaz
![Page 3: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/3.jpg)
A Simple Decision Structure
• The flowchart is a single-alternative decision
structure
• It provides only one alternative path of execution
• In C#, you can use the if statement to write such
structures. A generic format is:
if (expression)
{
Statements;
Statements;
etc.;
}
• The expression is a Boolean expression that can
be evaluated as either true or false
Coldoutside
Wear a coat
True
False
By: S. Sabraz Nawaz
![Page 4: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/4.jpg)
Relational Operators
• A relational operator determines whether a specific
relationship exists between two values
Operator Meaning Expression Meaning
> Greater than x > y Is x greater than y?
< Less than x < y Is x less than y?
>= Greater than or equal to x >= y Is x greater than or equal to y?
<= Less than or equal to x <= y Is x less than or equal to you?
== Equal to x == y Is x equal to y?
!= Not equal to x != y Is x not equal to you?
By: S. Sabraz Nawaz
![Page 5: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/5.jpg)
Examples that use relational operators
By: S. Sabraz NawazSlide 5
strFirstName == "Rapid" // equal to a string literal
yearsTextbox.Text == "" // equal to an empty string
strMessage == String.Empty // equal to an empty string
dblDiscountPercent == 2.3 // equal to a numeric literal
blnIsValid == false // equal to the false value
decCode == decProductCode // equal to another variable
strLastName != "Apps" // not equal to a string literal
dblYears > 0 // greater than a numeric literal
intI < intMonths // less than a variable
dblSubtotal >= 500 // greater than or equal to
// a literal value
intQuantity <= intReorderPoint // less than or equal to
// a variable
![Page 6: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/6.jpg)
BRANCHING
By: S. Sabraz NawazSlide 6
![Page 7: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/7.jpg)
BRANCHING
• Branching is the act of controlling which line of code should
be executed next. The line to jump to is controlled by some
kind of conditional statement. This conditional statement is
based on a comparison between a test value and one or more
possible values using Boolean logic.
• This section describes three branching techniques available in
C#:
o The ternary operator
o The if statement
o The switch statement
By: S. Sabraz NawazSlide 7
![Page 8: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/8.jpg)
The Ternary Operator
• The simplest way to perform a comparison is to use the
ternary (or conditional) operator; this operator works on three
operands.
By: S. Sabraz NawazSlide 8
![Page 9: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/9.jpg)
The if Statement
• The if statement is a far more versatile and useful way to make
decisions. Unlike the ternary operator, if statements don’t have
a result (so you can’t use them in assignments); instead, you
use the statement to conditionally execute other statements.
By: S. Sabraz NawazSlide 9
![Page 10: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/10.jpg)
The if Statement• The simplest use of an if statement is as follows, where <test> is evaluated
(it must evaluate to a Boolean value for the code to compile) and the line of
code that follows the statement is executed if <test> evaluates to true:
By: S. Sabraz NawazSlide 10
if (sales > 50000)
bonus = 500;
if (sales > 50000)
{
bonus = 500;
}
sales > 50000
bonus = 500
True
False
![Page 11: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/11.jpg)
The if / else Statement• An if-else statement will execute
one block of statement if its
Boolean expression is true or
another block if its Boolean
expression is false It has two
parts: an if clause and an else
clause
• You can also specify additional
code using the else statement in
combination with an if statement.
This statement is executed if
<test> evaluates to false:
By: S. Sabraz NawazSlide 11
Both sections of code can span multiple
lines using blocks in braces:
![Page 12: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/12.jpg)
Example of if-else Statement
temp >40
display “hot”display “cold”
TrueFalse
if (temp > 40)
{
MessageBox.Show(“hot”);
}
else
{
MessageBox.Show(“cold”);
}
By: S. Sabraz Nawaz
![Page 13: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/13.jpg)
Example
By: S. Sabraz NawazSlide 13
![Page 14: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/14.jpg)
Checking More Conditions Using if Statements
• You can also create a decision structure that evaluates multiple conditions
to make the final decision using the if-else-if statement
• In C#, the generic format is:if (expression)
{
}
else if (expression)
{
}
else if (expression)
{
}
…
else
{
}
By: S. Sabraz NawazSlide 14
![Page 15: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/15.jpg)
Checking More Conditions Using if Statements
By: S. Sabraz NawazSlide 15
![Page 16: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/16.jpg)
Improved Marks Processor
By: S. Sabraz NawazSlide 16
![Page 17: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/17.jpg)
Logical Operators
• The logical AND operator (&&) and the logical OR operator (||) allow you
to connect multiple Boolean expressions to create a compound expression
• The logical NOT operator (!) reverses the truth of a Boolean expression
Operator Meaning Description
&& AND Both subexpression must be true for the compound expression to be true
|| OR One or both subexpression must be true for the compound expression to be true
! NOT It negates (reverses) the value to its opposite one.
Expression Meaning
x >y && a < b Is x greater than y AND is a less than b?
x == y || x == z Is x equal to y OR is x equal to z?
! (x > y) Is the expression x > y NOT true?
By: S. Sabraz Nawaz
![Page 18: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/18.jpg)
Sample Decision Structures with Logical Operators
• The && operatorif (temperature < 20 && minutes > 12)
{
MessageBox.Show(“The temperature is in the danger zone.”);
}
• The || operatorif (temperature < 20 || temperature > 100)
{
MessageBox.Show(“The temperature is in the danger zone.”);
}
• The ! Operatorif (!(temperature > 100))
{
MessageBox.Show(“The is below the maximum temperature.”);
}
By: S. Sabraz Nawaz
![Page 19: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/19.jpg)
Nested Decision Structures
• You can create nested decision structures to test more than one condition.
• Nested means “one inside another”
• In C#, a generic format is:
if (expression)
{
if (expression)
{
statements;
}
else
{
statements;
}
}
else
{
statements
}
By: S. Sabraz Nawaz
![Page 20: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/20.jpg)
A Sample Nested Decision Structure
By: S. Sabraz NawazSlide 20
Salary >= 40000
yearsOnJob>= 2
Display “Minimum salary requirement
not met.”
Display “You qualify for the load.”
Display “Minimum years at current job
not met.”
End
![Page 21: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/21.jpg)
A Sample Nested Decision Structure
if (salary >= 40000)
{
if (yearOnJob >= 2)
{
decisionLabel.Text = "You qualify for the loan."
}
else
{
decisionLabel.Text = "Minimum years at current " + "job not met."
}
}
else
{
decisionLabel.Text = "Minimum salary requirement " + "not met."
}
By: S. Sabraz Nawaz
![Page 22: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/22.jpg)
By: S. Sabraz NawazSlide 22
if (strCustomerType == "R")
{
if (dblSubtotal >= 100)
decDiscountPercent = .2m;
else
decDiscountPercent = .1m;
}
else // customerType isn't "R"
decDiscountPercent = .4m;
A Sample Nested Decision Structure
![Page 23: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/23.jpg)
Example
• This program checks if
the mark’s range is
above 100 or below 0
(zero).
• If the mark is out of
range (0-100), an error
message is show using
the MessageBox.
• If it is within the range,
calculation is executed.
By: S. Sabraz NawazSlide 23
![Page 24: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/24.jpg)
Example
By: S. Sabraz Nawaz Slide
24
![Page 25: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/25.jpg)
switch
![Page 26: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/26.jpg)
The switch Multiple-Selection Statement
• C# provides the switch multiple-selection statement to perform
different actions based on the possible values of an expression.
• Each action is associated with the value of a constant integral
expression or a constant string expression that the variable or
expression on which the switch is based may assume.
• A constant integral expression is any expression involving
character and integer constants that evaluates to an integer
value or a constant.
• A constant string expression is any expression composed of
string literals that always results in the same string.
• A switch statement compares ONE variable against
MULTIPLE possible values
By: S. Sabraz Nawaz Slide
26
![Page 27: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/27.jpg)
By: S. Sabraz Nawaz Slide
27
The syntax of the switch statement
Supported data types: bool,
char, String, integral, or enum
![Page 28: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/28.jpg)
Sample switch Statement
switch (month)
{
case 1:
MessageBox.Show(“January”);
break;
case 2:
MessageBox.Show(“February”);
break;
case 3:
MessageBox.Show(“March”);
break;
default:
MessageBox.Show(“Error: Invalid month”);
break;
}
month
Display “January”
Display “February”
Display “March”
Display “Error: Invalid month”
![Page 29: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/29.jpg)
By: S. Sabraz NawazSlide 29
switch Example I: Lottory
![Page 30: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/30.jpg)
By: S. Sabraz Nawaz Slide
30
switch Example II: Month Finder
![Page 31: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/31.jpg)
switch (strCustomerType)
{
case "R":
case "C":
decDiscountPercent = .2m;
break;
case "T":
decDiscountPercent = .4m;
break;
}
By: S. Sabraz Nawaz Slide
31
A switch statement that falls through the first case label
![Page 32: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/32.jpg)
GUI Design
By: S. Sabraz Nawaz Slide
32
![Page 33: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/33.jpg)
GUI – CheckBox• A small area on a computer screen which, when selected by the user,
shows that a particular feature has been enabled. A CheckBox control
allows users to select a single or multiple options from a list of options.
• A typical CheckBox control has two possible states:
o Checked state is when the CheckBox has check mark on
o Unchecked is when the CheckBox is not checked
• Typically, we use a mouse to check or uncheck a CheckBox
• Checked property is true when a CheckBox is in checked state
By: S. Sabraz NawazSlide 33
![Page 34: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/34.jpg)
GUI – CheckBox…
By: S. Sabraz NawazSlide 34
![Page 35: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/35.jpg)
CheckedChanged Event
• Like the Click event for a button, the checkbox has
CheckedChanged as its default event.
• The CheckedChanged event occurs when the value of the
Checked property changes
By: S. Sabraz NawazSlide 35
![Page 36: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/36.jpg)
GUI – RadioButton• A RadioButton control provides a round
interface to select one option from a number of options.
• Radio buttons are usually placed in a group on a container control such as a Panel or a GroupBox and one of them is selected.
• A typical RadioButton control has two possible states:
o Checked state is when the button has check mark on
o Unchecked is when the button is not checked
• Typically, we use a mouse to check or uncheck
• Checked property is true when a radiobutton is in checked state
• At a time only one radiobutton can be selected within a container
By: S. Sabraz NawazSlide 36
![Page 37: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/37.jpg)
GUI – RadioButton…
By: S. Sabraz NawazSlide 37
![Page 38: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/38.jpg)
GUI - PictureBox
• You can display images on your form by using the PictureBox
control.
• It is a simple control which has a main purpose of displaying
images.
• All you have to do is browse for the desired image and Visual
Studio will import it to your project. You can use several
image formats such as JPEG, GIF, PNG, and BMP.
By: S. Sabraz NawazSlide 38
Properties Description
Image The image that will be displayed by the control.
ImageLocation The path of the image to be displayed by the PictureBox.
SizeMode Tells how the image will be displayed.
![Page 39: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/39.jpg)
GUI – PictureBox…• To display an image using the PictureBox control, there are multiple ways
you can use. You can go to the Properties Window and find the Image
property. Click the button to the right of it to bring out the Select Resource
Dialog.
By: S. Sabraz NawazSlide 39
![Page 40: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/40.jpg)
GUI – PictureBox…
• Once the image is displayed, it may not look like you want it
to. If the loaded image is larger the size of the PictureBox,
then the image will be clipped.
• You can use the SizeMode property to change the way the
image is positioned or resized inside the control.
By: S. Sabraz NawazSlide 40
PictureBoxSizeMode Description
NormalThe image will be positioned in the upper-left corner of the PictureBox and if the image
is larger than the PictureBox, the image will be clipped.
StretchImage Resizes the image to match the size of the PictureBox.
AutoSize Resizes the PictureBox to match the size of the image.
CenterImageThe image is centered inside the PictureBox. If the image is larger than the PictureBox,
the image will be clipped.
Zoom Fits the whole image inside the PictureBox while maintaining the image’s size ratio.
![Page 41: No Slide Title - sabraz...2017/07/04 · The if / else Statement• An if-else statement will execute one block of statement if its Boolean expression is true or another block if](https://reader031.vdocuments.us/reader031/viewer/2022041923/5e6cf79a7aa87d6bf539ea7a/html5/thumbnails/41.jpg)
THANK YOU
By: S. Sabraz NawazSlide 41