Lesson 3 - Control Structures
Lesson 3 - Control Structures
with
Lesson 3:
Java Control Structures, Arrays and Strings
Control Structures
These are statements that are used to define a specific way of execution of a
program
2. Loop / iterative
Selection
a) if
b) if….else
c) if….else if
d) Nested if
e) Switch
Java IF Statement
The Java if statement tests the condition. It executes the if block if condition is
true.
Syntax:
if(condition){
//code to be executed
}
Example: Java IF Statement
int age=20;
if(age>18){
}
Java IF-else Statement
The Java if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.
Syntax:
if(condition){
}else{
}
Example: Java IF-else Statement
int number=13;
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
Java IF-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Example: Java IF-else-if Statement
public static void main(String[] args) {
else if(marks>=70 && marks<80){
int marks=65;
System.out.println("B grade");
if(marks<50){ }
System.out.println("fail"); else if(marks>=80 && marks<90){
} System.out.println("A grade");
else if(marks>=50 && marks<60){ }else if(marks>=90 && marks<100){
System.out.println("D grade"); System.out.println("A+ grade");
} }else{
System.out.println("Invalid!");
else if(marks>=60 && marks<70){
}
System.out.println("C grade");
}
}
Example: if … else if statement
public class JavaApplication5 {
if (num > 0) {
System.out.println("The number is positive");
} else if (num < 0) {
System.out.println("The number is negative");
} else {
System.out.println("The number is zero");
}
}
}
Nested if
This is a condition where you can have an if statement inside another if statement.
Example:
import java.util.*;
public class SelectionOperator {
public static void main(String[] args) {
int age=17, cash=200;
if (age>=18){
if (cash>=300){
System.out.println("You can go for a drink");
}
}
else {
System.out.println("Please stay home");
}
}
}
Switch case
public class JavaApplication5 {
1. for loop
2. while loop
The condition is set in the first line and should be met during the execution of
the statement
Example:
int i;
for(i=1; i<=5; i++){
System.out.println(i + " There we go again");
}
While loop
Example:
int x=1;
while(x<=5){
System.out.println(“ Welcome to Java Programming");
x++;
}
Do ….. While loop
Example:
int y=1;
do{
System.out.println("Do while loop will print 5 times");
y++;
} while(y<=5);
Java Break Statement
The Java break is used to break loop or switch statement. It breaks the current
flow of the program at specified condition. In case of inner loop, it breaks only
inner loop.
Example:
public static void main(String[] args) {
for(int i=1; i<=10; i++){
if(i==5){
break;
}
System.out.println(i);
}
}
Java Continue Statement
The Java continue statement is used to continue loop. It continues the current flow of
the program and skips the remaining code at specified condition.
In case of inner loop, it continues only inner loop.
Example:
public static void main(String[] args) {
for(int i=1; i<=10; i++){
if(i==5){
continue;
}
System.out.println(i);
}
}
Practice Questions
To manipulate an element in an array you need to use the array name and the index
or subscript
An array data block can be referenced using a universal name called arrayName
Example
double[] myList;
or
double myList[];
Declaring and Creating Arrays
To create an array object, you specify the type of the array elements and the number
of elements as part of an array-creation expression that uses keyword new.
Example
int myList[][] = {{60,80,90},{100,120,150}}
Java for-each Loop
class Main {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
// iterating through an array using the for-each loop
for (char item: vowels) {
System.out.println(item);
}
}
}
Traversing the collection elements
import java.util.*;
class MyExample{
public static void main(String args[]){
//Creating a list of elements
ArrayList<String> list=new ArrayList<String>();
list.add(“Tadiwa");
list.add(“Lisa");
list.add(“Onias");
//traversing the list of elements using for-each loop
for(String s:list){
System.out.println(s);
}
}
}
Practice Question 1
Using an array mark[] write a program that accepts 5 student marks, display the
marks, determine and display the following:
a) Total marks
b) Lowest mark
c) Highest mark
d) Average mark
Practice Question 2
Write a program that accepts sales from 10 sales reps, determine and display the
following:
a) Average sales
c) Total Commission
d) Average Commission
Practice Question 3
Design a program that enter 10 student marks between 0 and 100. The program
should distinguish the number of student who have failed and passed
0 to 49 fail
50 to 100 pass
Java - Strings Class
The Java platform provides the String class to create and manipulate strings.
As with any other object, you can create String objects by using the new
keyword and a constructor
Example
public static void main(String args[]) {
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
Accessor Methods - String Length
Methods used to obtain information about an object are known as
accessor methods.
One accessor method that you can use with strings is the length()
method, which returns the number of characters contained in the string
object.
The following program is an example of length(), method String class.
Example
public static void main(String args[]) {
String myStmnt = “We are getting there";
int len = myStmnt.length();
System.out.println( "String Length is : " + len );
}
Concatenating Strings