How to increment variable value by 5 iterations with multiple conditions

increment two variables in for loop java
how to increment two values in for loop
multiple conditions in for loop java
for loop with multiple conditions javascript
for loop with multiple conditions python
multiple conditions in for loop c
for loop multiple conditions c
for loop with two variables c++

I am trying to put some pieces of code into the loop. What I want to achieve is add the tax to total by every 5 product quantity added.

if($items['qty'] >= 5 && $items['qty'] < 10){
    $tax += $items[$type] * 1;
}

if($items['qty'] >= 10 && $items['qty'] < 15){
    $tax += $items[$type] * 2;
}

if($items['qty'] >= 15 && $items['qty'] < 20){
    $tax += $items[$type] * 3;
}
if($items['qty'] >= 20 && $items['qty'] < 25){
    $tax += $items[$type] * 4;
}

I tried this code, it works with a static value. It would be great if someone helps me to put this code into the loop.

Example: tax = $10. For product quantity 1 to 4 the tax will be $10. For product quantity 5 to 9 the tax will be $20, for product quantity 9 to 14 the tax will be $30 so on...

$items['data'] = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

$tax = 10;

$j = 5;

$k = 1; for($i=0;$i < $j;$i++){

echo $items['data'][$i];

} $j = $j+5;

echo "".$tax*$k ."";

$k= $k+1;

for($i=5;$i < $j;$i++){

echo $items['data'][$i];

} $j = $j+5;

echo "".$tax*$k ."";

$k= $k+1;

for($i=10;$i < $j; $i++){

echo $items['data'][$i];

}

$j = $j+5;

echo "".$tax*$k ."";

$k= $k+1;

What do multiple increments mean in a for loop?, <initialization> = p = prevp->s.ptr - initializing p to some pointer value <condition> It is updating two variables here prevp and p , such that in the next iteration they both will have the new values. The coma Up vote 5 Down vote. In this case, on every iteration of the loop, both statements are called. In Bash, there are multiple ways to increment/decrement a variable. This article explains some of them. Using + and -Operators # The most simple way to increment/decrement a variable is by using the + and -operators.

This should help in calculating the factor. What is $type here?

$items['qty'] = 24;
$tax = 10;
$floorFactor = floor($items['qty'] / 5);

$factor = $floorFactor === 0 ? 1 : $floorFactor;

echo '$floorFactor is ' . floor($items['qty'] / 5);

$tax += $items['qty'] * $factor;

echo "<br > tax is $tax <br />";

6. Iteration — How to Think Like a Computer Scientist: Learning with , In Python, an assignment statement can make two variables equal, but they don't have to stay that way: a = 5 b = a # a and b are now equal a = 3 # a and b are no longer equal For some particular values of n, we can prove termination. The variable count is initialized to 0 and then incremented each time the loop body is � Increment and Decrement Operators in Python; Python | Set 2 (Variables, Expressions, Conditions and Functions) Python | Set 3 (Strings, Lists, Tuples, Iterations) Project Idea | (A Game of Anagrams ) Print Single and Multiple variable in Python; Swap two variables in one line in C/C++, Python, PHP and Java; Java is Strictly Pass by Value!

There are many ways to do it and you might not need a loop (3 line of code might be enought), but you want a loop you might try something like this. It's a loop as you asked:

$j increment 1 by one in numerical order, so each time it will be a multiple of 5 it will increase the multiplicator by 10 (your tax) up to the value in $items['qty'].

$multiplicator is equal to 10 at the start then it will increase by 10 in the the loop for each 5 items, for the maintenance if you change the tax value you only change this one.

($j % 5) === 0) is to check if $j is a multiple of 5, same here for the maintenance you will need to change the multiple if you decide you want more or less items to change the tax.

    $multiplicator = 10; 
             for($j = 1; $j <= $items['qty']; $j++){ 
                     if(($j % 5) === 0){ 
                         $multiplicator += 10;
                         }
                     //echo $j."-".$multiplicator."<br>"; // to check how it works
                     }
