Oracle DBA Forum  

Вернуться   Oracle DBA Forum > Oracle University Official Study Notes (ENG) > Oracle 10g Java Programming > Oracle 10g Java Programming

Ответ
 
Опции темы Опции просмотра
  #1  
Старый 14.01.2010, 01:25
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию 05 Controlling Program Flow

__________________
Чат форума (требуется аккаунт на github или twitter)
Ответить с цитированием
  #2  
Старый 14.01.2010, 01:25
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Objectives



Lesson Aim


This lesson introduces Javas decision-making and repetition constructs. You learn how to use those constructs to build nonlinear applications. Like other block-structured languages, Java supports statements to control the flow of execution in a program. This lesson explains how to use decision-making constructs and how to use the loop statements that are provided in Java.


What Is Program Flow?

By default, computer programs start execution at, or near, the beginning of the code and move down the code until the end. This is fine if your program does one thing, always in the same order, and never needs to deviate from this path. Unfortunately, this does not happen very often in most businesses.

Program control constructs are designed so that the programmer can design and build programs that perform certain parts of code conditionally. There are also constructs so that code can be executed repetitively. These coding structures give you infinite control of what, when, and how many times your program performs a particular task.

By providing standard control mechanisms, Java gives programmers control over the exact execution order of their program code.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 10:45.
Ответить с цитированием
  #3  
Старый 14.01.2010, 01:25
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Categorizing Basic Flow Control Types



Categorizing Basic Flow Control Types

Sequential

Sequential is the flow control type where the program flow follows a simple sequential path, executing one statement after another. The primary sequential structure is a compound block statement, which is a series of statements inside braces.

Selection

Selection is the flow control type where only one path out of a number of possibilities is taken. Simple selection involves the conditional execution of a statement or block of code, which is guarded by an expression that will have the value true if the guarded code is to be executed. This is the if statement. An ifelse statement provides an alternate path of execution: The true or false evaluation of a control expression determines which branch is taken. A switch statement supports a multiway branch based on the value of a control expression.

Iteration

Iteration is the type where one statement or block of code is executed repeatedly. A simple loop executes the same code whereas a control expression has a true value, terminating execution when the expression evaluates to false. Java provides three loop forms: while, dowhile, and for loops.

Transfer

Transfer is the type where the point of execution jumps to a different point in the program. Transfer is generally considered poor programming style, because it is hard to follow the logic and therefore difficult to maintain the code.

Goto

The Java language does not have a goto statement. This allows the language to be less complex and eliminates rules about how the goto must operate in for statements. The break and continue statements work in the majority of occasions where a goto would normally be used.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 10:47.
Ответить с цитированием
  #4  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Using Flow Control in Java



Using Flow Control in Java

Simple Statements

A simple statement is any expression that terminates with a semicolon. For example:

var1 = var2 + var3;
var3 = var1++;
var3++;


Compound Statements (Blocks)

Related statements can be grouped in braces to form a compound statement or block:

Код:
	  {
	    int i;
	    boolean finished = true;
            System.out.println("i = " + i);
            i++;
	  }
Semantically, a block behaves like a single statement and can be used anywhere a single statement is allowed. There is no semicolon after the closing brace. Java does not use matched block delimiters, such as if and end if, like PL/SQL. Any variables that are declared in a block remain in scope up to the closing brace. After the block is exited, the block variables cease to exist.

Blocking improves readability of program code and can help to make your program easier to control and debug.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 10:51.
Ответить с цитированием
  #5  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Using the if Statement



The if Statement

The if statement provides basic selection processing. A Boolean control expression determines which branch is taken, as follows:

If the expression evaluates to true, the first branch is taken; that is, the if body is executed.
If the expression evaluates to false, the second branch is taken; that is, the else body is executed. The else clause is optional; if it is omitted, nothing is executed if the control expression evaluates to false.

Example

Код:
	if (orderIsPaid) {
		System.out.println("send with receipt");
	}
	else {
			System.out.println("collect funds");
	}

Common Mistakes When Using if Statements

Use the equality operator (==) rather than the assignment operator (=) in the control expression.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:17.
Ответить с цитированием
  #6  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Nesting if Statements




Nested if Statements

