[go: up one dir, main page]

0% found this document useful (0 votes)
11 views31 pages

Class 5++++++

The document provides an overview of flow-control cycles in programming, focusing on loops, if-else statements, and switch operators. It includes examples and exercises for implementing these concepts in Java, such as reading user input and determining conditions. Additionally, it covers while and for loops with various exercises to practice these programming constructs.
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)
11 views31 pages

Class 5++++++

The document provides an overview of flow-control cycles in programming, focusing on loops, if-else statements, and switch operators. It includes examples and exercises for implementing these concepts in Java, such as reading user input and determining conditions. Additionally, it covers while and for loops with various exercises to practice these programming constructs.
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/ 31

Class 5

Flow-control cycles
Loops
Operator:
If-else
if (logical condition)
command_1;
else
command_2;

a = 8; b = 4; If A is smaller than B, the first


if (a < b) command is executed, otherwise
System.out.println("А is smaller than B"); the second command is executed.
else Commands will never be executed
System.out.println("А is not smaller than B"); at the same time.

if (a < b){ Instead of one command, you can


System.out.println("А is smaller than B"); replace the command block in
System.out.println("B is greater than A"); braces (necessarily)
} else {
System.out.println("B is not greater than A");
System.out.println("А is not smaller than B");
}

if (a < b){ The else block can be omitted if it


a = 0; is empty.
} else { The 3 examples given below are
absolutely equivalent.
}

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.

int temperature = 38;


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");
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");
}

int temperature = 40;


switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
break;
default:
System.out.println("Please call the doctor!");
}
int temperature = 40; int temperature = 40;
switch(temperature)
{ if (temperature == 36)
case 36: {
System.out.println("Low"); System.out.println("Low");
break; }
case 37: else if (temperature == 37)
System.out.println("Normal"); {
break; System.out.println("Normal");
case 38: }
System.out.println("High"); else if (temperature == 38)
break;
{
default:
System.out.println("High");
System.out.println("Please call the
}
doctor!");
else
}
{
System.out.println("Please call the doctor!");
}
Day day = Day.MONDAY;
switch (day)
{
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
}
Exercise 0
1. Write a program where the user is asked to enter a
number to choose from: 1, 2 or 3
2. The program should tell which number the user
entered: 1, 2, or 3.
3. Do this task in 2 variants (using both: if-else
construction and switch operator)
Task 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 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;

public class Main {


public static void main(String[] args) throws Exception {

}
}
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.

int i = 3; for (int i = 3; i >= 0; i--){


while (i >= 0) { System.out.println(i);
System.out.println(i); }
i--;
}

int i = 0; for (int i = 0; i < 3; i++){


while (i < 3){ System.out.println(i);
System.out.println(i); }
i++;
}

while (true) for (; true; )


System.out.println("C"); System.out.println("C");

while (true) for (; true; )


{ {
String s = buffer.readLine(); String s = buffer.readLine();
System.out.println(s); System.out.println(s);
if (s.equals("exit")) if (s.equals("exit"))
break; break;
} }
Exercise 9
Using the for loop, complete the program so that it
finds the sum of numbers from -10 to b, where the
value of b is entered from the keyboard.
Requirements:
* The program must read b number from the
keyboard.
* The program must sum numbers from -10 to b.
* The program must output result on the screen.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i, s, b;
... // read b from the keyboard
s = 0; //update s to accumulate sum value
for (i = __; i<=___; i++) {
s = s + i;
}
System.out.println(s);
}
}
Exercise 11
Write a program that finds the sum of integers from a to b (using
for-loop), where a and b are entered from the keyboard.

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.

You might also like