Professional Documents
Culture Documents
Conditional branch, A programming instruction that directs the computer to another part
of the program based on the results of a compare. High-level language statements, such
as IF THEN ELSE and CASE, are used to express the compare and conditional branch.
What is an array?
Arrays a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables. A specific element in an array is accessed
by an index.All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
To declare an array a programmer specifies the type of the elements and the number
of elements required by an array as follows −
type arrayName [ arraySize ];
If-else statement
This is how an if-else statement looks:
if(condition) {
Statement(s);
}
else {
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the statements
inside “else” would execute if the condition is false.
Example:
class Day {
public static void main(String[] args) {
int week = 4;
String day;
switch (week) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
day = "Invalid day";
break;
}
System.out.println(day);
}
}
Output:
Wednesday
Example:
class WhileLoopExample {
public static void main(String args[]){
int i=10;
while(i>1){
System.out.println(i);
i--;
}
}
}
Write about do-while with example
In while loop, condition is evaluated before the execution of loop’s body but in do-while
loop condition is evaluated after the execution of loop’s body.
Syntax of do-while loop:
do
{
statement(s);
} while(condition);
How do-while loop works?
First, the statements inside loop execute and then the condition gets evaluated, if the
condition returns true then the control gets transferred to the “do” else it jumps to the
next statement after do-while.
Example:
class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}
Write about for loop with example
Loops are used to execute a set of statements repeatedly until a particular condition is
satisfied. In Java we have three types of basic loops: for, while and do-while. In this we
will learn how to use “for loop” in Java.
Syntax of for loop:
for(initialization; condition ; increment/decrement)
{
statement(s);
}
Flow of Execution of the for Loop:
As a program executes, the interpreter always keeps track of which statement is about
to be executed. We call this the control flow, or the flow of execution of the program.
First step: In for loop, initialization happens first and only one time, which means that the
initialization part of for loop only executes once.
Second step: Condition in for loop is evaluated on each iteration, if the condition is true
then the statements inside for loop body gets executed. Once the condition returns false,
the statements in for loop does not execute and the control gets transferred to the next
statement in the program after for loop.
Third step: After every execution of for loop’s body, the increment/decrement part of for
loop executes that updates the loop counter.
Fourth step: After third step, the control jumps to second step and condition is
re-evaluated.
Example:
class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--){
System.out.println("The value of i is: "+i);
}
}
}
The output of this program is:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2
small = arr[0];