Format CodeFormat Code Description C or c Formats the string as currency.. Format CodeFormat Code Description E or e Formats the number using scientific notation with a default of six
Trang 2Format Code
Format
Code
Description
C or c Formats the string as currency.
Precedes the number with an appropriate currency symbol ($
in the US)
D or d Formats the string as a decimal
Displays number as an integer
N or n Formats the string with commas
and two decimal places
Trang 3Format Code
Format
Code
Description
E or e Formats the number using scientific
notation with a default of six decimal places
F or f Formats the string with a fixed
number of decimal places (two by default)
G or g General Either E or F.
X or x Formats the string as hexadecimal.
Trang 5Logical and Conditional Operators
Trang 6Logical AND( & )
Conditional AND ( && )
Trang 7Used to add multiple
conditions to a statement
Trang 8exp1 exp2 exp1 && exp2
false false false
true false false
(logical AND) operator.
Trang 9Exp1 exp2 exp1 ||
exp2
(logical OR) operator.
Trang 10exp1 exp2 exp1 ^ exp2
false false false
Truth table for the logical
Trang 12Control Structures
• Program of control
–Program performs one
statement then goes to next
line
•Sequential execution
Trang 13–Different statement other than the
next one executes
Trang 14Flowcharting C#’s sequence structure.
Trang 15if Selection Structure
Causes the program to make a
selection
Chooses based on conditional
• Any expression that evaluates to a
bool type
• True: perform an action
• False: skip the action
Single entry/exit point
Require no semicolon in syntax
Trang 17If/else Selection Structure
Alternate courses can be taken when the
statement is false
Rather than one action there are two choices
Nested structures can test many cases
Structures with many lines of code need
braces ({)
• Can cause errors
Fatal logic error
Nonfatal logic error
Trang 18If/else Selection Structure
Conditions
do something
do something else
Trang 19Conditional Operator ( ? :)
C#’s only ternary operator
Similar to an if/else structure
The syntax is:
boolean value ? if true : if false
boolean value ? if true : if false
MessageBox.Show(dtb>=5?
“Dau”: “Rot”);
MessageBox.Show(dtb>=5?
“Dau”: “Rot”);
Trang 20 Repeating a series of instructions
Each repetition is called an iteration
Trang 21while Repetition Structure
true
false
do something
conditions
Trang 22for Repetition Structure
• The for repetition structure
– Syntax: for (Expression1, Expression2, Expression3)
• Expression1 = names the control variable
– Can contain several variables
• Expression2 = loop-continuation condition
• Expression1 can only be used in the body of the for loop
• When the loop ends the variable expires
Trang 23for Repetition Structure
for ( int i = 1; i <= 5; i++ )
Initial value of control variable
Increment of control variable
Control variable name
Final value of control variable
for keyword
Loop-continuation condition
Trang 24for Repetition Structure
variable has been
reached. counter <= 10 Console.WriteLine
statements)
Increment
the control variable.
Trang 25switch Multiple-Selection Structure
• The switch statement
– Constant expressions
• String
• Integral – Cases
Trang 26switch Multiple-Selection Structure
break;
case : a true case a action(s)
false
.
Trang 27do/while Repetition Structure
• The while loops vs the do/while loops
– Using a while loop
• Condition is tested
• The the action is performed
• Loop could be skipped altogether – Using a do/while loop
• Action is performed
• Then the loop condition is tested
• Loop must be run though once
• Always uses brackets ({) to prevent confusion
Trang 28do/while Repetition Structure
5
Trang 29do/while Repetition Structure
true false
action(s)
condition
Trang 30Statements break and continue
• Use
– Used to alter the flow of control
– The break statement
• Used to exit a loop early – The continue statement
• Used to skip the rest of the statements and begin the loop at the first statement in the loop
– Programs can be completed without their usage
Trang 31Handle Exception
Trang 32• An exception occurs when a program encounters any unexpected problems.
• Your program should be able to
handle these exceptional situations
and, if possible, gracefully recover
from them This is called exception
handling
Trang 36The throw statement
if (n < 0)
{
ArithmeticException ex1 =
Trang 37finally Block
The finally block contains code that always
executes, whether or not any exception occurs
Trang 38class CMyException : ApplicationException
{
Trang 39}
Trang 40Error provider
Trang 43bool bValid = true ;
Trang 44bool bValid = true ;
Trang 45bool bValid = true ;
if
(dateTimePicker1.Value.DayOfWeek
errorProvider1.SetError (dateTimePicker1
}
}
Trang 46btnDangKy_Click( object sender,
Trang 47END