Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

forward, reverse for loops, Sum of n, even, odd numbers

Notes:

I. C# forward for loop: [Starts at: 01min:20sec]
II. C# reverse for loop: [Starts at: 06min:31sec]
III. Displaying even numbers: [Starts at: 15min:11:sec]
IV. Displaying odd numbers: [Starts at: 17min:15sec]
V. Sum of n numbers: [Starts at: 19min:30sec]
VI. Sum of even and odd numbers: [Starts at: 23min:20sec]

I. C# forward for loop: [Starts at: 01min:20sec]
Displaying numbers in ascending order

Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
/* forward for loop
for (int i = 0; i < max; i++) {
Debug.Log ("i="+ i);
}
*/

for (int i = 1; i < 11; i++)
{
Debug.Log ("i="+ i);
}
}
}

II. C# reverse for loop: [Starts at: 06min:31sec]
Displaying numbers in descending order

Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
/* reverse forward for loop
for (int i = max - 1; i >= 0; i--) {
}
*/

for (int i = 11 - 1; i >= 1; i--) {
Debug.Log ("i=" + i);
}

}
}

III. Displaying even numbers: [Starts at: 15min:11:sec]
Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
/*
for (int i = 0; i < 11; i=i+2) {
Debug.Log (i);
}
*/

// OR
for (int i = 0; i < 11; i++) {
if (i % 2 == 0)
Debug.Log (i);
}

}
}

IV. Displaying odd numbers: [Starts at: 17min:15sec]

Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
for (int i = 0; i < 11; i++) {
if (i % 2 != 0)
Debug.Log (i);
}
}
}

V. Sum of n numbers: [Starts at: 19min:30sec]

Example Code:
using UnityEngine;
public class LoopingStatements : MonoBehaviour {
void Start () {
/*
// Sum of n numbers
int sum=0;
for (int i = 0; i <=10; i++) {
sum = sum + i;
}
Debug.Log (sum); // 55
*/
}
}

VI. Sum of odd numbers: [Starts at: 23min:20sec]

Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
// Sum of even numbers
int sum=0;
for (int i = 0; i <=10; i++) {
if(i%2==0)
sum = sum + i;
}
Debug.Log (sum); // 30

/*
// Sum of odd numbers
int sum=0;
for (int i = 0; i <=10; i++) {
if(i%2!=0)
sum = sum + i;
}
Debug.Log (sum); // 25
*/
}
}