04 - UP 2023-Predavanja

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32


– while
– do-while
– for
– for



while
• while
• while
true

while (uslov){
naredba;
}

• while

• (uslov) while
while
while

иницијализација
бројача
int brojač = 0;
while (brojač < 2) {
brojač = 0
System.out.println("Pozdrav!");
brojač++;
}
while
while

brojač < 2
услов је: true
int brojač = 0;
while (brojač < 2) {
brojač = 0
System.out.println("Pozdrav!");
brojač++;
}
while
while

исписује се у
конзоли
"Pozdrav"
int brojač = 0;
while (brojač < 2) {
brojač = 0
System.out.println("Pozdrav!");
brojač++;
}
while
while

brojač се
повећава за 1
int brojač = 0;
while (brojač < 2) {
brojač = 1
System.out.println("Pozdrav!");
brojač++;
}
while
while

brojač < 2
услов је: true
int brojač = 0;
while (brojač < 2) {
brojač = 1
System.out.println("Pozdrav!");
brojač++;
}
while
while

исписује се у
конзоли
"Pozdrav"
int brojač = 0;
while (brojač < 2) {
brojač = 1
System.out.println("Pozdrav!");
brojač++;
}
while
while

brojač се
повећава за 1
int brojač = 0;
while (brojač < 2) {
brojač = 2
System.out.println("Pozdrav!");
brojač++;
}
while
while

brojač < 2
услов је: false
int brojač = 0;
while (brojač < 2) {
brojač = 2
System.out.println("Pozdrav!");
brojač++;
}
while
while

програм наставља
да се извршава
int brojač = 0;
while (brojač < 2) {
System.out.println("Pozdrav!");
brojač++;
}
while
while

while

while

false
while
while
package rs.edu.asss.test;

public class Test{


public static void main(String[] args) {
int broj = 1;
while (broj <= 10) {
System.out.print(broj + " ");
broj++;
}
}
}
while
• while

public class Test {


public static void main(String[] args) {
int i, j;
i = 100;
j = 200;
// петља која проналази средину између i и j
while (++i < --j);
System.out.println("Sredina je: " + i);
}
}
do-while
• while


• do-while
• do-while

do{
naredba;
}
while (uslov);
do-while
do-while


true

true false
do-while
do-while

package rs.edu.asss.test;

public class Test {


public static void main(String[] args) {
int broj = 1;
do{
System.out.print(broj + " ");
broj++;
}
while (broj <= 10);
}
}
do-while

package rs.edu.asss.test; У приказаном примеру, израз


public class Test { (++broj <= 10)
public static void main(String[] args) { комбинује увећавање
int broj = 1;
do{
вредности променљиве за 1
System.out.print(broj + " "); и у истом исказу утврђивање
} да ли је та вредност мања
while (++broj <= 10);
} или једнака броју 10.
}
static Scanner ulaz = new Scanner(System.in);
static int odabir;
public static void main(String[] args) {
do {
System.out.println("==== Naša mobilna prodavnica ====");
System.out.println("------ 1. iPhone ------");
System.out.println("------ 2. Samsung ------");
System.out.println("------ 3. Motorola ------");
System.out.println("------ 4. izlaz ------");
System.out.println();
System.out.println("Izaberi sa menija jednu opciju.");
System.out.println("Unesi odgovarajući broj od 1 do 4:");
odabir = ulaz.nextInt();
switch (odabir){
case 1: System.out.println("Odabran je tel. iPhone!"); break;
case 2: System.out.println("Odabran je tel. Samsung!"); break;
case 3: System.out.println("Odabran je tel Motorola!"); break;
case 4: System.out.println("Hvala što kupujete kod nas."); break;
default: System.out.println("Odaberi broj od 1 do 4.");
}
}while (odabir != 4);
}
for
• for

• for ;



• for

for (inicijalizacija; uslov; iteracija){


naredba;
}
for
• for
(inicijalizacija; uslov; iteracija)
for
• inicijalizacija


• iteracija

• true
for
for


for

false


for
for

package rs.edu.asss.test;

public class Test {


public static void main(String[] args) {
for (int i = 1; i <= 10; i++){
System.out.print(i + " ");
}
}
}
for
• i

public static void main(String[] args) {


int i;
for (i = 1; i <= 10; i++){
System.out.print(i + " ");
}
}
for

public class Test {


public static void main(String[] args) {
int i, y;
for (i = 1, y = 10; i < y; i++, y--){
System.out.println("i = " + i);
System.out.println("y = " + y);
}
}
}
for
• for

• for

• for

• for


public class Test {


public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = i; j < 10; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

– break
– continue
– return

• break continue return

• break

• continue





You might also like