Ps Exp 3
Ps Exp 3
Theory:
Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, Java provides statements that can be
used to control the flow of Java code. Such statements are called control flow statements. It is
one of the fundamental features of Java, which provides a smooth flow of program.
Java provides three types of control flow statements.
1. Decision Making statements
o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
III) Syntax of if-else-if statement is given below.
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
V) For Loop
for(initialization, condition, increment/decrement) {
//block of statements
}
VI) While Loop
while(condition){
//looping statements
}
Java Arrays:
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
We have now declared a variable that holds an array of strings. To insert values to it, you can
place the values in a comma-separated list, inside curly braces:
Problem Statement 1: Write a java program to understand control flow statements (Find if a
Number is Armstrong or not).
Ans:
PROGRAM:
OUTPUT:
Problem Statement 2: Write a java program to find the smallest and largest element from the
array.
PROGRAM:
OUTPUT:
Output:
Course Outcome:
CO1: Understand about Various Control flow statements and concepts of Arrays
Conclusion:
With the help of control loop structure’s, we were able to simplify our code and perform the
iteration in java with some examples like Armstrong Number. Also, we were able to
understand the concept of arrays which are used to store similar type of data. We performed
looping pattern operations and learnt how can we print different types of patterns
Viva Questions:
Q2). What are the differences between while and do while loop?
Ans: In while loops, the condition is checked before the execution of code written in the loop.
While in do-while loop, the code of the loop is executed once before the checking of
condition. Thus, a do-while loop is run once, no matter the condition.