Lecture 1-2
Lecture 1-2
Object-Oriented
Programming
Lecture 1
4 Use the concept of file handling to read/write the data from binary and Cognitive C3 5
text files and write codes by using templates and exception handling.
• Follow the above points and you will definitely get a good grade
Correct Output
Incorrect Output
• If we want to print first 10 integers then the loop will look like this
Required Final value of control
Semicolon variable for which Required
Control variable Separator the condition is true Semicolon
initialization Separator
for keyword
4. for (int i=0; i<=10; i*=2) infinite iterations (condition is always true)
5. for (int i=0; i<50; i--) 0 iterations (condition is false in 1st run)
6. for (int i=10; i<=1; i++) infinite iterations (i will never be updated)
7. for (int i=0; i<=10; i+3)
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 30
NATIONAL UNIVERSITY
of Computer & Emerging Sciences
Chiniot-Faisalabad Campus
Object-Oriented
Programming
Lecture 2
• The above program will generate an error, because the variables defined inside any block are
invisible to rest of code and cannot be accessed outside that block
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 36
Placing a Semicolon After for
Loop
• If we place a semicolon after a for loop like this:
for(int a = 0; a <= 5; a++);
{
cout<<“Hello”<<endl;
}
• Then it do the followings:
• Repeat six times for (a=0;a<=5;a++)
• do nothing (semicolon)
• Open a new scope for local variables {
• Print "hello"
• Close the scope }
• The operation that gets repeated is ; not the cout
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 37
Review Question 1
Identify the number of iterations and output of the following code.
6 iterations
4 iterations
4 iterations
Syntax Error
a = 6 will be displayed
int a = -1;
while(a < 0)
{
cout<<“Enter the value of a: ”;
cin>>a;
}
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 43
continue and break Statements
with “while”
• Break Statement • Continue Statement
int a = 0; int a = 0;
while(a < 5) while(a < 5)
{ {
if(a == 2) if(a == 2)
{ {
break; continue;
} }
cout<<"a = "<<a<<endl; cout<<"a = "<<a<<endl;
a++; a++;
} }
• The loop terminates, once the break statement is • Will result in infinite iterations, as “a” will not exceed
executed. from 2
int a = 0;
int a = 0;
while(a <= 5) int b = 5;
{
Output = 20
while(a <= 5)
int b = 5; {
b = b + a; b = b + a;
a++;
a++;
}
} cout<<b<<endl;
cout<<b<<endl;
• The above code will generate a syntax error, as we are using the local variable
outside its scope.
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 45
Placing a Semicolon after While
Loop
• Placing a semicolon after the while loop, will result in infinite
iterations
• Because the loop body never executes, and the compiler will keep on
checking the loop condition
int a = 0;
while(a <= 5);
{
cout<<“a = ”<<a<<endl;
a++;
}
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 46
do-while Loop
• Do-while loop will also repeat the specific section of code
multiple times.
• Do-while loop is used when we want to make sure at least one
iteration
• The syntax of a do-while loop is given below
do
{
Statements;
}
while(condition);
• The above loop will keep on iterating until the condition is true
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 47
Comparison of “for”, “while”
and “do” loop
• for loop
• Used when we know that how many times the code/section of code will
repeat
• Runs for fixed number of times and minimum iterations are 0
• while loop
• Used when we don’t know that how many time the code will repeat
• Loop condition is evaluated at the start loop will iterate minimum 0 times
• do-while loop
• Used to repeat a code until the user wants
• Condition is evaluated at the end of loop loop will iterate at least 1 time
• Here the function is performing some independent task on a fixed data. Hence no
parameters are required.
void print_line()
{
for(int i=0; i<80; i++)
{
cout<<“*”;
}
}
CS1004 - OOP Course Instructor: Muhammad Sajid Iqbal 59
Example 1 (Modification 1)
• Write a function to print a line of 80 characters entered by the user.
• Now both the number of iterations and the character are not fixed, we need to pass two
arguments (one integer and one character).
• Now both the number of iterations and the character are not fixed, we need to pass two arguments (one
integer and one character).
• In the end we need to return an integer as well.