DONG NAI UNIVERSITY OF TECHNOLOGY
Format Code
Format
2
DONG NAI UNIVERSITY OF TECHNOLOGY
Description
Code
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.
Format Code
Format
3
DONG NAI UNIVERSITY OF TECHNOLOGY
Description
Code
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.
DONG NAI UNIVERSITY OF TECHNOLOGY
DONG NAI UNIVERSITY OF TECHNOLOGY
Logical and
Conditional Operators
DONG NAI UNIVERSITY OF TECHNOLOGY
1
2
Logical
Conditional AND (&&)
AND(&)
3
4
Logical OR (|)
5
Logical exclusive OR
or XOR (^)
Conditional OR (||)
6
Logical NOT (!)
DONG NAI UNIVERSITY OF TECHNOLOGY
Used to add multiple
conditions to a statement
DONG NAI UNIVERSITY OF TECHNOLOGY
exp1
exp2
exp1 && exp2
false
false
false
false
true
false
true
false
false
true
true
true
Truth table for the && (logical AND)
operator.
DONG NAI UNIVERSITY OF TECHNOLOGY
Exp1
exp2
exp1 || exp2
false
false
false
false
true
true
true
false
true
true
true
True
Truth table for the || (logical OR) operator.
DONG NAI UNIVERSITY OF TECHNOLOGY
exp1
exp2
exp1 ^ exp2
false
false
false
false
true
true
true
false
true
true
true
false
Truth table for the logical exclusive OR (^)
operator.
DONG NAI UNIVERSITY OF TECHNOLOGY
expression
!expression
false
true
True
false
Truth table for operator! (logical NOT).
DONG NAI UNIVERSITY OF TECHNOLOGY
Control Structures
• Program of control
–Program performs one statement then goes to next
line
•Sequential execution
DONG NAI UNIVERSITY OF TECHNOLOGY
– Different statement other than the next one executes
• Selection structure
–The if and if/else statements
• Repetition structure
–The while and do/while loops
–The for and foreach loops
DONG NAI UNIVERSITY OF TECHNOLOGY
total = total + grade;
add grade to
total
counter = counter + 1;
add 1 to counter
Flowcharting C#’s sequence structure.
DONG NAI UNIVERSITY OF TECHNOLOGY
if 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
DONG NAI UNIVERSITY OF TECHNOLOGY
if Selection Structure
true
conditions
false
do something
DONG NAI UNIVERSITY OF TECHNOLOGY
If/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
DONG NAI UNIVERSITY OF TECHNOLOGY
If/else Selection Structure
false
true
Conditions
do something else
do something
DONG NAI UNIVERSITY OF TECHNOLOGY
Conditional Operator (?:)
C#’s only ternary operator
Similar to an if/else structure
The syntax is:
booleanvalue
value??ififtrue
true::ififfalse
false
boolean
MessageBox.Show(dtb>=5?“Dau”:
“Dau”:“Rot”);
“Rot”);
MessageBox.Show(dtb>=5?
DONG NAI UNIVERSITY OF TECHNOLOGY
Loops
Repeating a series of instructions
Each repetition is called an iteration
Types of Loops
o
Do
o
Use when the number of iterations is unknown
For
Use when the number of iterations known
DONG NAI UNIVERSITY OF TECHNOLOGY
while Repetition Structure
true
conditions
false
do something
DONG NAI UNIVERSITY OF TECHNOLOGY
for Repetition Structure
• The for repetition structure
– Syntax: for (Expression1, Expression2, Expression3)
• Expression1 = names the control variable
– Can contain several variables
• Expression2 = loop-continuation condition
• Expression3 = incrementing/decrementing
– If Expression1 has several variables, Expression3
must have several variables accordingly
– ++counter and counter++ are equivalent
– Variable scope
• Expression1 can only be used in the body of the for loop
• When the loop ends the variable expires
DONG NAI UNIVERSITY OF TECHNOLOGY
for Repetition Structure
Final value
for
of control variable
keyword
Control variable
for ( int
Initial value
name
i = 1; i <= 5; i++ )
of control variable
Loop-continuation condition
Increment
of control variable
DONG NAI UNIVERSITY OF TECHNOLOGY
for Repetition Structure
Establish initial value of control
variable.
int counter = 1
Determine if final value of
control variable has been
reached.
true
Console.WriteLine
counter <= 10
counter++
( counter * 10 );
Increment the
false
Body of loop (this may be
multiple statements)
control variable.
DONG NAI UNIVERSITY OF TECHNOLOGY
switch Multiple-Selection Structure
•
The switch statement
– Constant expressions
• String
• Integral
– Cases
• Case ‘x’ :
– Use of constant variable cases
• Empty cases
• The default case
– The break statement
• Exit the switch statement