Professional Documents
Culture Documents
3.1. Introduction
3.2. Basic control structures
3.2.1. Sequence
3.2.2. Selection
3.2.3. Iteration
3.3. Representation techniques
3.3.1. Control flow diagrams
3.3.2. Pseudocode
3.4 Data structures (Arrays)
Programming Fundamentals I 1
Tema 3. Basic control and data structures
• Recommended bibliography
• Structured programming
- Muñoz Caro C., Niño A. y Vizcaíno Barceló A. Introducción a la
programación con orientación a objetos. Chapter 4. Prentice-
Hall, 2002. Reimpresión 2007
Programming Fundamentals I 2
3.1. Introduction (I)
• Structured program theorem
– Bohm, C. and Jacopini, G, "Flow Diagrams, Turing Machines
and Languages with Only Two Formation Rules".
Communications of the ACM, 9 (5),366–371 (1966)
Programming Fundamentals I 3
3.2. Basic control structures
• Next, each control structure shall be studied
– Sequence
– Selection
– Loop
Programming Fundamentals I 4
3.2.1. Sequence
• First basic control structure
int value=0;
value=value+1;
System.out.println(value);
Programming Fundamentals I 5
3.2.2 Selection (I)
• Second basic control structure
– Complete structure:
if (condition) {
---- block of instructions ----
}
else {
---- block of instructions ----
}
if (condition) {
---- block of instructions ----
}
• Next an example
Programming Fundamentals I 6
3.2.2 Selection (I)
• Graphic representation of “IF” (flow diagram)
Programming Fundamentals I 7
import java.util.*;
class Condition {
// Example of conditional instruction if-else
public static void main(String[] args) {
int x;
Scanner leer =new Scanner(System.in);
// data reading
System.out.println(“Insert an integer");
x=leer.nextInt();
System.out.println(“Integer written"+x);
Programming Fundamentals I 8
3.2.2 Selection (II)
• In addition to the instruction if in Java a conditional operator
exists, which is a “ternary” operator, becuase uses three
operands. The syntax is:
condition ? expression1 : expression2;
Programming Fundamentals I 9
3.2.2. Selection (III)
• Multiple selection: switch
switch (expression) {
case literal 1:
-- block 1--
break;
case literal 2:
-- block 2 --
break;
...
default:
-- block n --
}
Programming Fundamentals I 11
import java.util.*;
class Switch {
public static void main(String [] args){
int number ;
Scanner leer = new Scanner (System.in);
// Data reading
System.out.print(“Insert an integer: ");
number = leer.nextInt();
switch (number) {
case 1:
System.out.println(“The number is 1");
break;
case 2:
System.out.println(“The number is 2");
break;
default:
System.out.println(“The number is neither 1 nor 2");
} // End switch
Programming Fundamentals I 12
3.2.2. Selection (VI)
• Combination of control structures
– Concatenation
– Nesting
• Concatenation
Struct. 1
Struct. 2
Struct. 3
Programming Fundamentals I 13
3.2.2. Selection (V)
• Nesting
Struct. 1
Struct. 2
Struct. 3
Struct. 4
Programming Fundamentals I 14
3.2.2. Selección (VI)
• Example with concatenated ifs
if (price > 10.0) {
price = price*0.9;// Disccount 10%
}
if (quantity > 5) {
quantity ++; // An article free
}
Programming Fundamentals I 16
3.2.3. Loop (iteration) (II)
• Graphical representation of “while” (flow diagram)
Programming Fundamentals I 17
3.2.3. Loop (iteration) (III)
• Example
class Counter {
public static void main (String [] args) {
final int END=3;
int i=0,sum=0;
while (i<=END) {
System.out.print("Counter= "+i);
sum=i+2;
System.out.println(“Addition= "+sum);
i=i+1; // Increment of the counter (i)
}
System.out.println("Value of the counter out of loop=“+i);
System.out.println(“Final value of addition= "+sum);
}
}
Programming Fundamentals I 18
3.2.3. Loop (iteration) (IV)
• Type do-while. Syntax:
do {
---- block of actions ----
} while (condition);
Programming Fundamentals I 19
3.2.3. Loop (iteration) (V)
• Graphical representation “do-while” (flow diagram)
Programming Fundamentals I 20
• Example
class Counter{
public static void main (String [] args){
final int END=3;
int i=0, sum=0;
• Equivalent to
inicialization;
while (condition) {
---- block of actions -----
increment;
}
Programming Fundamentals I 22
3.2.3. Loop (iteration) (VIII)
• Graphical representation
“for” flow diagram)
Programming Fundamentals I 23
• Example
class Contador {
public static void main (String [] args) {
final int END=3;
int i,sum=0;
// Example of loop for
for(i=0;i<=END;i++) {
System.out.print(“Counter = "+i);
sum=i+2;
System.out.println(“Sum= "+sum);
}
}
}
Programming Fundamentals I 24
3.2.3. Loop (iteration) (X)
• The counter can be defined into the loop for. In this case it
exists only into the loop for.
for(int i=0;i<=END;i++) {
System.out.println(“Value of the counter: "+i);
}
Programming Fundamentals I 25
3.3. Representation techniques
• They allow working in a higher abstraction level with respect to
code.
• 2 possible techniques:
Programming Fundamentals I 26
3.3.1. Control flow diagram (I)
• Control Flow diagram. Symbols:
Process Connector
Input/Output
Programming Fundamentals I 27
3.3.2. Control Flow diagram(II)
Programming Fundamentals I 28
3.3.3. Pseudocode (I)
• Pseudocode (lenguage for specifiying algorithms)
– Begin and end
BEGIN, END
– Assignment
– Selection
• If
IF(condition) THEN -- block of actions --
ELSE -- block of actions --
END_IF
• Switch
SWITCH (variable) :
VALUE 1: --- block of actions 1 ---
...
VALUE n: --- block of actions n ---
END_SWITCH
Programming Fundamentals I 29
3.3.3. Pseudocode(II)
• Loop
– while
WHILE (condition)
--block of actions--
END_WHILE
– do-while
DO
--block of actions--
WHILE(condition)
– for
FOR inicial value WHILE condition INCREA/DECREA
-- block of actions --
END_FOR
Programming Fundamentals I 30
3.3.3. Pseudocode (III)
• Example. Addition of the first integers from 1 up to 10:
BEGIN
sum←0
num←1
DO
sum←sum+num
num←num+1
WHILE (num ≤10)
Write sum
END
Programming Fundamentals I 31
3.4. Arrays (I)
• In programming it is usual to find many elements from the
same type to be manipulated.
Programming Fundamentals I 32
3.4. Arrays (II)
• Elements are differenciated by an index:
a0, a1, a2, .... ß monodimensional case
ai j k , i=0, n
j=0, m ß tridimensional case
k=0, l
Programming Fundamentals I 34
3.4. Arrays (IV). Example
class Matrices_1 {
public static void main(String [ ] args) {
final int LIMITE=5; //vector’s dimension
} // End method
} // End class
Programming Fundamentals I 35
3.4. Arrays (V)
• An index cannot point to an element whose index is out of the
bounds of the arrray. In Java the exception
ArrayOutOfBoundsException is thrown.
• Difference between:
– Static dimensioning (in compilation time)
– dynamic dimensioning (in runtime)
Programming Fundamentals I 36
3.4. Arrays (VI). Example
import java.util.*;
class Matrices {
public static void main (String [ ] args) {
int limit;
Scanner scan =new Scanner (System.in);
limit= scan.nextInt(); // Read the length
int [ ] list = new int [limit];
} // End method
} // End class
Programming Fundamentals I 37
3.4. Arrays (VII)
• In Java the size of an array can be always read through the
constant variable length:
length = lista.length;
Programming Fundamentals I 38
3.4. Arrays (VIII)
• Multidimensionales arrays.
Example: a bidimensional array:
int [ ] [ ] twoD = new int [4][5];