Topic 2 - Java Programming Basics (Part 2)
Topic 2 - Java Programming Basics (Part 2)
➢ Control Structures
➢ Array of Primitives
➢ Packages
CHAPTER OUTLINE
Most of the programming languages use control structures to control the flow of a program.
Types of control structures:
Selection statement
If-else
Switch-case
Loop (needs loop entry, loop test, iteration and termination)
for loop
while loop
do-while loop (suitable for menu selection)
CONTROL STRUCTURE - SELECTION
1. One Way Selection
Syntax:
if (expression)
statement
switch (month)
{
switch (expr) case 1: monthString = "January";
{ break;
case option1: statements; case 2: monthString = "February";
break; break;
case option2: statements; case 3: monthString = "March";
break; break;
default: statements; case 4: monthString = "April";
break; break;
}
default: monthString = "Invalid month";
}
System.out.println(monthString);
CONTROL STRUCTURE - LOOP
Why Loop is Needed?
1. For Loop
for (initialize; boolean testexpr; increment)
• Its primary purpose is to simplify the
{
writing of counter-controlled loops.
statements or block;
• For this reason, the for loop is typically
}
called a counted or indexed for loop.
Example:
2. While Loop
• Statements must change value of
expression to false.
• A loop that continues to execute
endlessly is called an infinite loop.
(expression is always true)
Example:
while (boolean)
{ while (count < 11)
statements or block; {
} System.out.println("Count is: " + count);
count++;
}
CONTROL STRUCTURE - LOOP
3. Do While Loop do
• Statements are executed first and then {
expression is evaluated. statements or block;
• Statements are executed at least once } while (boolean test);
and then continued if expression is
true.
Example:
do
{
System.out.println("Count is: " +
count);
count++;
} while (count < 11);
ARRAY OF PRIMITIVES
BASIC ARRAY
Array Creation
<variable> = new <data type> [ <size> ]
Example
Variation 1 Variation 2
double[ ] rainfall; double rainfall [ ];
rainfall = new double[12]; rainfall = new double[12];
Like other data types, it is possible to declare and initialize an array at the same time.
int[] number = { 2, 4, 6, 8 };
number.length 4
samplingData.length 9
monthName.length 12
VARIABLE – SIZE DECLARATION
System.out.print("Size of an array:"));
size= scanner.nextInt( );
files in one directory (or package) would have different functionality from those of
another directory
For example,
files in java.io package do something related to I/O, but
files in java.net package give us the way to deal with the Network.
In GUI applications, it's quite common for us to see a directory with a name "ui" (user interface),
meaning that this directory keeps files related to the presentation part of the application.
TO CREATE JAVA PACKAGE
Suppose we have a file called HelloWorld.java, and we want to put this file in a package
world.
Example:
// only comment can be here
package world;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
create a directory world and put our HelloWorld.java into it.
JAVA BASIC INPUT/OUTPUT (EXTRA)
STANDARD OUTPUT
Using System.out, we can output multiple lines of text to the standard
output window.
We use the print method to output a value to the standard output
window.
Example:
int x = 123, y = x + x;
System.out.println( "Hello, Dr. Caffeine.“ );
System.out.print( " x = “ );
System.out.println( x );
System.out.print( " x + x = “ );
System.out.println( y );
System.out.println( " THE END“ );
STANDARD INPUT
Scanner scanner;
Method Example