Website Development Prices

Website Development Prices

Wednesday, May 13, 2015

Povecanje i smanjenje promenljive za 1 (Incrementing and decrementing the variable to 1)

Operator povecanja (en. increment) je (++).
Operator smanjenja (en. decrement) je (--).

Primer:

Ako je vrednost $motori 0, onda se pomocu operatora ++ ($motori ++) dobija vrednost promenljive $motori od 1. 
Ako je vrednost $motori 10, onda se pomocu operatora -- ($motori --) dobija vrednost promenljive $motori od 9. 

Upotreba operatora ++ i -- pre i posle imena promenljive.

++$motori se naziva prefiks operator.
$motori++ se naziva postfiks operator.

Ako koristite ++$motori vrednost promenljive $motori se povecava, a onda se ta povecana vrednost koristi u ostalom delu iskaza.  


Ako koristite $motori++ u iskazu se prvo upotrebi originalna vrednost promenljive $motori, pa se ta vrednost poveca za 1.

++$aqua     preinkrement - povecava se vrednost promenljive $aqua za 1, a onda se vraca vrednost $aqua.

$aqua++     postinkrement - vraca se $aqua, a onda se promenljiva $aqua povecava za 1.

--$aqua     predekrement - smanjuje se vrednost promenljive $aqua za 1, a onda se vraca vrednost $aqua.

$aqua--     postdekrement - vraca se $aqua, a onda se promenljiva $aqua smanjuje za 1.

Primer:

<?php
$motori = $kaciga = $rukavice = $jakna = 1;

echo "\$motori++ daje ", $motori++, "<br>"; // 
R: $motori++ daje 1

 echo "++\$kaciga daje ", ++$kaciga, "<br>"; 
// R: ++$kaciga daje 2

 echo "\$rukavice-- daje ", $rukavice--, "<br>"; 
// R: $rukavice-- daje 1

 echo "--\$jakna daje ", --$jakna, "<br>"; 
// R: --$jakna daje 0


?>

Ovaj kod sacuvajte kao povecanje-smanjenje.php.

Koristite ++ i -- kao prefiks operatore ako samo zelite da vrednost promenljive povecate ili smanjite za 1 a ne i da brinete o tome kada je ta vrednost promenjena.


Incrementing operator is (++).
Decrementing operator is (-).

Example:

If the value of $motobikes is 0, then using operator ++ ($motobikes++) gets the value of $motobikes  is 1.
If the value of $motobikes is 10, then using operator -- ($motobikes--) gets the value of $motobikes is 9.

Using operators ++ and - before and after the variable name.

++$motobikes is called a prefix operator.
$motobikes++ is called postfix operator.

If you use ++$motobikes, value of $motobikes increases, then the increased value is used in the rest of the statement.

If you use $motobikes++, in the statement first is used the original value of $motobikes, and then that value increases for 1.

++$aqua preincrement - increases the value of $aqua for 1, and then returns the value of $aqua.

$aqua++ postincrement - returns $aqua, and then the variable $aqua increases for 1.

--$aqua predecrement - reduces the value of $aqua for 1, and then returns the value of $aqua.

$aqua-- postdecrement - returns $aqua, and then the variable $aqua decreases for 1.


Example:

<?php
$motobikes = $helmet = $gloves = $jacket = 1;

echo "\$motobikes++ daje ", $motobikes++, "<br>"; 
// R: $motobikes++ daje 1

echo "++\$helmet daje ", ++$helmet, "<br>"; 
// R: ++$helmet daje 2

echo "\$gloves-- daje ", $gloves--, "<br>"; 
// R: $gloves-- daje 1

echo "--\$jacket daje ", --$jacket, "<br>"; // 
R: --$jacket daje 0

?>

This code save as an increment-decrement.php.


Use the ++ and - as a prefix operators if you just want to increase or decrease  value of variables to 1 and not to worry about that when the value was changed.

No comments:

Post a Comment

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