0% found this document useful (0 votes)
6 views

Lecture-05 Decision Making With Control Statement

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture-05 Decision Making With Control Statement

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Ghalib Private University

Visual Programming Language


BY MR.SADDIQI
Decision Structures(Control Statements)
• Due to These statements you’ll increase your programming
vocabulary by creating code blocks called decision structures
that control how your program executes, or flows, internally .
• The statement which is used to set or ignore the execution of
some other statement is called conditional statements.
• One of the most useful tools for processing information in an
event procedure is a conditional expression. A conditional
expression is a part of a complete program statement that asks
a True-or-False question about a property, a variable, or
another piece of data in the program code.
C# Comparison Operators
You can use the
following comparison
operators within a
conditional expression.
Using if Statements to Make Decisions
•When a conditional expression is used in a special
block of statements called a decision structure, it
controls whether other statements in your program
are executed and in what order they’re executed; You
can use an (If ) decision structure to evaluate a
condition in the program and take a course of action
based on the result.
• simplest form, an If . . decision structure is written
on a single line:
• If (condition) { statement;}
Testing Several Conditions in an If …Then
Decision Structure (Nested if else structure)
Code Example
private int dateCompare(DateTime leftHandSide, DateTime
rightHandSide)
private void button1_Click(object sender, EventArgs e) {
int result;
{
if (leftHandSide.Year < rightHandSide.Year)
int diff= dateCompare(dateTimePicker1.Value, result = -1;
dateTimePicker2.Value); else if (leftHandSide.Year > rightHandSide.Year)
result = 1;
textBox1.Text = ""; else if (leftHandSide.Month < rightHandSide.Month)
textBox1.Text = “first == second" + (diff==0); result = -1;
else if (leftHandSide.Month > rightHandSide.Month)
textBox1.Text +=“\r\nfirst != second"+ (diff != 0); result = 1;
else if (leftHandSide.Day < rightHandSide.Day)
textBox1.Text += "\r\nfirst < second" + (diff < 0); result = -1;
textBox1.Text += "\r\nfirst <= second" + (diff <= 0); else if (leftHandSide.Day > rightHandSide.Day)
result = 1;
textBox1.Text +="\r\nfirst > second" + (diff > 0); else
result = 0;
textBox1.Text +="\r\nfirst >= second"+ (diff >= 0);
return result;
} }
Example: Validate users by using If…
Then1. Start Visual Studio, and create a new Windows Forms
Application project named My User Validation
2. Click the form, and then set the form’s Text property to
“User Validation ”
3. Use the Label control to create a label on your form, and
use the Properties window to set the Text property to
“Enter Your Social Security Number ”
4. Use the Button control to create a button on your
form, and set the button’s Text property to “Sign In ”
5. Click the MaskedTextBox control on the Common Controls tab
in the Toolbox, and then create a masked text box object on
your form below the label
oThe MaskedTextBox control is similar to the TextBox control that
you have been using, but by using MaskedTextBox, you can control
the format of the information entered by the user into your
program
oYou control the format by setting the Mask property; you can use
a predefined format supplied by the control or choose your own
format
6. With the MaskedTextBox1 object selected, click the Mask property in
the Properties window, and then click the ellipses button in the
second column.
The Input Mask dialog box opens, showing a list of your predefined
formatting patterns, or masks
7. Click Social Security Number in the list
◦ The Input Mask dialog box looks like this:
8. Click OK to accept Social Security Number as your input mask

9. Double-click the Sign In button The Button1_Click event procedure appears in the Code Editor
10. Type the following program statements in the event procedure:
If (MaskedTextBox1.Text = "555-55-1212" )
MessageBox.Show("Welcome to the system!") ;
Else
MessageBox.Show("I don't recognize this number") ;
The Switch Decision
Structures
A Switch Statement is similar to an If . . .. . ElseIf structure,
but it’s more efficient when the branching depends on one key
variable, or test case You can also use Select Switch structures
to make your program code more readable.
Syntax of select case statements
Questions?

You might also like