0% found this document useful (0 votes)
10 views49 pages

C++ For Loop

The document provides an overview of various loop constructs in C++, including for loops, while loops, do-while loops, and their nested and infinite variants. It also discusses control statements like break, continue, and goto, as well as the concept of functions, their advantages, and parameter passing techniques such as call by value and call by reference. Additionally, it covers the scope of variables, differentiating between local and global variables.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views49 pages

C++ For Loop

The document provides an overview of various loop constructs in C++, including for loops, while loops, do-while loops, and their nested and infinite variants. It also discusses control statements like break, continue, and goto, as well as the concept of functions, their advantages, and parameter passing techniques such as call by value and call by reference. Additionally, it covers the scope of variables, differentiating between local and global variables.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

C++ For Loop

• The C++ for loop is used to iterate a part of the


program several times. If the number of
iteration is fixed, it is recommended to use for
loop than while or do-while loops.
• for(initialization; condition; incr/decr){
• //code to be executed
• }
• #include <iostream>
• using namespace std;
• int main() {
• for(int i=1;i<=10;i++){
• cout<<i <<"\n";
• }
• }
• Output: 1 2 3 4 5 6
7 8 9 10
C++ Nested For Loop

• In C++, we can use for loop inside another for


loop, it is known as nested for loop. The inner
loop is executed fully when outer loop is
executed one time.
• #include <iostream>
• using namespace std;

• int main () {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• cout<<i<<" "<<j<<"\n";
• }
• }
• }
• Output:
• 1 1 1 2 1 3 2 1 2 2 2
3 3 1 3 2 3 3
• C++ Infinite For Loop
• If we use double semicolon in for loop, it will
be executed infinite times.
• int main () {
• for (; ;)
• {
• cout<<"Infinitive For Loop";
• }
• }
• Output:
• Infinitive For Loop
• Infinitive For Loop
• ctrl+c
• C++ While loop
• In C++, while loop is used to iterate a part of
the program several times. If the number of
iteration is not fixed, it is recommended to use
while loop than for loop.
• while(condition){
• //code to be executed
• }
• #include <iostream>
• using namespace std;
• int main() {
• int i=1;
• while(i<=10)
• {
• cout<<i <<"\n";
• i++;
• }
• }

• Ouput: 1 2 3 4 5 6 7 8 9
10
• C++ Nested While Loop Example
• In C++, we can use while loop inside another
while loop, it is known as nested while loop.
The nested while loop is executed fully when
outer loop is executed once.
• #include <iostream>
• using namespace std;
• int main () {
• int i=1;
• while(i<=3)
• {
• int j = 1;
• while (j <= 3)
• {
• cout<<i<<" "<<j<<"\n";
• j++;
• }
• i++;
• }
• }

• Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1
3 2 3 3
• C++ Infinitive While Loop Example:
• We can also create infinite while loop by
passing true as the test condition.
• #include <iostream>
• using namespace std;
• int main () {
• while(true)
• {
• cout<<"Infinitive While Loop";
• }
• }
• Output:
• Infinitive While Loop
• Infinitive While Loop
• ctrl+c
• C++ Do-While Loop
• The C++ do-while loop is used to iterate a part of
the program several times. If the number of
iteration is not fixed and you must have to execute
the loop at least once, it is recommended to use
do-while loop.
• The C++ do-while loop is executed at least once
because condition is checked after loop body.
• do{
• //code to be executed
• }while(condition);
• #include <iostream>
• using namespace std;
• int main() {
• int i = 1;
• do{
• cout<<i<<"\n";
• i++;
• } while (i <= 10) ;
• }
• Output: 1 2 3 4 5 6 7 8 9
10
• C++ Nested do-while Loop
• In C++, if you use do-while loop inside another
do-while loop, it is known as nested do-while
loop. The nested do-while loop is executed
fully for each outer do-while loop.
• #include <iostream>
• using namespace std;
• int main() {
• int i = 1;
• do{
• int j = 1;
• do{
• cout<<i<<"\n";
• j++;
• } while (j <= 3) ;
• i++;
• } while (i <= 3) ;
• }
• Output: 1 1 1 2 1 3 2 1 2 2 2 3
3 1 3 2 3 3
• C++ Infinitive do-while Loop
• In C++, if you pass true in the do-while loop, it
will be infinitive do-while loop.
• do{
• //code to be executed
• }while(true);
• C++ Infinitive do-while Loop Example
• #include <iostream>
• using namespace std;
• int main() {
• do{
• cout<<"Infinitive do-while Loop";
• } while(true);
• }
• Output:
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• ctrl+c