Where multiple tests are necessary, if statements can be nested. However, this approach is generally not recommended because you have to maintain a mental stack of the decisions that are being made; this becomes difficult if you have more than three levels of nested if statements.

Also, it is very easy to forget that an else clause always binds to the nearest if statement above it that is not already matched with an else, even if the indentation suggests otherwise. This is sometimes referred to as the dangling else problem.


The ifelseif Construct


The dangling else problem can be solved with a prudent use of braces, but a cleaner approach is to use the ifelse if construct, as shown in the second example in the slide. Note that these are two separate keywords; unlike some languages, Java does not have an elseif keyword.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:20.
Ответить с цитированием
  #7  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Guided Practice: Spot the Mistakes



Guided Practice: Spot the Mistakes

Example 1: Use braces to associate or bind statements and make the code easier to follow.

if (x >= 0) {
if (y < x)
System.out.println("y is less than x");}
else
System.out.println("x is negative");


It is the last if that pairs with an else. Although it does not become a compiler error, it becomes a logic error.


Example 2: The second example uses an assignment operator (=) rather than an equality operator (==) in the if test. Fortunately, Java compilers detect this mistake and indicate a compiler error because the expression in the if test must evaluate to a Boolean.

Example 3

The third example has an extra semicolon at the end of the if test. This is not a compiler error; the compiler treats the semicolon as an empty if body, as follows:

if ( x % 2 == 0 && y % 2 == 0 )
; // Null "if" body
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:23.
Ответить с цитированием
  #8  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию The switch Statement




The switch Statement

The switch statement provides a clean way to dispatch to different sections of your code, depending on predefined values. It can be used to choose among many alternative actions, based on the value.

Anatomy of the switch Statement

The switch statement is useful when selecting some action from several alternatives. The value inside the test expression must be a byte, char, short, or int. It cannot be a boolean, long, double, float, String, or any other kind of object.

The value inside the test expression is compared against the case labels, which are constant expressions.

If a match is found, then the statements following the label are executed. Execution continues until a break is encountered, which transfers control to the statement following the switch statement.
If no match is found, then control passes to the statements following the default label. The default label is optional; if no default label is provided, then the switch statement does nothing when no match is found. It is a good practice to always provide a default label, even if no action is required.


The case Labels

The case labels must be constant expressions and known at compile time. You can use either literal numbers or final variables (constants).
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:25.
Ответить с цитированием
  #9  
Старый 14.01.2010, 01:26
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию More About switch



More About switch

The slide shows a simple example of a switch statement. There are situations where falling through can be useful. To fall through, simply do not include a break as in the following example:

Код:
char c = 'b';
switch (c) {
  case 'a': System.out.println("First letter in alphabet"); 
		 break;
  case 'b': System.out.println("Second letter in alphabet");
  case 'c': System.out.println("Third letter in alphabet");
  case 'd': System.out.println("Fourth letter in alphabet");
}
In the above example, the results that are printed displays the following lines because of the absence of a break statement between each case test for b, c, and d.

Second letter in alphabet
Third letter in alphabet
Fourth letter in alphabet
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:36.
Ответить с цитированием
  #10  
Старый 14.01.2010, 01:27
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Looping in Java



Looping in Java


What Is Looping?

Looping in any programming language refers to repeatedly executing a block of code until a specified condition is met. Java provides three standard loop constructs: while, dowhile, and for.


All Loops Have Four Parts

All loops contain four parts: initialization, iteration, statement body, and termination.
Initialization: Initialization sets the initial conditions of the loop. This includes any variable that may be incremented to control execution of the loop.
Iteration: The iteration is the code that you want to execute after the body but before you go through the loop again. It is used for control of the loop execution.
Body: The body is executed if the termination condition is true.
Termination: Termination is the expression that is evaluated to determine if the body must be executed. The expression must be a Boolean expression. If the expression evaluates to true, then the body is executed; if it is false, then the body is not executed.

Choosing the appropriate loop

Use the while loop to ensure that the termination condition is tested before executing the body of the loop. Use the dowhile to ensure that the body executes once before the termination condition is made. The for loop is similar to the while loop.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 06.04.2010 в 11:38.
Ответить с цитированием
Ответ
Опции темы
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.

Быстрый переход


Текущее время: 19:18. Часовой пояс GMT +3.


Powered by vBulletin®