A for loop.
I don't specifically know Ruby on Rails, but usually (in most programming languages), to do this task you would define one variable for the sum, and one for a counter. You initialize the sum to zero, and then, in a loop, you (1) increment the counter (most languages specifically have a "for" loop for this), and (2) add the counter to the sum. Here is the pseudocode; it is NOT for a specific programming language, but it gives you the general idea; you should be able to adapt it to most programming languages: sum = 0 for addend = 1 to 10 { sum = sum + addend } output "Sum = ", sum Note that you may also need to initialize the variables, depending on the language.
For
If it isn't, then you don't know for sure what value it will start at in some languages. Thus, your count will be wildly inaccurate. In other languages, it will just generate an error if you forget to initialize. Two steps: 1. It is critical that variables be properly initialized. 2. Counter-variables are variables.
// set up reader BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); // number of line to read int numberOfLines = 3; // initialize the counter for number of characters int numberOfChars = 0; // prompt user to enter the lines of text System.out.format("Enter %d lines of text:\n", numberOfLines); for (int i = 0; i < numberOfLines; i++) { String line = reader.readLine(); // increment the counter with the number of characters entered in current line numberOfChars += line.length(); } // print the number of characters entered System.out.println("Number of characters entered: " + numberOfChars);
MVI B, 09 : Initialize counter START : LXI H, 2200H: Initialize memory pointer MVI C, 09H : Initialize counter 2 BACK: MOV A, M : Get the number INX H : Increment memory pointer CMP M : Compare number with next number JC SKIP : If less, don't interchange JZ SKIP : If equal, don't interchange MOV D, M MOV M, A DCX H MOV M, D INX H : Interchange two numbers SKIP:DCR C : Decrement counter 2 JNZ BACK : If not zero, repeat DCR B : Decrement counter 1 JNZ START HLT : Terminate program execution
for loop
Test initialize increment
Test initialize increment
LXI H, 6000H : Initialize memory pointer MVI C, 00H : Initialize number counter MVI B, 00H : Initialize negative number counter MVI E, 00H : Initialize zero number counter BEGIN:MOV A, M : Get the number CPI 00H : If number = 0 JZ ZERONUM : Goto zeronum ANI 80H : If MSB of number = 1i.e. if JNZ NEGNUM number is negative goto NEGNUM INR D : otherwise increment positive number counter JMP LAST ZERONUM:INR E : Increment zero number counter JMP LAST NEGNUM:INR B : Increment negative number counter LAST:INX H : Increment memory pointer INR C : Increment number counter MOV A, C CPI 32H : If number counter = 5010 then JNZ BEGIN : Store otherwise check next number LXI H, 7000 : Initialize memory pointer. MOV M, B : Store negative number. INX H MOV M, E : Store zero number. INX H MOV M, D : Store positive number. HLT : Terminate execution
I don't specifically know Ruby on Rails, but usually (in most programming languages), to do this task you would define one variable for the sum, and one for a counter. You initialize the sum to zero, and then, in a loop, you (1) increment the counter (most languages specifically have a "for" loop for this), and (2) add the counter to the sum. Here is the pseudocode; it is NOT for a specific programming language, but it gives you the general idea; you should be able to adapt it to most programming languages: sum = 0 for addend = 1 to 10 { sum = sum + addend } output "Sum = ", sum Note that you may also need to initialize the variables, depending on the language.
For
A counter variable is "incremented" (the step number, 1 in this case, is added to it) in any of the following four ways: $counter = $counter + 1;$counter += 1; //this is shorthand for the above $counter++; //postfix increment operator $counter = 0;echo $counter++;The output would be 0++$counter; //prefix increment operator $counter = 0; echo ++$counter;The output is 1
Bleaching is the most common hiding trick. There are bleaches specifically designed for the face, available at the cosmetics counter of your local grocery.
For
A) Subtract 6 from 48. If the result less than 6 or 0 you made a mistake. Result is 42. Hence start a counter with a value of 1. B) Subtract 6 from 42. If the result less than 6 or 0 you made a mistake. Result is 36. Hence increment the counter by 1. Now counter value is 2. C) Subtract 6 from 36. If the result less than 6 or 0 you made a mistake. Result is 30. Hence increment the counter by 1. Now counter value is 3. D) Subtract 6 from 30. If the result less than 6 or 0 you made a mistake. Result is 24. Hence increment the counter by 1. Now counter value is 4. E) Subtract 6 from 24. If the result less than 6 or 0 you made a mistake. Result is 18. Hence increment the counter by 1. Now counter value is 5. F) Subtract 6 from 18. If the result less than 6 or 0 you made a mistake. Result is 12. Hence increment the counter by 1. Now counter value is 6. G) Subtract 6 from 12. If the result less than 6 or 0 you made a mistake. Result is 6. Hence increment the counter by 1. Now counter value is 7. H) Subtract 6 from 6. If the result is not 0 you made a mistake. Result is 0. Hence increment the counter by 1. Now counter value is 8. Since we have nothing more to subtract from stop the process. The last value of the counter was 8 and so the quotient is 8.
public static void main(String args){ int counter = 0; //initialize the counter variable to 0 while (counter < 10){ //while the counter variable is less than 10... counter ++; //increase the counter variable by 1 System.out.println(counter); //print the counter variable } }
int countVowels(const char* str) { int i = 0; int numVowels = 0; while(str[i] != '\0') { switch(str[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': ++numVowels; } ++i; } return numVowels; } static int countVowels(String str) { int numVowels = 0; foreach(char ch in str) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': ++numVowels; break; } } return numVowels; }