You are on page 1of 7

Lab6:FlowofControl– Looping-Solution

Objectives:
By completing this lab, students should be able to:
Learn different kind of loop concept (while, do-while, andfor)
Learn how to use the nestedloop.

Syntax Reference with examples:


Following are the syntaxes for the required commands.
do-while Loop Example:
int start = 10;
Statement Syntax: do{

do{ System.out.println(start);
Statement_1; start= start +10 ;
Statement_2;
... }while(start< =30);
Update;
}while(Boolean_Expression) The output:

10
20
30

While loop statement Example (1):


int start = 50;
Statement Syntax: while (start< =56){
System.out.print(start +” “);
while(Boolean_Expression){ start ++ ;
}
Statement_1;
Statement_2;
The output:
...
Update; 50 51 52 53 54 55 56
}

Example (2):
int start = 10;
while (start< =40){
System.out.print(i +” “);
start = start +10 ;
}

The output:

102030 40

SARAH AHMED & AFRAA SAYAH 1


switch Statement Syntax
Example (1):
Statement Syntax:
for(int start = 0; start <= 3; start++)
{
for(initializing; Boolean_Expression; Update) System.out.println(start);
{ }
Statement_Block
}
The output:
0
NOTE: 1
2
• Initializing: 3
just like a counter and provides the initial
value of the loop . It is executed once when
the loop starts.
for(i=2;i<14;i=i+2){ Example (2):
• BooleanExpression:
determinesSystem.out.println(i);
when the loop should} end, based int i=2;
onthe evaluation of a Boolean expression
before each iteration for(i=2;i<12;i=i+2){
• Update: System.out.println(i);
tells how the control variable or variables
}
are updated after
each iteration of the loop body
• Body: The output:
repeated section .
2
4
6
8
10

Example (3):
int rowNum, columnNum;
for (rowNum = 1; rowNum<=3; rowNum++)
{
for (columnNum = 1; columnNum<=2;columnNum++)
System.out.print(" row " + rowNum+ "column "+ columnNum);
System.out.println();
}

The output:

row 1 column 1 row 1 column 2


row 2 column 1 row 2 column 2
row 3 column 1 row 3 column 2

SARAH AHMED & AFRAA SAYAH 2


Instructions:
Task # 1: Creating a new Project in JCreator.
1. Find the JCreatoricon in your PC. It should on Desktop or on C Driver. Then double click on the
JCreatoricon.
2. Go to File ➔New➔Project.
3. Select Empty Java Project. Then Click Next button.
4. Write the Project Name “Lab6”. Then choose the location e.g. ICS102Labs folder on the Desktop.
Then Click Next ➔Finish.

Exercise 1:
1. Create class named itLab6Exercise1
2. Write a java program by using a while-loop to display the first 24 numbers starting from6.

Solution:
public class Lab6Exercise1{
public static void main (String [ ]args) {
int j=6;
while (j<=24){
System.out.println(j);
j++;
}
}}

Exercise 2:
1. Create class named itLab6Exercise2
2. Write a java program by using a do-while loop to display the numbers from32 , 28, 24to 6.

public class Lab6Exercise2{


public static void main (String [ ]args) {
int i=32;
do{
System.out.print(i+ " ");
i= i -4 ;
}while (i>=6);
System.out.println( );
}}

SARAH AHMED & AFRAA SAYAH 3


Exercise3:
1. Create class named itLab6Exercise3
2. Write a java program by using a for-loop to calculate and display the sum of even
numbers between 2 and 60.

Solution:
public class Lab6Exercise3{
public static void main (String [ ]args) {
int sum=0;
for(int i=2; i<=60;
i=i+2){sum= sum+i;}
System. out .println("The sum of even numbers between 2 and 50 is: "+
sum);}}

Exercise4:
1. Create class named itLab6Exercise4
2. Write a java program that will display the following:

UOH, CSSE Department


UOH, CSSE Department
Kingdom of Saudi Arabia

UOH, CSSE Department


UOH, CSSE Department
Kingdom of Saudi Arabia

UOH, CSSE Department


UOH, CSSE Department
Kingdom of Saudi Arabia

UOH, CSSE Department


UOH, CSSE Department
Kingdom of Saudi Arabia

Hint: Use only one loop.

SARAH AHMED & AFRAA SAYAH 4


Solution:
public class Lab6Exercise4{
public static void main (String [ ]args) {
for(int i=1; i<=8; i++){

System. out .println("UOH, CSSE Department");


If(i%2==0){
System. out .println("Kingdom of Saudi Arabia ");
System. out .println("");} }}

Exercise 5:
1. Create class named itLab6Exercise5
2. Write a program that reads an alphabetic character and a number of repeat
then prints a triangle of alphabet. Example:
t
tt
ttt
tttt
ttttt

Solution:
import java.util.Scanner;
class Lab {
public static void main(String[] args) {
System.out.println("Enter Char");
Scanner in=new Scanner(System.in);
String a=in.next();
Char x= a.charAt(0);
System.out.println ("Enter number");
int n=in.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print (a);}
System.out.println(); } }}

SARAH AHMED & AFRAA SAYAH 5


SARAH AHMED & AFRAA SAYAH 6
SARAH AHMED 7

You might also like