Presentation is loading. Please wait.

Presentation is loading. Please wait.

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.

Similar presentations


Presentation on theme: "DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures."— Presentation transcript:

1 DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures

2 06/26/10 Control Structures A computer program is a set of statements, which are normally executed sequentially in the order in which they appear. This happens when options or repetitions of certain calculations are not necessary. However, there are situations where it is necessary to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. These conditions are achieved by using the control structures.

3 06/26/10 Control Structures Control statements can be put into the following categories 1.Selection 2.Iteration 3.Jump Selection statements allow the program to choose different paths of execution based upon the outcome of an expression or the state of a variable Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops) Jump statements allow the program to execute in a nonlinear fashion

4 06/26/10 Selection Statements Two selection statements: if statements switch statement These statements allow to control the flow of the program's execution based upon conditions known only during the run time. if Syntax: if(condition) statement1; else statement2; The condition is any expression that returns a boolean value. The else clause is optional.

5 06/26/10 Selection Statements… Nested if A nested if is an if statement that is the target of another if or else. E.g.: if(i = = 10) { if(j < 20) a = b; if(k > 100) c = d; else a = c; } else a=d;

6 06/26/10 Selection Statements… If-Else-If Ladder A common programming construct that is based upon a sequence of nested ifs is the if else-if ladder. Syntax: if(condition) statement; else if(condition) statement; else if(condition) statement; : else statement.;

7 06/26/10 Selection Statements… switch The switch statement is Java's multi way branch statement. often provides a better alternative than a large series of if-else-if statements. Syntax: switch(expression) { case value1: //statement sequence break; case value2: //statement sequence break; :: default: //statement sequence break; }

8 06/26/10 Selection Statements… Nested switch It is also possible to use a switch as a part of the statement sequence of an outer switch statement. This is called a nested switch. Ex: switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero"); break; case 1: // no conflicts with outer switch System.out.println("target is one"); break; } break; case 2:

9 06/26/10 Iteration Statements The iteration statements in OOP are, while do-while for These statements creates what is commonly known as loops. a loop repeatedly executes the same set of instructions until a termination condition is met. While The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Syntax: while(condition) { //body of loop }

10 06/26/10 Iteration Statements… do-while If the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all.But the do- while-loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Syntax: do { //body of the loop } while (condition);

11 06/26/10 Iteration Statements… For Syntax: for(initialization;condition;iteration) { //body } When the loop First starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable..

12 06/26/10 Iteration Statements… Nested Loops In OOP one loop can include inside another loop Ex: for() { for() { statements… }.. Exercise

13 06/26/10 Jump Statements.. Java allows the use following jump statements: The jump statements in OOP are, break continue Break By using break, can force an immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.

14 06/26/10 Jump Statements.. Continue Sometimes it is useful to force an early iteration of a loop. It may necessary to continue running the loop, but stop processing the remainder of the code in its body for the current iteration.


Download ppt "DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures."

Similar presentations


Ads by Google