Website Development Prices

Search Blog

Friday, December 11, 2015

Upotreba iskaza switch (Using statement switch)

Ako zelite da proverite vise uslova mozete upotrebiti iskaz switch.

Iskaz switrch pocinje kljucnom recju switch. Stavka koja se proverava stavlja izmedju zagrada. Provere se vrse pomocu iskaza case, pri cemu za svaki iskaz zadajete vrednost. Ako vrednost u switch iskazu odgovara vrednosti u case iskazu, izvrsavaju se interni iskazi u iskazu case. Izvrsavanje se prekida kad se naidje na iskaz break, koji zavrsava iskaz case. Kada ne postoji nijedan case koji odgovara zadatoj vrednosti, izvrsava se klauzula default, ako postoji. 

Primer:

<?php

$cenamotora = 2500;
switch ($cenamotora) {
case '2500':
echo "Cena ovog motora je jeftina.";
break;
case '3000':
echo "Cena motora je pristupacna.";
break;
case '3500':
echo "Cena motora je jos uvek pristupacna.";
break;

default:
echo "Cena je izvan mojih mogucnosti.";
break;
}


?>


Rezultat: Cena ovog motora je jeftina.

Napomena: imajte u vidu da vrednosti koje zadajete u iskazu case moraju biti celobrojne vrednosti, realni brojevi ili stringovi.

Ako sa jednim iskazom case zelite da uparite vise vrednosti, izostavite iskaz break u pojedinim case iskazima. To znaci da ce se izvrsiti i naredni iskaz case.

Primer 2: ako zelite da imate raspon cena od 2500-2700, 2700-2900, itd mozete da napravite sledeci skript.

$cenamotora = 2900;
switch ($cenamotora) {
case '2500':
case '2600':
case '2700':
echo "Cena ovog motora je jeftina.";
break;
case '2800':
case '2900':
case '3000':
echo "Cena motora je pristupacna.";
break;
case '3100':
case '3200':
case '3300':
echo "Cena motora je jos uvek pristupacna.";
break;

default:
echo "Cena je izvan mojih mogucnosti.";
break;

}

Rezultat: Cena motora je pristupacna.

If you want to check more conditions, you can use the switch statement.

The statement begins with a key word switch. The item that is checked puts between parentheses. Checks are made using case statement, with that for each statement you set the value. If the value of the switch statement matches the value in the case statement, the internal statements are executed in the case statement. Execution stops when the break comes along in a statement, which ends statement case. When there is no case that matches the given value, the default clause executes, if any.

Example:

<?php

$priceofbike = 2500;
switch ($priceofbike) {
case '2500':
echo "The price of this motobike is cheap.";
break;
case '3000':
echo "The price of motobike is affordable.";
break;
case '3500':
echo "The price of motobike is still affordable.";
break;

default:
echo "The price of motobike is expensive.";
break;

}

?>

The result: The price of this motobike is cheap.

Note: Keep in mind that the values ​​that you specify in the case statement must be integers, real numbers or strings.

If with one case statement you want to pair more values, omit the break statement in some case statements. This means that the next case statement will be executed.

Example 2: If you want to have a price range of 2500-2700, 2700-2900, and so on, you can make the following script.

<?php

$priceofbike = 2900;
switch ($priceofbike) {
case '2500':
case '2600':
case '2700':
echo "The price of this motobike is cheap.";
break;
case '2800':
case '2900':
case '3000':
echo "The price of motobike is affordable.";
break;
case '3100':
case '3200':
case '3300':
echo "The price of motobike is still affordable.";
break;

default:
echo "The price of motobike is expensive.";
break;
}


?>

The result: The price of motobike is affordable.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.