In any programming language there are three things we need to learn at the earliest.
1. Types of variable
2. Selection Statements
3. Iteration or Loops
Since ours is a “get ready quick” course. We will only try what we think is essential.
1. Variable.
There are no types of variables. What ever you assign the variable takes that values. Unlike in other programming languages you have int, char, fload, date etc. There is nothing here. You can just put any value in any variable. The upside is variable usage is easy. Same variables can be recycled. The downside is explicit conversions are needed everytime a variable of a specific type is needed.
Assume you have a program with the following
i=5 |
When you run it there is no error.
2. Selection statements.
Keeping it simple.
if <condn> then
<statements>
else
<statements>
randomize |
randomize – resets the seed, so that you get a real random numbers all the time. rnd generates a random number between 0 and 0.999xxx. The int function converts the output to an integer (we don’t need the decimals). The mod is a operation to find the remainder.
The above program prints either even or odd depending on the random number. In the example the program is saved as third.vbs
C:\scripts>cscript third.vbs 7 C:\scripts> C:\scripts>cscript third.vbs 1 C:\scripts>third 0 C:\scripts>cscript third.vbs 6 C:\scripts> |
3. Iteration or Loops
The most convenient loop is the for – next.
An example
wscript.echo "Program 2" |
A simple program the prints, from 1 to 5. And then random numbers between 0 and 9.
Program 2 |
Try this exercise.
Print the first 100 prime numbers.
use the concepts learnt.
No comments:
Post a Comment