Đang chuẩn bị liên kết để tải về tài liệu:
Programming in Objective-C 2.0 edition phần 3

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Các ví dụ trước đó sử dụng một năm đó không phải là một năm nhuận bởi vì nó không phải là chia hết cho 4 (1955), một năm đó là năm nhuận bởi vì nó là chia hết cho 400 (2000), và một năm đó không phải là một bước nhảy vọt năm bởi vì nó là chia hết cho 100 nhưng không 400 (1800). | The if Statement 109 Program 6.5 Output Rerun Enter the year to be tested 2000 It s a leap year. Program 6.5 Output Rerun Enter the year to be tested 1800 Nope it s not a leap year. The previous examples use a year that is not a leap year because it isn t evenly divisible by 4 1955 a year that is a leap year because it is evenly divisible by 400 2000 and a year that isn t a leap year because it is evenly divisible by 100 but not by 400 1800 .To complete the run of test cases you should also try a year that is evenly divisible by 4 and not by 100.This is left as an exercise for you. We mentioned that Objective-C gives the programmer a tremendous amount of flexibility in forming expressions. For instance in the previous program you did not have to calculate the intermediate results rem_4 rem_100 and rem_400 you could have performed the calculation directly inside the if statement as follows if year 4 0 year 100 0 year 400 0 Using blank spaces to set off the various operators still makes the previous expression readable. If you decided to ignore this and removed the unnecessary set of parentheses you could end up with an expression that looked like this if year 4 0 year 100 0 year 400 0 This expression is perfectly valid and believe it or not executes identically to the expression shown immediately before it. Obviously those extra blanks go a long way toward aiding our understanding of complex expressions. Nested if Statements In discussions of the general format of the if statement we indicated that if the result of evaluating the expression inside the parentheses is TRUE the statement that immediately follows is executed. It is perfectly valid for this program statement to be another if statement as in the following statement if chessGame isOver NO if chessGame whoseTurn YOU chessGame yourMove If the value returned by sending the isOver message to chessGame is NO the following statement is executed this statement in turn is another if statement.This if statement .