Programming Fundamentals Lec 06

You might also like

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

* Programming

Fundamentals

Lecture 06

Instructor: Rao Umer Farooq


*Programming Fundamentals
Do While Loop:

*Do while loop always executes the block of code at least


once
*No matters condition is true or false

*It iterates as long as the condition remains true.

*This is an exit-controlled loop.


*Programming Fundamentals
Do While Loop:
*Syntax:
*Programming Fundamentals
Do While Loop:
*Example:
main()
{
int i=1;
do
{
cout <<i<<endl;
i++;
}
while (i<=20);
cout<< "program end";
}
*Programming Fundamentals
Do While Loop:
Use a do…while statement to print the numbers 1–10.
*Programming Fundamentals
Do While Loop:
Use a do…while statement to print the numbers 1–10.
*Programming Fundamentals
Continue Statement:

* The goto statement gives the power to jump to any part of a


program but, makes the logic of the program complex and
tangled.

* The goto statement can be replaced in most of C++


program with the use of break and continue statements.
*Programming Fundamentals
Continue statement

*Continue statement is used inside the loops.

*Whenever a continue statement is encountered inside a loop,


*control directly jumps to the beginning of the loop for
next iteration

*skipping the execution of statements inside loop's body for


the current iteration.
*Programming Fundamentals
Continue statement - Example
main ()
{
int i;
for (i=1; i<=30; i++)
{
if (i%3==0)
continue;
cout<< i<< endl;
}
}
*Programming Fundamentals
Break Statement

*The break in C++ is a loop control statement


*which is used to terminate the loop.

*As soon as the break statement is encountered within a loop


*the loop iterations stops there and control returns from the
loop immediately to the first statement after the loop.
*Programming Fundamentals
Break Statement - Example

main ()
{
int i;
for (i=1; i<=30; i++)
{
if (i%5==0)
break;
cout<< i<< endl;
}
cout << “Program end”;
}
*Programming Fundamentals
File Handling in C++
*Files are used to store data in a storage device permanently.
*File handling provides a mechanism to store the output of a
program in a file and to perform various operations on it.
*In file handling we can perform following task using c++
code:
Create a file ( .doc, .csv, .ppt, .txt etc)
Save the output of program into required file
Display the content of file in c++ console window
Add content to existing file without effecting previous
stored data
*Programming Fundamentals
File Handling in C++

*For file handling we use preprocessor directive fstream


*#include<fstream>

*ofstream (output file stream: for file creation or output)


*ifstream (for input or read the content of file)
*fstream ( for input and output)
* File Handling (Create a file)

#include<iostream> #include<iostream> #include<iostream>


#include<fstream> #include<fstream> #include<fstream>
using namespace std; using namespace std; using namespace std;
main () main () main ()
{ { {
ofstream file
ofstream file ofstream file
("G://introduction.doc" ); ("G://introduction.csv“); ("G://stdata.csv" );
{
file << “how are you"<< file << “hello word"<< endl;
endl; file << "name,
file.close(); rollno. ,adress"<< endl;
file.close();
} file<< "abc, 123,
} street4areacdf";
}
file.close();
}

You might also like