0% found this document useful (0 votes)
17 views7 pages

Ps Exp 3

Uploaded by

neerajg0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Ps Exp 3

Uploaded by

neerajg0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment 03- Control Flow Statements & Arrays

Learning Objectives: To understand usage of Control Flow Statements, and to study


declaration and initialization of Arrays.

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

I) Syntax of if statement is given below.


if(condition) {
statement 1; //executes when condition is true
}
II) Syntax of if else statement is given below.

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
}

IV) The syntax to use the switch statement is given below.


switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}

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.

To declare an array, define the variable type with square brackets:


String [] cars;

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:

String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:


Int [] myNum = {10, 20, 30, 40};

Access the Elements of an Array:

You can access an array element by referring to the index number.

This statement accesses the value of the first element in cars:


String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars [0]);
// Outputs Volvo

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:

Problem Statement 3: Write a Java program to display the following pattern.


*****
****
***
**
*
Program:

Output:

Learning Outcomes: Students will able to


LO1.1 Understand the Control Flow Statements.
LO1.2 Understands the usage of loops to print different patterns.
LO1.3 Understands about declaration and initialization of arrays.

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:

Q1.) What is the syntax of if Statement?


if(condition){
st 1; //executes if condition is true
}

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.

Q3). What are arrays?


Ans: Arrays are a collection of similar data element like int, float, char. Arrays are used to
store many data elements which are of the same type together so it’s easier to perform any
function on them together.

For Faculty Use only:

Correction Formative Timely completion Attendance / Total


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]
Marks
Obtained

You might also like