0% found this document useful (0 votes)
5 views

flow control statements in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

flow control statements in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

OCS1902

OOPS USING
JAVA
SESSION 2
Operators and
Branching
Statements
01
Table of contents
0
1 Loop Statements

0
2 Problem
Solving
01
Loop
Statements
Loop Statements
• Loop statements are used to repeat
lines of code.
• Statements that execute a block of
code repeatedly until a specified
condition is met are known as looping
statements.
• Java provides three types of loops:
while
do-while
for
 Known
While Statement
as the most
common loop, the while
loop evaluates a certain
condition.
 If the condition is true,
the code is executed.
This process is continued
until the specified
condition turns out to be
false.
 The condition to be
specified in the while
loop must be a Boolean
expression. An error will
be generated if the type
While Statement -
Example
Output
5
7
9
11
13
15
do-while Statement
 It is similar to the
while loop, the only
difference being that
the condition in the
do-while loop is
evaluated after the
execution of the loop
body.
 This guarantees that
the loop is executed
do-while Statement -
Example

Output
20
for Statement
 It is used to iterate and evaluate a code multiple times.
When the number of iterations is known by the user, it
is recommended to use the for loop.

 Syntax:
for (initialization; condition; increment/decrement)
{
statement;
}
for Statement - Example
Output
1
2
3
4
5
6
7
8
9
10
for-each Statement
• The traversal of elements in an array(any
sequence) can be done by the for-each loop.
The elements present in the array are
returned one by one.
• It must be noted that the user does not
have to increment the value in the for-each
loop.
for-each Statement -
Example Output
9
5
0
0
2
3
0
6
0
8
How Do You Exit a Loop Early?
• Usually, the only way to exit a loop is for the loop
condition to evaluate to false
• However, it’s often convenient to terminate a loop
early when certain conditions are met
• In such cases, continuing to loop would be a waste
of processor time
• You can use two Java statements to terminate a
loop early:
−break
−continue
Using break in a Loop
• When a break statement is executed inside
a loop, the loop statement is terminated
immediately.
• The program continues to execute with the
statement following the loop statement.
• Syntax:
break;
Using break in a while
Loop - Example
Control passes to the
statement outside
the loop

Output
0
1
2
3
Using break in a for Loop
- Example Control passes to the
statement outside
the loop

Output
5
6
7
Using continue in a Loop
• To jump to the next iteration of the loop, we
make use of the continue statement.
• This statement continues the current flow
of the program and skips a current
iteration.
• Syntax:
continue;
Using continue in a while
Loop
Using continue in a for
Loop Control passes to the
loop condition

Output
01235678
9
02
Problem Solving
Problem Statement 1
Write a program that reads input an integer n and an integer k.
You have to print the numbers 1 to n, each being printed k
times. Sample Input
Example, if n is 4 and k is 2 then print: 2 //number of input
11 4 //n value(1st input)
2 //k value
22
2 // n value (2nd input)
33 10 //k value
44 Output:
Input format: 11
The first line contains the number of inputs. 2 2
33
The lines after that contain a different values 4for
4
n and k
1111111111
2222222222
Solution 1
import java.util.Scanner;

public class Demo{


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n, k, i, j, m, no;
n = scan.nextInt(); //input number of test cases
for(i = 1; i<= n; i++) {
no = scan.nextInt();
k = scan.nextInt();
for(j = 1; j<= no; j++) {
for(m = 1; m<= k; m++) {
System.out.print(j +” ”);
}System.out.println();
}
}
}}
Problem Statement 2
Write a program that prints a simple chessboard. You have to read the the size
of the chessboard from stdin. Print W for white spaces and B for black spaces.
Input format:
The first line contains the number of inputs.
The lines after that contain a different values for size of the chessboard.
Example Input:
2
3
5
Output:
WBW
BWB
WBW
WBWBW
BWBWB
WBWBW
BWBWB
WBWBW
Solution
import java.util.Scanner;
2
public class Demo{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input:Number of test cases


System.out.print("Enter Number of test cases:
");
n = sc.nextInt();
while(n > 0) {
size = sc.nextInt();
count = 0;
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++) {
if(++count % 2 == 1)
System.out.print("W");
else
System.out.print("B");
}
if(size % 2 == 0)
count++;
System.out.println();
}n--;
}
Problem Statement 3
Task: We are now interested in gardening! Our garden will have 2 things:
flowers and grass.
A flower is represented by # and grass by .
Write a code that takes an integer N and makes the garden pattern like in the
examples.
Input Format: INPUT:
•The input is an integer N.
3
Output: OUTPUT:
The program should INPUT:
display a pattern.
2 #.#.#.#.#.#.#
OUTPUT: #.#.#...#.#.#
INPUT: #.#.......#.#
1 #.#.#.#.#
#.#...#.# #...........#
OUTPUT: #.#.......#.#
#.#.# #.......#
#.#...#.# #.#.#...#.#.#
#...# #.#.#.#.#.#.#
#.#.# #.#.#.#.#
You Write the
Solution
Thank
you….
Thank
you!
30

You might also like