Logic To Print Natural Numbers from 1 to N using for loop
We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now for loop keeps executing until value of count is less than or equal to user entered value. Inside for loop we printout the value of count and then increment the value of count by one for each iteration of loop.
This way we printout all the natural numbers from 1 to N.
Today lets learn about another loop control statement i.e., for loop. For loop is used to repeatedly execute certain set of instructions.
For loop allows us to specify 3 things in a single line: 1. Loop count initialization. 2. Condition Checking. 3. Modification Statement(increment/decrement statements).
In this video tutorial we shall learn some basics of a page component.
Topics Covered 1. Decorators – little bit 2. Class 3. Template File – ionic list items, For loop in ionic template. 4. Variables in Ionic – data type(number, string, Array, any) 5. Importing libraries and using it in our project. 6. Setting home page of our project.
import { StatusBar, Splashscreen } from 'ionic-native';
import { HomePage } from '../pages/home/home';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
exportclass MyApp {
rootPage = HomePage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
});
}
}
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { HomePage } from '../pages/home/home';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage = HomePage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
});
}
}
Important: Do NOT forget to import all your classes in app.module.ts file, also specify its class name inside declarations and entryComponents section.
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController) {
}
}
Here we have imported two important library files. Component is used to configure the decorator and NavController is used for navigation purposes. In this tutorial we’re not using Navigation, but we’ll make use of it in our next video tutorial.
Decorators There are 3 types on decorators: 1. Component 2. Pipe 3. Directive
I’ll explain each one of them separately in other videos.
Component In today’s tutorial we’ve a component and it has a selector and a templateUrl. Selector name is used to inject an element or an attribute into the DOM. TemplateUrl tells the class about its associated template file. Components(Decorative) are placed just above the class definition.
Class A class is a blueprint of an object. This is where we write our logic. It’ll usually have some properties(public or private or protected) and some methods.
In Ionic 2 class, you might have seen export keyword – which means we can import this file in some other class file and make use of its service or output.
variables In TypeScript variables are typed – which means, variables have data type. var1: any; Means variable var1 can take value of any data type. var1: string; Here var1 can be assigned with string value only. var1: number; as you might have already guessed, var1 takes number value. var1: Array< {}>; var1 takes an array as its value. var1: boolean; var1 takes value true or false.
Wrong initialization var1: number = ‘Satish’; var1: string = 1;
Correct initialization var1: number = 1; var1: string = ‘Satish’;
Here we have an array called companies – which has a list of company names like Microsoft, Apple, Google, Oracle, IBM. Each of this is stored as an individual element inside the array.
Using ngFor loop we can iterate through this array and display the company names.
In Ionic 2, we make use of *ngFor for for loop. We declare a new variable company and reference individual elements of the array present inside companies array.
Lets learn to create index and to optimize the database in MongoDB.
Creating “Database”: “temp”, “Collection”: “no”, and inserting 10 Million documents inside it
1
2
3
4
5
use temp
switched to db temp
for(i=0; i< = 10000000; i++)
db.no.insert({"student_id": i, "name": "Satish"});
use temp
switched to db temp
for(i=0; i< = 10000000; i++)
db.no.insert({"student_id": i, "name": "Satish"});
Since Mongo Shell is built out of JavaScript, you can pass in any valid Javascript code to it. So we write a for loop and insert 10 Million documents inside “no” collection.
“no” collection has 10 Million record, but it won’t fetch you all records at once, as it would take a lot of time and resources of your computer! So it only fetches 20 records at a time. You can iterate through next 20 documents by using command “it“.
find() method scans through all the documents present in the collection to find multiple matches for the condition. So in above case, find() method scans through 10 Million documents, hence returns the result slowly. Where as findOne() method stops scanning the collection as soon as it finds the first matching document, so findOne() returns result faster than find() method.
We create index on “student_id”. It takes little time to create the index, as we have 10 Million documents inside “no” collection.
After creating index on “student_id”, run the same command and you’ll get the results instantly – maybe it takes 0.01 ms, but the delay can’t be noticed. Why does it return results faster after creating index on “student_id”? Watch this short video lesson to know it: index / key: MongoDB