Difference between revisions of "Java/C2/For-Loop/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by one other user not shown)
Line 15: Line 15:
  
 
'''Welcome'''
 
'''Welcome'''
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''for loop'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''for loop''' i  java.
  
 
|-
 
|-
Line 39: Line 39:
  
 
'''Prerequisites'''
 
'''Prerequisites'''
| style="border:1pt solid #000000;padding:0.097cm;"| For this tutorial, you should have knowledge on '''relational operators in Java.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| For this tutorial, you should have knowledge on '''relational operators ''' and '''if statement''' in Java.
  
  
If not, please go through the tutorials on these topics available at our website [http://spoken-tuitorial.org/ http][http://spoken-tuitorial.org/ ://][http://spoken-tuitorial.org/ spoken][http://spoken-tuitorial.org/ -][http://spoken-tuitorial.org/ tutorial][http://spoken-tuitorial.org/ .][http://spoken-tuitorial.org/ org][http://spoken-tuitorial.org/ .]
+
If not, for relevant tutorial please visit  our website which is as shown [http://spoken-tuitorial.org/ http][http://spoken-tuitorial.org/ ://][http://spoken-tuitorial.org/ spoken][http://spoken-tuitorial.org/ -][http://spoken-tuitorial.org/ tutorial][http://spoken-tuitorial.org/ .][http://spoken-tuitorial.org/ org][http://spoken-tuitorial.org/ .]
  
 
|-
 
|-
Line 49: Line 49:
 
'''Syntax'''
 
'''Syntax'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here is the syntax of the '''for loop'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here is the syntax of the '''for loop'''.
 
+
It consisits of initialization, loop condition and increment.
 +
Then it has the for block which keeps on executing till the loop condition is true.
  
 
Now, let us try out an example in '''Eclipse.'''
 
Now, let us try out an example in '''Eclipse.'''
Line 65: Line 66:
  
 
'''}'''
 
'''}'''
| style="border:1pt solid #000000;padding:0.097cm;"| We have the skeleton required.
+
| style="border:1pt solid #000000;padding:0.097cm;"| So switch to '''eclipse'''
 
+
We already have a '''class''' named '''ForLoopDemo'''.
 
+
We have a '''class''' named '''ForLoopDemo'''.
+
  
  
Line 82: Line 81:
  
 
Highlight''' '''when you explain.
 
Highlight''' '''when you explain.
| style="border:1pt solid #000000;padding:0.097cm;"| Inside the main function, type '''int i '''''semicolon''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| So Inside the main function, type '''int i '''''semicolon''.
  
  
Then type '''for '''''within brackets'' '''i '''''equal to''''' 0 '''''semicolon'' '''i '''''less than '''''10''' ''semicolon''''' i '''''equal to'' '''i '''''plus '''''1'''.
+
Then '''for '''''open curly braces'' '''i '''''equal to''''' 0 '''''semicolon'' '''i '''''less than '''''10''' ''semicolon''''' i '''''equal to'' '''i '''''plus '''''1'''.
  
  
Line 92: Line 91:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight i = 0
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight i = 0
| style="border:1pt solid #000000;padding:0.097cm;"| This is the starting condition for the loop.
+
| style="border:1pt solid #000000;padding:0.097cm;"| i =0 is the starting condition for the loop.
  
  
 
This condition allows the variable to be initialized.
 
This condition allows the variable to be initialized.
 
+
-
|-
+
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight i<nowiki> < 10</nowiki>
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight i<nowiki> < 10</nowiki>
| style="border:1pt solid #000000;padding:0.176cm;"| This is the loop running condition.
+
| style="border:1pt solid #000000;padding:0.176cm;"| i<10  is the loop running condition.
  
  
Line 112: Line 110:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight i = i + 1
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight i = i + 1
| style="border:1pt solid #000000;padding:0.176cm;"| This states how the loop variable is going to change.
+
| style="border:1pt solid #000000;padding:0.176cm;"|Then i= i+1 ,  states how the loop variable is going to change.
  
  
Line 132: Line 130:
  
 
Highlight the println statement and explain.
 
Highlight the println statement and explain.
| style="border:1pt solid #000000;padding:0.097cm;"| Now, open and close curly brackets.
+
| style="border:1pt solid #000000;padding:0.097cm;"|So open and close curly brackets.
  
  
Next line type '''System''' ''dot''''' out '''''dot '''''println''' within brackets '''i''' ''into'' '''i'''.
+
Inside the curly brackets type '''System''' ''dot''''' out '''''dot '''''println''' and print  '''i''' ''into'' '''i'''.
  
  
Line 144: Line 142:
  
  
So '''save''' and '''run''' the file.
+
So '''save''' and '''run''' the program
 
+
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output
 
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output
| style="border:1pt solid #000000;padding:0.097cm;"| As we can see, the loop ran over numbers from 0 to 9.
+
| style="border:1pt solid #000000;padding:0.097cm;"| As we can see, the loop run over numbers from 0 to 9.
  
  
Line 175: Line 172:
  
 
'''}'''
 
'''}'''
| style="border:1pt solid #000000;padding:0.097cm;"| Delete '''System''' ''dot'' '''out''' ''dot'' '''println '''within brackets '''i''' ''into'' '''i'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
+
Then inside the curly brackets We print the number only if it is a multiple of 3 or 5.
 
+
We print the number only if it is a multiple of 3 or 5.
+
  
  
 
