I really don’t understand what to do {} while the structure

I try to learn Java, studied Pascal in high school and had instruction repeat until..;.

I want to solve an exercise in which I have to continue to enter numbers until the penultimate + antepthultative numbers coincide with the last number I entered. (a[i-2]+a[i-1] = a[i]);I do this without arrays, but that doesn't really matter.

In Pascal it would be easy, because repeating until it is easier to use For ex it would be

repeat  
...   
until ((a[i-2]+a[i-1] = a[i]) and (n=3)); 

n - number of entered values

I cannot figure out how to present it in Java while I did this, but it does not work if I enter 2 2 4. He has to stop, but he keeps asking for numbers

    int pen = 0, ant = 0, s = 0, n = 1;
    int ult = input.nextInt();
    s = s + ult;
    do {
        do {
            ant = pen;
            pen = ult;
            ult = input.nextInt();
            n++;
            s = s + ult;
        } while (ant + pen != ult);
        System.out.println(n);
    } while ((n == 3) || (n < 4));

ult- This is the last number I entered s- the sum of the numbers entered.

- , , , 2 2 4?

+4
5

Do-While . , , , , .

, , , . , , , .

. , , .

+5

Java , Pascal, , :). , , - Java, , , - , n , ints, .

int pen = 0, ant = 0, ult = 0, n = 0;

do {
    ant = pen;
    pen = ult;
    ult = input.nextInt();
} while (++n < 3 || ant + pen != ult );

assert n >= 3;
assert ant + pen == ult;

, , Pascal , .

+1

:

repeat
  doStuff();
until (boleanValue);

Java , :

do 
  doStuff();
while (~boleanValue);

, "~" booleanValue. Pascal repeat ... until , boolean true. Java do ... while , boolean false. Pascal Java , .

+1

while do - while , while , do-while

while:

:

while (expression) {
     statement(s)
}

enter image description here

( http://www.w3resource.com/c-programming/c-while-loop.php)

:

public class WhileDemo{

    public static void main(String args[]) {
        boolean isSunday = false;
        while(isSunday) {
            System.out.println("Yayy.. Its Sunday!!");
        }
    }

}

: ( )

: isSunday ,

do-while: . do .

:

do {
     statement(s)
} while (expression);

enter image description here

( http://www.w3resource.com/c-programming/c-do-while-loop.php)

:

public class DoWhileDemo{

    public static void main(String args[]) {
        boolean isSunday = false;
        do {
            System.out.println("Yayy.. Its Sunday!!");
        } while(isSunday);
    }

}

: Yayy.. !

: do , , Yayy.. ! while(isSunday); false, isSunday false,

+1

. Pascal , - .

Java .

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

:

int n = 0;
int a[] = new a[3];
do {
   n++;
   a[0] = a[1];
   a[1] = a[2];
   a[2] = input.nextInt();
} while ((n < 3) || (a[0]+a[1] != a[2])); 
System.out.println(a[2]);
0

Source: https://habr.com/ru/post/1650835/


All Articles