• C++ Break Statement
• The C++ break is used to break loop or switch
statement. It breaks the current flow of the
program at the given condition. In case of
inner loop, it breaks only inner loop.
• jump-statement;
• break;
• #include <iostream>
• using namespace std;
• int main() {
• for (int i = 1; i <= 10; i++)
• {
• if (i == 5)
• {
• break;
• }
• cout<<i<<"\n";
• }
• }
C++ Break Statement with Inner Loop

• #include <iostream>
• using namespace std;
• int main()
• {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• break;
• }
• cout<<i<<" "<<j<<"\n";
• }
• }
• }
C++ Continue Statement

• The C++ continue statement is used to


continue loop. It continues the current flow of
the program and skips the remaining code at
specified condition. In case of inner loop, it
continues only inner loop.
• jump-statement;
• continue;
• #include <iostream>
• using namespace std;
• int main()
• {
• for(int i=1;i<=10;i++){
• if(i==5){
• continue;
• }
• cout<<i<<"\n";
• }
• }
C++ Continue Statement with Inner Loop

• #include <iostream>
• using namespace std;
• int main()
• {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• continue;
• }
• cout<<i<<" "<<j<<"\n";
• }
• }
• }
C++ Goto Statement

• The C++ goto statement is also known as jump


statement. It is used to transfer control to the
other part of the program. It unconditionally
jumps to the specified label.
• It can be used to transfer control from deeply
nested loop or switch case label.
• #include <iostream>
• using namespace std;
• int main()
• {
• ineligible:
• cout<<"You are not eligible to vote!\n";
• cout<<"Enter your age:\n";
• int age;
• cin>>age;
• if (age < 18){
• goto ineligible;
• }
• else
• {
• cout<<"You are eligible to vote!";
• }
• }
• #include <iostream>
• using namespace std;
• int main(){
• int num; cout<<"Enter a number: "; cin>>num;
• if (num % 2==0){
• goto print;
• }
• else {
• cout<<"Odd Number";
• }
• print:
• cout<<"Even Number";
• return 0;
• }
C++ Functions

• The function in C++ language is also known as procedure or


subroutine in other programming languages.
• To perform any task, we can create function. A function can
be called many times. It provides modularity and code
reusability.
• A function is a block of code which only runs when it is
called.
• You can pass data, known as parameters, into a function.
• Functions are used to perform certain actions, and they are
important for reusing code: Define the code once, and use
it many times.
Create a Function

• C++ provides some pre-defined functions, such as main(), which is used to


execute code. But you can also create your own functions to perform certain
actions.
• To create (often referred to as declare) a function, specify the name of the
function, followed by parentheses ():
• Syntax
• Declaration of a function
• The syntax of creating function in C++ language is given below:
• return_type function_name(data_type parameter...)
• {
• //code to be executed
• }
• myFunction() is the name of the function
• void means that the function does not have a
return value. You will learn more about return
values later in the next chapter
• inside the function (the body), add code that
defines what the function should do
Call a Function

• Declared functions are not executed


immediately. They are "saved for later use",
and will be executed later, when they are
called.
• To call a function, write the function's name
followed by two parentheses () and a
semicolon ;
• Inside main, call myFunction():
• // Create a function
void myFunction() {
cout << "I just got executed!";
}