So type,
 
So type,
  
'''if '''within brackets '''i''' ''mod'' '''3''' ''double equal to'' '''0 '''or''' i '''''mod'' '''5 '''''double equal to '''''0'''.
+
'''if '''within brackets '''i''' ''mod'' '''3''' ''double equal to'' '''0 '''or''' within brackets i '''''mod'' '''5 '''''double equal to '''''0'''.
  
  
Line 189: Line 184:
  
  
Then, next line type '''System''' ''dot'' '''out '''''dot'' '''println''' within brackets '''i'''.
+
Then, in curly  bracket we print the value of i  
 
+
  
 
Now, let us see the output.
 
Now, let us see the output.
  
 +
So save and run the program
  
  
Line 199: Line 194:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Highlight the output
 
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Highlight the output
| style="border:1pt solid #000000;padding:0.097cm;"| We can see that the numbers are either multiples of 3 or of 5.
+
| style="border:1pt solid #000000;padding:0.097cm;"| We can see that the numbers are either multiples of 3 or   5. In this way we use for loop in Java
  
 
|-
 
|-
Line 214: Line 209:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 7'''Assignment'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 7'''Assignment'''
| style="border:1pt solid #000000;padding:0.097cm;"| A three digit number is called Armstrong Number if it is equal to the sum of cubes of its digits.
+
| style="border:1pt solid #000000;padding:0.097cm;"|For assignment A three digit number is called Armstrong Number if it is equal to the sum of cubes of its digits.
  
  

Latest revision as of 11:26, 9 May 2013

Title of script: for loop in Java

Author: TalentSprint

Keywords: conditions, loops, for loop, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on for loop i java.
Slide 2

Learning Outcomes

In this tutorial, you will learn how to use the for loop in Java.
Slide 3

Tools Used

For this tutorial we are using
  • Ubuntu 11.10,
  • JDK 1.6 and
  • Eclipse 3.7.0


Slide 4

Prerequisites

For this tutorial, you should have knowledge on relational operators and if statement in Java.


If not, for relevant tutorial please visit our website which is as shown http://spoken-tutorial.org.

Slide 5

Syntax

Here is the syntax of the for loop.

It consisits of initialization, loop condition and increment. Then it has the for block which keeps on executing till the loop condition is true.

Now, let us try out an example in Eclipse.

Minimize Slides and open Eclipse

Eclipse should contain the following code

public class ForLoopDemo{

public static void main(String[] args){

}

}

So switch to eclipse

We already have a class named ForLoopDemo.


Let us add the for loop inside the main method.

Inside the main function, type

int i;

for (i = 0; i < 10; i = i + 1)


Highlight when you explain.

So Inside the main function, type int i semicolon.


Then for open curly braces i equal to 0 semicolon i less than 10 semicolon i equal to i plus 1.


This statement decides how the loop is going to progress.

Highlight i = 0 i =0 is the starting condition for the loop.


This condition allows the variable to be initialized. -

Highlight i < 10 i<10 is the loop running condition.


If the condition is true then the for block will be executed.


Else it will be ignored.


That means when i becomes more than or equal to 10, the block is not executed.

Highlight i = i + 1 Then i= i+1 , states how the loop variable is going to change.


Here, the value of i starts with 0.


It keeps increasing by 1 for every iteration of the loop until it becomes 10.


Now let us do something with i.

Change for (i = 0; i < 10; i = i + 1) to for (i = 0; i < 10; i = i + 1) {

System.out.println( i * i );

}


Highlight the println statement and explain.

So open and close curly brackets.


Inside the curly brackets type System dot out dot println and print  i into i.


This will print the square of each number from 0 to 9.


Let us see the output.


So save and run the program

Save and run. Point to output As we can see, the loop run over numbers from 0 to 9.


The square of the number has been printed in each iteration.


Now let us print all the 2 digit numbers that are multiples of 3 or 5.

Change i = 0 to i = 10 So, i should have values from 10 to 99.


So change i equal to 0 to i equal to 10.

Change i < 10 to i < 100 Change i less than 10 to i less than 100.
Change System.out.println( i * i ) to

if ((i % 3 == 0) || (i % 5 == 0)){

System.out.println(i);

}

Then inside the curly brackets We print the number only if it is a multiple of 3 or 5.


So type,

if within brackets i mod 3 double equal to 0 or within brackets i mod 5 double equal to 0.


This statement checks whether i is divisible by 3 or by 5.


Then, in curly bracket we print the value of i

Now, let us see the output.

So save and run the program


Save and run. Highlight the output We can see that the numbers are either multiples of 3 or 5. In this way we use for loop in Java
Minimize the Eclipse window and switch to slides.


Slide 6

Summary

We have come to the end of this tutorial.

In this tutorial we have learnt how to use for loop in java.

Slide 7Assignment For assignment A three digit number is called Armstrong Number if it is equal to the sum of cubes of its digits.


For example, 153 is equal to 1 cube plus 5 cube plus 3 cube.


Find all such 3 digit numbers.

Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.Alternatively, you can download and watch it.
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
Slide 10Acknowledgement
  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
Slide 11About the contributor
  • This script has been contributed by TalentSprint
  • www.talentsprint.com
  • Thanks for joining


This script has been contributed by TalentSprint.

This is Prathamesh Salunke signing off.

Thanks for joining.



Contributors and Content Editors

Arya Ratish, Pratham920, Sneha