1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng lập trình c 2010 chương 3 đh công nghệ đồng nai

47 262 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 47
Dung lượng 652,38 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 2

Format 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 3

Format 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 5

Logical and Conditional Operators

Trang 6

Logical AND( & )

Conditional AND ( && )

Trang 7

Used to add multiple

conditions to a statement

Trang 8

exp1 exp2 exp1 && exp2

false false false

true false false

(logical AND) operator.

Trang 9

Exp1 exp2 exp1 ||

exp2

(logical OR) operator.

Trang 10

exp1 exp2 exp1 ^ exp2

false false false

Truth table for the logical

Trang 12

Control 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 14

Flowcharting C#’s sequence structure.

Trang 15

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

Trang 17

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

Trang 18

If/else Selection Structure

Conditions

do something

do something else

Trang 19

Conditional 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 21

while Repetition Structure

true

false

do something

conditions

Trang 22

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

Expression1 can only be used in the body of the for loop

• When the loop ends the variable expires

Trang 23

for 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 24

for Repetition Structure

variable has been

reached. counter <= 10 Console.WriteLine

statements)

Increment

the control variable.

Trang 25

switch Multiple-Selection Structure

The switch statement

– Constant expressions

• String

• Integral – Cases

Trang 26

switch Multiple-Selection Structure

break;

case : a true case a action(s)

false

.

Trang 27

do/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 28

do/while Repetition Structure

5

Trang 29

do/while Repetition Structure

true false

action(s)

condition

Trang 30

Statements 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 31

Handle 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 36

The throw statement

if (n < 0)

{

ArithmeticException ex1 =

Trang 37

finally Block

The finally block contains code that always

executes, whether or not any exception occurs

Trang 38

class CMyException : ApplicationException

{

Trang 39

}

Trang 40

Error provider

Trang 43

bool bValid = true ;

Trang 44

bool bValid = true ;

Trang 45

bool bValid = true ;

if

(dateTimePicker1.Value.DayOfWeek

errorProvider1.SetError (dateTimePicker1

}

}

Trang 46

btnDangKy_Click( object sender,

Trang 47

END

Ngày đăng: 03/12/2015, 18:31

TỪ KHÓA LIÊN QUAN