Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

For loop (Continued)

Example: How to create an infinite loop.


Method 1: Using empty statements
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for( ; ; ){
cout << "This is just for testing";
}
return 0;
}
Method 2: Using true in the condition part
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for( ; true; ){
cout << "This is just for testing";
}
return 0;
}

Method 3: Using any statement that doesn't contain or


results 0, false, NULL, null_ptr or '\0'
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for( ; cout << "PF"; ){
cout << "This is just for testing";
}
return 0;
}

Method 3b: Using any statement that doesn't contain or


results 0, false, NULL, null_ptr or '\0'
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = 0;
for( ; number = 10; number += 1){
cout << "This is just for testing";
}
return 0;
}
Method 4: Changing the positions of the components of loop
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = 0;
for(number < 10 ; number = 1; number += 1){
cout << "This is just for testing";
}
return 0;
}
Method 5: Logical error in the arithmetic operation
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for(int number = 0; number < 10; number -= 1){
cout << "This is just for testing";
}
return 0;
}

Example: How to never to start a loop.


Method 1: Using false in the condition part
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for( ; false; ){
cout << "This is just for testing";
}
return 0;
}

Method 2a: Using any statement that contain or results 0,


false, NULL, null_ptr or '\0'
#include <iostream>
using namespace std;

int main(int argc, char ** args){


for( ; NULL; ){
cout << "This is just for testing";
}
return 0;
}

Method 2b: Using any statement that contain or results 0,


false, NULL, null_ptr or '\0'
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = 10;
for( ; number = 0; ){
cout << "This is just for testing";
}
return 0;
}

Method 3: Logical error in the arithmetic operation


#include <iostream>
using namespace std;

int main(int argc, char ** args){


for(int number = 0; number > 10; number += 1){
cout << "This is just for testing";
}
return 0;
}
***********************************************************************
Example: Printing the ASCII table

#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = 32;
cout << "Symbol Value" << endl;
for( ; number <= 127; number += 1){
cout << ((char)number) << "\t" << number << endl;
}
return 0;
}

Nested For loop


- for loop within for loop

Example: Printing the right angled triangle


*
* *
* * *
* * * *
* * * * *

#include <iostream>
using namespace std;

int main(int argc, char ** args){


int size = 0;
cout << "Enter the size of the triangle: ";
cin >> size;
for (int line = 1; line < size + 1; line += 1){
for (int star = 1; star < line + 1; star += 1){
cout << "* ";
}
cout << endl;
}
return 0;
}

Escape Sequence
- Characters which are used in string to change the sequence of the string
- Characters
- \t
- adds tab (4-8) space
- \n
- new line
- \r
- return to the starting of the string
- \\
- to print \
- \"
- to print "
- \'
- to print '
Example: Escape sequences
#include <iostream>
using namespace std;
int main(int argc, char ** args){
cout << "This\tis\njust\rfor\\testing\"escape sequences\"";
return 0;
}

Type Casting
- Conversion of one type of data into the other type of data
- 2 types
- Implicit casting
- conversion which is done by the compiler itself
Example: Implicit casting
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = 4.5f; // Implicit casting, the 4.5f will converted into int 4
cout << number; // 4
return 0;
}
- Explicit casting
- conversion which is done by the programmer
Example: Explicit casting
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = ((int) 4.5f); // Explicit casting, the 4.5f will converted into
int 4
cout << number; // 4
return 0;
}
- 2 Behavior
- Promotion
- Smaller type of value is converted into bigger type of
value/data type
- Example
#include <iostream>
using namespace std;

int main(int argc, char ** args){


double number = ((double) 4.5f); // Explicit casting, the 4.5f will converted
into double 4.5
cout << number; // 4.5
return 0;
}
- Demotion
- Bigger type of value is converted into smaller type of value
- Data loss
- Example
#include <iostream>
using namespace std;

int main(int argc, char ** args){


int number = ((int) 4.5f); // Explicit casting, the 4.5f will converted into
int 4
cout << number; // 4
return 0;
}

You might also like