answersLogoWhite

0

Why we use TO keyword in for loop?

Updated: 11/3/2022
User Avatar

Wiki User

9y ago

Best Answer

Not all languages support a TO keyword. It is commonly found in languages such as BASIC and is used to define a closed range of values. In the context of a FOR loop, it is used to define the closed range of values for the loop's control variable.

For example:

FOR x=1 TO 10

PRINT x

NEXT x

Here, the FOR statement defines a control variable named x in the closed range [1:10]. Note that a closed range is an inclusive range, thus the body of this loop will execute for all values x where 1<=x<=10. Initially, x is assigned the lower bound of the range, 1.

At the start of each iteration, x must be evaluated to ensure that it is in the valid range of values. If it is in the range, execution passes to the first statement in the loop, where we print the value of x. When we reach the NEXT x statement, x is incremented (adding 1 to the value of x) and control passes to the start of loop where x is re-evaluated.

In this case, our loop will print the values 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.

When x is 10, the NEXT x statement will increment x to 11 and pass control back to the start of the loop where x is re-evaluated. At this point x is no longer in the valid range of values so execution passes to the statement immediately following the NEXT x statement, thus terminating the loop.

A FOR statement can also include a STEP value, which determines the amount we increment the control variable at the end of each iteration. The default STEP value is 1. However, if we wish our loop to count backwards, we must provide a STEP value of -1:

FOR x=10 TO 1 STEP -1

PRINT x

NEXT x

Output: 10, 9, 8, 7, 6, 5, 4, 3, 2 and 1.

We can also STEP by values other than 1 and -1.

FOR x=1 TO 10 STEP 2

PRINT x

NEXT x

Output: 1, 3, 5, 7 and 9.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why we use TO keyword in for loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Who can used to exit from a loop?

If you meant 'what can be used' then it is statement break.


What keyword is used to jump out of a loop without waiting to get back to the conditional test?

break


What are the differencess between while and do while repetition structures?

If a loop begins with "while" then the test is made at the top of the loop, and the block is executed only when the expression (in parentheses) evaluates to true; if the loop begins with "do" then the block is executed at least once, and the "while" test is made at the bottom of the loop, to determine whether it is executed again. In C/C++ the test at the end is a "while" keyword. However, in some other languages, the test at the end may use an "until" keyword, so that the block is repeated only when the condition is false and the loop exits when it is true.


What is the use of keyword new used in AWT programming?

what is the use of new keyword in awt programming


For loop -uses?

You can use a for loop whenever you can use a while loop; it's the same.


How do you terminate in C language a loop?

The keyword "break" will immediately terminate the enclosing loop. Otherwise using conditions in the loop will terminate the loop once the condition becomes false. eg int i = 0; int b =0; for(i ; i &lt; 3; i ++){ b++; } Will increment i and b on each iteration. The loop will terminate when i &gt;= 3 (when the condition i &lt; 3 becomes false).


Which keyword do you use to deallocate memory that has been assigned to a stream object?

Delete keyword


Why you use if loop in telecoms?

No such thing as if-loop. if-else statement is not a loop.


Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i&lt;=n;)


Can you declears static in a local variable in a method in java?

we cannot use the staic keyword inside the method... But we can use the final keyword inside the method....


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


What are the 6 ways to use this keyword?

"Java This keyword" is a reference to the current object, it is very helpful when you need to refer an instance of a particular Object from its available methods or using it's constructor, also "this" keyword helps us to avoid naming conflicts.The following are different ways to use java this keyword1) Using with instance variable2) Using with Constructor3) Pass / Return current instanceUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with ConstructorUsing this keyword inside constructor like followingthis("Sony", 20); it will call the constructor having same parameter