int main() {
myFunction(); // call the function
return 0;
}
Advantage of functions in C++

• There are many advantages of functions.


• 1) Code Reusability
• By creating functions in C++, you can call it many times. So we
don't need to write the same code again and again.
• 2) Code optimization
• It makes the code optimized, we don't need to write much code.
• Suppose, you have to check 3 numbers (531, 883 and 781)
whether it is prime number or not. Without using function, you
need to write the prime number logic 3 times. So, there is
repetition of code.
• But if you use functions, you need to write the logic only once and
you can reuse it several times.
Types of Functions

• There are two types of functions in C programming:


• 1. Library Functions: are the functions which are
declared in the C++ header files such as ceil(x),
cos(x), exp(x), etc.
• 2. User-defined functions: are the functions which
are created by the C++ programmer, so that he/she
can use it many times. It reduces complexity of a
big program and optimizes the code.
Scope of variables

• The scope of a variable is defined as the


extent of the program code within which the
variable can be accessed or declared or
worked with. There are mainly two types of
variable scopes:
• Local Variables
• Global Variables
Local Variables

• Variables defined within a function or block are


said to be local to those functions.
• Anything between ‘{‘ and ‘}’ is said to inside a
block.
• Local variables do not exist outside the block in
which they are declared, i.e. they can not be
accessed or used outside that block.
• Declaring local variables: Local variables are
declared inside a block.
• #include<iostream>
• using namespace std;
• void func()
• {
• // this variable is local to the
• // function func() and cannot be
• // accessed outside this function
• int age=18;
• cout<<age;
• }
• int main()
• {
• cout<<"Age is: "<<age;
• return 0;
• }
Global Variables

• They are available through out the life time of


a program.
• They are declared at the top of the program
outside all of the functions or blocks.
• Declaring global variables: Global variables are
usually declared outside of all of the functions
and blocks, at the top of the program. They
can be accessed from any portion of the
program
• #include<iostream>
• using namespace std;

• // global variable
• int global = 5;

• // global variable accessed from
• // within a function
• void display()
• {
• cout<<global<<endl;
• }
• // main function
• int main()
• { display();
• // changing value of global
• // variable from main function
• global = 10;
• display();
• }
Parameter Passing Techniques

• There are different ways in which parameter data can


be passed into and out of methods and functions.
• Let us assume that a function B() is called from
another function A(). In this case A is called
the “caller function” and B is called the “called
function or callee function”.
• Also, the arguments which A sends to B are
called actual arguments and the parameters of B are
called formal arguments.
• Call by value and call by reference in C++
• There are two ways to pass value or data to
function in C language: call by value and call
by reference. Original value is not modified in
call by value but it is modified in call by
reference
Call by value in C++

• In call by value, original value is not modified.


• In call by value, value being passed to the
function is locally stored by the function
parameter in stack memory location. If you
change the value of function parameter, it is
changed for the current function only. It will
not change the value of variable inside the
caller method such as main().
• #include <iostream>
• using namespace std;
• void change(int data);
• int main()
• {
• int data = 3;
• change(data);
• cout << "Value of the data is: " << data<< endl;
• return 0;
• }
• void change(int data)
• {
• data = 5;
• }
Call by reference in C++

• In call by reference, original value is modified


because we pass reference (address).
• Here, address of the value is passed in the
function, so actual and formal arguments
share the same address space. Hence, value
changed inside the function, is reflected inside
as well as outside the function.
• #include<iostream>
• using namespace std;
• void swap(int *x, int *y)
• {
• int swap;
• swap=*x;
• *x=*y;
• *y=swap;
• }
• int main()
• {
• int x=500, y=100;
• swap(&x, &y); // passing value to function
• cout<<"Value of x is: "<<x<<endl;
• cout<<"Value of y is: "<<y<<endl;
• return 0;
• }

You might also like