//$tax += $items[$type] + $multiplicator; // I would finish like that but i don't know what is $type
// so to match your multiplication number in $tax += $items[$type] * x you need the code below as well
//(($multiplicator/10)-1) is to match your multiplication numbers in your code as $tax += $items[$type] * x;
        if((($multiplicator/10)-1) == 0){ 
            $tax += $items[$type]; // less than 5 items
            }
        else{
            $tax += $items[$type] * (($multiplicator/10)-1); // 5 items and above 
            }

For loop multiple conditions - Get Started, I want to check two conditions in one 'for' loop. lname and rname are soon as j equals to 5 so you wont be able iterate all variables in lname. Maximum Iterations determines how many times Excel is to recalculate the workbook, Maximum Change determines the maximum difference between values of iterative formulas. Note that entering a smaller number here means more accurate results. Iterative calculations stop when one of the conditions defined (iterations count or change value) are matched.

Finally I achieved this using array functions and foreach loop. This may be a long code but i could not achieve it quickly.

            if ($items['qty'] >= 5){
                  $tax += $items[$type];   // $items[$type] is a tax that is being fetched from the product table
                  // Calculation Start
                  $keys = range(1, 25);
                  $num = range($items['qty'], $items['qty'] + 5);
                  $a = array_fill_keys($keys, $num);

                  foreach ($a as $keys => $endnum) {
                        if ($endnum[$keys] % 5 == 0) {
                            if($items['qty'] >= $items['qty'] && $items['qty'] < $endnum[$keys]){
                                $range = range(5, 10000, 5);
                                $count = array_search($endnum[$keys], $range);
                                $tax += $items[$type] * $count; 
                            }
                            break;
                        }
                  }
                  // Calculation Ends
            }else{
                $tax += $items[$type];
            }

Lesson 18: Generating Data With Do Loops, DO, index-variable, start, TO, stop, and END are required in every iterative DO DATA multiply (drop = i); multiple = 0; do i = 1 to 20; multiple + 5; output; end; RUN; Rather than specifying start, stop and increment values in a DO statement,� If the conditions are met (using a Condition) the Count variable would increment until it match the value "How many will be assigned". I am using a Do Until flow to accomplish this. It will not go past 2 items (the original and the new item) even though there the "How many will be assigned" value is 4. I cannot figure out that I am doing wrong.

Loops and iteration, for (let step = 0; step < 5; step++) { // Runs 5 times, with values of step 0 through 4. of selected options in a scrolling list (a <select> element that allows multiple selections). The for statement declares the variable i and initializes it to 0 . With each iteration, the loop increments n and adds that value to x . For loop is basic feature we use in programming. In it we use a variable and keep on increasing or decreasing it till a condition is matched. But many times a scenario comes where we want to increment or decrement two variables instead of one. For example, I want to do for loop with 2 variable i.e. i that will increment from 0 to 9

Loops: while and for, Loops are a way to repeat the same code multiple times. Any expression or variable can be a loop condition, not just i > 5 ) ? alert ( i ) : continue ; // continue isn't allowed here For every loop iteration, write down which value it outputs and then So it increments i to 5 , but returns the old value. Hence� For example, I want to display a variable called SNR and have it automatically increment its value corresponding to its iterations. The legend would look something like this: -- SNR = 0

JavaScript Loops Explained: For Loop, While Loop, Dowhile Loop , A loop will continue running until the defined condition returns false . Usually used to increment a counter. For loops are commonly used to count a certain number of iterations to repeat a statement. There are two ways to fix this code. for (var i = 0; i < 9; i++) { console.log(i); } output: 0 1 2 3 4 5 6 7 8. In the following example, we will check whether the variable value is odd or even. If the variable value is odd, the code enters the IF…ELSE statement blocks and increment the value of the variable, execute the CONTINUE statement and starts a new iteration:

Comments
  • I am sorry, i forgot to mention what is $items[$type] here. So let me tell you $items[$type] is nothing but the tax itself which is coming from the product table. Means $items[$type] = 10;
  • Please check the formatting of your code. Also may be worth adding some description of your solution.