Class 5++++++
Class 5++++++
Flow-control cycles
Loops
Operator:
If-else
if (logical condition)
command_1;
else
command_2;
if (a < b){
a = 0;
}
if (a < b)
a = 0;
Operator:
switch
switch(statement)
{
case value1: code_block_1;
case value2: code_block_2;
case value3: code_block_3;
}
Some expression or variable is specified inside the brackets. If the value of the
expressions is equal to the value 1, code 1 begins to be executed, if it is equal to the
value 2, code 2 begins to be executed, if it is equal to the value 3, code 3 begins to be
executed.
switch(temperature)
{
case 36: System.out.println("Low");
case 37: System.out.println("Normal");
case 38: System.out.println("High");
}
int temperature = 36;
switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
}
}
else {
….
}
}
}
Exercise 1
1.Program must read 2 numbers from the user
2.If the first number is greater than the second, then program must display it on the
screen, if not, then the second number
3.Complete missing parts in the program
4. Which block will be executed if a =b?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
if ( …) {
}
else {
….
}
}
}
Exercise 2
1.Program must read 2 numbers from the user: m & n
2.If an integer m is completely divisible by an integer n, then display
the quotient of the integer division of m by n, otherwise display the
message "not completely divisible".
Example:
№ Input Output
1 16 2 8
not completely
2 16 3
divisible
Exercise 3
1.Program must read 3-digits numbers from the user
2.Determine which of its digits is greater: the second or the last.
3.Display the larger of these two digits.
Example:
№ Input Output
1 354 5
2 678 8
Exercise 4*
1.Program must read 2 numbers from the user: number n and the
number a
2.Program must determine whether the sum of n digits is a
two-digit number (print YES or NO in the first line);
3.Program must determine whether the number a is greater than
the sum of the digits of the entered number n (print YES or NO in
the second line).
Example:
№ Input Output
25 NO
1
7 NO
Cycles (flow
control cycles)
While loop
while(boolean condition)
set of commands in {}
int i = 3; 3
while (i >= 0) { 2
System.out.println(i); 1
i--; 0
}
int i = 0;
while (i < 3){
System.out.println(i);
i++;
}
while (true)
System.out.println("C");
while (true)
{
String s = buffer.readLine();
System.out.println(s);
if (s.equals("exit"))
break;
}
Exercise 5
Given a fragment of the program, determine how many times the body of
the while loop is executed and what is the value of the variable a after
executing this loop.
Ex 1:
a = 8;
b = 12;
while (a < b)
a++;
Ex 2:
a = 32;
b = 40;
while (a < b)
a+=b;
Ex 3:
a = 8;
b = 12;
while (a < b)
a--;
Exercise 6
Use the while loop to display numbers from 1 to 10
inclusive. Each value from a new line.
Requirements:
* The program should not read numbers from the
keyboard.
* The program should display numbers on the screen.
* Each value must be derived from a new line.
* The program should display numbers from 1 to 10
inclusive.
Alternative reading input from the keyboard:
import java.io.BufferedReader;
import java.io.InputStreamReader;
….
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name=br.readLine();
Exercise 7
Enter a string of characters from the keyboard and a number n greater
than 0.
Display a string of n times using the while loop. Each value from a new
line.
Sample input data:
ABC
2
Hint: Use
Example output:
ABC
ABC
Requirements:
* The program must read the text from the keyboard.
* The program should display text on the screen.
* Each value must be derived from a new line.
* The program should display a sequence of n times on the screen.
* The while loop should be used in the program.
import java.io.BufferedReader;
import java.io.InputStreamReader;
}
}
Exercise 8
Run the program.
Look at the result of her work.
Explain how it works
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, count;
n = in.nextInt();
count = 0;
while (n != 0) {
count ++;
n = n / 10;
}
System.out.println("Number "+ n+ " contains " + count + " digits");
}
}
For loop
for(statement 1; statement 2; statement 3)
set of commands in {}
The first expression in the for loop is executed once before the loop.
The second expression is executed each time before executing the body of the loop -
similar to the condition in the while loop.
The third is every time after the execution of the body of the cycle.
Example:
№ Input Output
1 1 5 15
Exercise 12
Display using the for loop:
- a horizontal line of 10 eights
- a vertical line of 10 eights (the character from the
horizontal line is not taken into account).
Requirements:
* The program should not read numbers from the
keyboard.
* The program should display numbers on the screen.
* The program should output a horizontal line of 10
eights.
* The program should output a vertical line of 10
eights.
Exercise 13*
Display the multiplication table 10 by 10 as follows:
1 2 3 4 ...
2 4 6 8 ...
3 6 9 12 ...
4 8 12 16 ...
...
Requirements:
* The program should display text.
* The printed text must contain 10 lines.
* Each resulting row must contain 10 numbers separated
by a space.
* The output numbers must be a multiplication table.
Exercise 14*
Enter two numbers m and n from the keyboard.
Using the for loop, display a rectangle of size m by n built
of the eights.
Example: m=2, n=4
8888
8888
Requirements:
* The program must read two numbers from the keyboard.
* The program must display text on the screen.
* The program should output a rectangle of size m by n
from the eights.