North South University
Department of Electrical and Computer Engineering
CSE 215L (Programming Language II Lab)
Lab 3: Conditional statements, Loops
Lab Instructor: Arfana Rahman
Objective:
• How to use the conditional statements such as if-else and switch
• How to use the loops
Statement:
Any meaningful expression is known as statement.
Example: int number = 5;
Conditional Statements:
Conditional statements are used for checking whether a certain condition is being fulfilled
or not and we commonly use if-else and switch statements. How they are used is given
below:
if
if (condition){
// Execute the code in this if block
}
if-else
if (condition){
// Execute the code in this if block
}
else {
// Execute the code in this else block
}
else if (ladder)
if (condition){
// Execute the code in this if block
}
else if (condition){
// Execute the code in this if block
}
else if (condition){
// Execute the code in this if block
}
else {
// Execute the code in this else block
}
CSE215 LAB MANUAL 3 ARFANA RAHMAN
Switch Syntax:
switch (expression){
case value1:
// Execute the given code
break;
case value2:
// Execute the given code
break;
default:
// Execute the given code
break;
Keywords used in switch statement → switch, case, break, default
Loops:
Repetition statements allow us to execute a statement for multiple times.
In Java, we use the following types of loops such as the for, while, and do-while loops, and
how they are used are given below:
for loop while loop do-while loop
for (int i=0; i<10; i++) { initialization; initialization;
// Statement(s) while (condition) { do {
} // Statement(s) // Statement(s)
//Any required increment/decrement // Any increment/decrement
} } while (condition);
Classwork:
1. (Using if-else statement) Take a user input for your name and check whether it starts
with an Uppercase Vowel. If the condition is fulfilled, write “My name starts with a
vowel”, else write “My name starts with a consonant”.
2. (Using while loop) Take an integer (n) through user input and print the summation of
all numbers from zero to n.
CSE215 LAB MANUAL 3 ARFANA RAHMAN