Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

For loops

You may have noticed in the previous tutorial what in the loop I used an integer incremented it, and exited the loop once it reached a certain target number, this ensured the loop ran 'x' times, and is comon practice infact so comon practice another loop was created specificaly for this, named the for loop.
I will write it in BASIC first since put simply basic reads easier

for i = 1 to 10 step 1
'do some code
next i
This code will work in Vbasic not in Java and basicly says:
while i starts at 1 (for i=1) and is less then 10 (to 10) add 1 to i (step 1).
Since you are not here to learn basic the same thing in c
Code:
for(int i=1; i<=10; i++){

//do some code
}
You may recall I said all lines of code in c end with a semi-colon ';' this is still true, the for statement expects 3 lines of code in the brackets:
1. A declaration, where a variable usually an integer is declared and its initial value given (int i=1)
2. A check (if statement if you will) to see if the loop should still run (i<=10)
3. An incrementer, something want can change the variable so that at somepoint it will not equal line '2.' any more. (i++)
I choose my words carefully here because unlike BASIC the for loop isn't limited in anyway.
A String for loop
Code:
// to prove c is more flexible then basic... try running this!
for(String test=""; test.compareTo("+++++")!=0;test=test.concat("+")){

System.out.println(test);
}
// yeap so useful, so flexible this is the first time I have EVER written a string for loop and I haven't came accross any, but atleast I had the pleasure of knowing I COULD WITH C :-)
Slightly more useful
Code:
// ok remembering you are probably a beginner and want things you can use!
// to ensure you know for loops almost always use integers! you may want to include a bailout, and you may want to add 2 at a time or subtract one all is ofcourse possible!
boolean bail=false;
for(int i=16; (i > 0 && bail==false); i-=2){

if(i == 4) bail=true;
System.out.println(""+i);
}
What number is printed last?
[click for answer]

Excersize

Recalling tutorial c_1_4 introduction to variables I told you to create timetables1 and showed you a long way how to print the 5 times table upto 5 the long way, now to shorten it!
Create a new project named 'timestables2' use this code:
Code:
public class TimesTable2 {


public static void main(String[] args) {

/* Times Tables Excersize */
int i=0; // 'I'ncrementer
int intTable=5;
int intAnswer;

System.out.println("Times table "+intTable+" up to 5.");
// 1
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 2
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 3
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 4
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 5
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
}

}
Can you shorten this code using a for loop?
[click for answer]
Keep this code it will be used again with the end of section excersizes!

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source