/* Suma1.java Program wyznacza sumę liczb, suma = 1 + 2 + 3. */ public class Suma1 { public static void main(String[] args) { int suma = 0; int i = 1; suma = suma + i; // suma = 0 + 1 = 1 i++; // i = 2 suma = suma + i; // suma = 1 + 2 = 3 i++; // i = 3 suma = suma + i; // suma = 3 + 3 = 6 i++; // i = 4 System.out.println("suma = " + suma); System.out.println("i = " + i); } }
suma = 6 i = 4 Press any key to continue...
/* Suma2.java Program wyznacza sumę liczb, suma = 1 + 2 + ... + n, gdzie n = 3. */ public class Suma2 { public static void main(String[] args) { int n = 3; int suma = 0; int i; for (i = 1; i <= n; i++) { suma = suma + i; } System.out.println("suma = " + suma); System.out.println("i = " + i); } }
suma = 6 i = 4 Press any key to continue...
Zadanie Dokonaj analizy programu Suma2.
n = 3 suma = 0 i i = 1 1 <= 3 suma = 0 + 1 = 1 i = 2 2 <= 3 suma = 1 + 2 = 3 i = 3 3 <= 3 suma = 3 + 3 = 6 i = 4 4 <= 3 false "suma = 6" "i = 4"