Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Output of (a++ * a++) , Number patterns using nested loops

Notes:

I. Output of (a++ * a++), (a++*a++*a++), (++a * a++): [Starts at: 00min:00sec]
II. Given number is prime or not: [Starts at: 13min:48sec]
III. Linear, quadratic, cubic loops: [Starts at: 17min:28sec]
IV. Dependent quadratic loops & number patterns: [Starts at: 22min:17sec]

I. Output of (a++ * a++), (a++*a++*a++), (++a * a++): [Starts at: 00min:00sec]

Example Code:
using UnityEngine;
public class LoopingStatements : MonoBehaviour
{
void Start ()
{
/*
int a = 10;
Debug.Log (a++ * a++); // 110
Debug.Log (a); // 12
*/
/*
int a = 10;
Debug.Log (a++ * a++ * a++); // 1320
Debug.Log (a); 13
*/
int a = 10;
Debug.Log (++a * a++); // 121
Debug.Log (a); // 12
}
}

II. Given number is prime or not: [Starts at: 13min:48sec]
Example Code:
using UnityEngine;
public class LoopingStatements : MonoBehaviour
{
void Start ()
{
int num = 10;
bool isPrime = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
Debug.Log (i + " divides " + num + " hence");
break;
}
}
if (isPrime == true)
Debug.Log (num + " is prime");
else
Debug.Log (num + " is not prime");
}
}

III. Linear, quadratic, cubic loops: [Starts at: 17min:28sec]
Example Code:
using UnityEngine;
public class LoopingStatements : MonoBehaviour
{
void Start ()
{
int m = 3, n = 3, o = 3;
/*
// Linear
for (int i = 0; i < n; i++) {
Debug.Log ("Hi"); // executes n times
}
// Quadratic
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
Debug.Log ("Hi"); // executes m x n times
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Debug.Log ("Hi"); // executes n x n times
}
}
// Cubic
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
Debug.Log ("Hi"); // executes n x n x n times
}
}
}
*/
// Ex: Number series
/*
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
Debug.Log (i); // 000 111 222
}
}
*/
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
Debug.Log (j); //012 012 012
}
}
}
}

IV. Dependent quadratic loops: [Starts at: 22min:17sec]
Example Code:
using UnityEngine;
public class LoopingStatements : MonoBehaviour {
void Start () {
// dependent quadratic loop
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
Debug.Log (i);
}
}
// output 1 22 333
// dependent quadratic loop
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
Debug.Log (j);
}
}
// output 1 12 123
}
}