Java basics 3 flow control (lập TRÌNH NÂNG CAO SLIDE)

38 37 0
Java basics 3   flow control (lập TRÌNH NÂNG CAO SLIDE)

Đ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

ADVANCED PROGRAMMING CONTROL FLOW STATEMENTS Program Control Structures Control flow statement When writing a program, you type statements into a file Without control flow statements, the interpreter executes the statements in the order they appear in the file from left to right, top to bottom You can use control flow statements in your programs to conditionally execute statements; to repeatedly execute a block of statements; and to otherwise change the normal, sequential flow of control Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 2/38 While statement  You use a while statement to continually execute a block of statements while a condition remains true The following is the general syntax of the while statement while (expression) { statement }  First, the while statement evaluates expression, which must return a boolean value If the expression returns true, the while statement executes the statement(s) in the while block The while statement continues testing the expression and executing its block until the expression returns false Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 3/38 Example for while statement class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } } We can implement an infinite loop using the while statement as follows: while (true){ // your code goes here } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 4/38 – while statement { statement(s) } while (expression);  Instead of evaluating the expression at the top of the loop, do-while evaluates the expression at the bottom Thus, the statements within the block associated with a do-while are executed at least once int count = 1; { System.out.println("Count is: " + count); count++; } while (count b  GCD( a, b ) = GCD ( a, b − a ), if a < b a, if a = b  int a = 15; int b = 6; while (a != b){ if (a > b) a = a - b; else if (a < b) b = b - a; } System.out.println("GCD is " + a); // Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 7/38 The for Statement Từ dành riêng Phần khởi đầu forInit Lệnh statement thi thi hành lần hành điều trước bắt đầu vòng lặp kiện condition false for ( forInit ; condition ; forUpdate ) statement; Phần forUpdate thi hành cuối vòng lặp Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 8/38 The for Statement for (initialization; termination; increment) { statement(s) }  The initialization expression initializes the loop; it's executed once, as the loop begins  When the termination expression evaluates to false, the loop terminates  The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value for(int i=1; i

Ngày đăng: 29/03/2021, 10:53

Mục lục

    Example for while statement

    Comparing the while and do – while

    while loop statement example

    For loop statement example

    Iterating with Enhanced for

    The if and else Statements

    Example for if-else statement

    Example for switch statement

    Ex.: Calculte a number of days in a month

    Ex.: Calculte a number of days in a month