In this slide we try to figure out what are loops and why they are important and using them with a couple of examples. After that we will try to understand some other statements like Break, Continue and Goto statement.
Loops are used to perform some task repeatedly until some condition remains true. for e.g. take this blog, we will keep learning C++ until we don't get to a level, where we can write basic C++ program very easily.
Let's discuss these, one by one -
- for loop Statement.
- while loop Statement.
- do-while loop Statement.
for loop
#include<iostream> using namespace std; int main() { for( initialization ; condition ; increment/decrement ) { Body; } }
Here, in Initialization part we define our predicate/variable which going to be used in our loop. (We can modify these initialization, condition, increment/decrement statements according to need of the task)
let's see a program to find Sum of first 100 natural numbers, using For loop.
#include<iostream> using namespace std; int main() { int sum = 0; // Sum is initialized to 0; for(int i = 1; i<= 100; i++) sum += i; /* Other way we can write it, by doing the initialization in some other step, before loop. int i = 1; for(;i <= 100 ;i++) sum += i */ cout << "Sum of first 100 natural numbers is : " << sum << endl; /* Direct formula for calculating sum of first "n" natural numbers is n*(n+1)/2 */ return 0; }
Mostly, we use for loop in initializing the printing array, because there indices are consecutive it is very easy to use for loop, for example -
#include<iostream> using namespace std; int main() { int sum = 0; // Sum is initialized to 0; int *arr = new int[10]; for(int i = 1; i<= 10; i++) arr[i] = 2*(i-1); // Initializing array with some values. // Try to figure out what will be the values. for(int i = 0; i<10; i++) cout << arr[i] << " "; cout << endl; return 0; }
Creating array of size dynamically and using for loop to initialize it.
Did you noticed, after for loop we have not used curly braces( { } ), because, if we are writing only one statement after this special statement like if, else, for, while etc. we don't need to use curly braces but writing more than one statements after these special statements and to make them lie inside these statement we have to use curly braces. around them.
There is a one more type of for loop, that C++ provide us on the go.
Foreach loop -
It resembles to the loop that we write in python and was included in C++11 ,For syntax, see below program -
#include<iostream> using namespace std; int main() { // We will iterate over the array using foreach loop. int arr[10] = {1,2,3,4,5,6,7,8,9,10}; for(int x : arr) cout << x << " "; cout << endl; return 0; }
Remember you can't modify the values of arr elements using this loop, until you don't define the "x" as a reference to array elements(If you didn't get it now don't worry, will be covering soon).
Now, we shall start our talk about while loop -
While loop
#include<iostream> using namespace std; int main() { while(some condition) { // Do something; } return 0; }
#include<iostream> using namespace std; int main() { int a = 10; while(a < 20) // We don't know when "a" becomes 20. { cout << a << " "; ++a; } cout << endl; return 0; }
do-While loop
#include <iostream> using namespace std; int main() { do{ // Do Something }while(condition); // don't forget semicolon here return 0; }
#include <iostream> using namespace std; int main() { int a = 10; do{ cout << "A is less than 5" << endl; } while ( a < 5 ); // False as 10 > 5 return 0; }
#include <iostream> using namespace std; int main() { // We have lineraly search over this array to find 6 int arr[] = {1,2,3,4,5,6,7,8,9,10}; for(int i = 0; i< 10; i++) { if(arr[i] == 6) { cout << "Found at " << i << " index" << endl; break; } } /* In the above loop I used break to save some time as if we already found the 6 there is no need to iterate over other elements, so directly break the loop, otherwise it will keep iteraing over next elements even if there are million of elements next to it. */ return 0; }
#include <iostream> using namespace std; int main() { // We have to print number from 1 - 10 but not 6 for(int i = 1; i<=10; i++) { if(i == 6) continue; // this statement skips every statement occuring after it. cout << "Value is : " << i << endl; } return 0; }
#include <iostream> using namespace std; int main() { cout << "First" << endl; cout << "Second" << endl; goto last; // Jumps from here cout << "thrid" << endl; cout << "fourth" << endl; last: // To here like. cout << "Fifth" << endl; cout << "sixth" << endl; return 0; }
Try to execute this program in your environment, You will see that "third" and "fourth" will not be printed out, this is due to we jumped from above of those two statements.
You can jump in either direction of your program, it just depends on your program and you.
It sufficient for now, practicing will be done throughout the whole slides. Keep reading.
Stay tuned for the upcoming content. For any query leave a comment below.
- Lippman, Stanley B. c++ primer, 3rd ed. April 2 , 1998.
Comments
Post a Comment