0% found this document useful (0 votes)
49 views

Structure in C++

This document discusses various control structures or loops in C++ programming including for, while, do-while loops and jump statements like break, continue and goto. It provides the syntax and usage of each loop and jump statement, explaining how they allow programmers to control the flow and execution of code within a program. Key loop concepts covered are initialization, condition, increment, repetition, termination and nesting of loops.

Uploaded by

Tooba Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Structure in C++

This document discusses various control structures or loops in C++ programming including for, while, do-while loops and jump statements like break, continue and goto. It provides the syntax and usage of each loop and jump statement, explaining how they allow programmers to control the flow and execution of code within a program. Key loop concepts covered are initialization, condition, increment, repetition, termination and nesting of loops.

Uploaded by

Tooba Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming Fundamentals

in C++

(PRACTICAL#05)
Objective: To become familiar with Control Structures
(Iteration statements/ Loops)
 The execution of the program is linear.
 Control Structures are used to alter/change the flow of program.
 Controls which statement(s) should be executed next.
Control Structures

Selection / Iterative/
Conditional/ Branching Loops (Jump Statements)
 Loops Definition : cause a section of your code
to repeated a certain number of times,
 The repetition continues while a condition
remains true, when the condition becomes
false the loop terminates and the control
transferred to the statements following the
loop.
 for Loop
 while Loop
 do-while Loop

 Jump Statements (Break, Continue, goto , return)


(Iteration statements) Loops
 for Loop : Executes a section of your program a fixed number
of times.
 The for loop is used when you know before entering the loop,
how many times the loop body will execute.
 Syntax
for (initialization; test expression/condition; increment)
{
// body of for loop
}
 The loop first starts, the initialization portion of the loop is executed and sets
the value of the loop control variable.
 The condition is evaluated and this must be a Boolean expression.
 Increment expression is an expression that increments or decrements the
loop control variable.
for Loop : Syntax

In case of Multiple Statements, enclose them


into pair of braces to form a code block.
Iteration Statements

void main() {
Int n;
for(n=1; n<=10; n++)
cout<<“Loop Cyc le: “<< + n;
}
Iteration Statements
loop variations
 for(int
z = 100; z>=1; z--);
 //loops from 100 to 1 with step of -1.

 •for(inti = 5; i<=100; i+=5);


 //loop from 5 to 100 with step of +5.
Nested for Loop
 Nested: one for loop inside the other for loop.
 One for loop can be nested inside the other one.
 For each single step of the outer loop, inner for loop will
complete its all iterations.

 Nested for loops have a lot of applications, they are


mostly used to access elements in multi dimensional
arrays.
Iteration Statements

 While :A while statement repeats an action again and again as long as a controlling
Boolean expression is true.
 When condition becomes false, control passes to the next line of code immediately
following the loop.
 Used when we don’t know the exact number of repetitions to be performed in our program.
 It uses “while” (without quotes) as a c++ keyword for looping.
Syntax
while(test expression)
{
// body of loop
}
Iteration Statements

 While : Syntax
 When condition becomes false, control passes to the next line of code immediately
following the loop.
 Used when we don’t know the exact number of repetitions to be performed in our program.
 It uses “while” (without quotes) as a c++ keyword for looping.
Syntax
while(test expression)
{
// body of loop
}
While counter-controlled repetition

int main(){
int count =0;

while(count < 50 ){
Cout<<count<<endl;
count++;
}
Cout<<“End of loop”;
}
While Sentinel-controlled repetition

int main(){
int num1, num2; char ch =‘A’;
while(ch != ‘n’ ) {
Cout<Cout<<“Enter first number:”;
cin>>num1
<“Enter second number:”;
cin>>num2;
Cout<<“Sum=”<<num1+num2;
Cout<<“Do you want to continue(y/n)”;
Cin>>ch;
}
Cout<<“End of loop”;
}
Nested while loop
Allow the flow of execution to jump to a different part of the program.

Break: terminates the existing structure.

continue : causes a loop to stop its current iteration and begin


the next one.

goto: causes the logic to jump to a different place in the


Iteration Statements

 Do . . . While
 the body of a do-while loop is always executed at least once, no matter
what the initial state of test expression.
Syntax
do {
First_Statement
Second_Statement
...
Last_Statement
}
while(Boolean_Expression);
do-while loop
Allow the flow of execution to jump to a different part of the program.

Break: terminates the existing structure.

continue : causes a loop to stop its current iteration and begin


the next one.

goto: causes the logic to jump to a different place in the


Iteration Statements

void main(){
int count =0;
do {
cout<<count;
count++;
}
while(count<=10);
Cout<<“End of loop”;
}
Nested do-while loop
Allow the flow of execution to jump to a different part of the program.

Break: terminates the existing structure.

continue : causes a loop to stop its current iteration and begin


the next one.

goto: causes the logic to jump to a different place in the


(Jump Statements)
Allow the flow of execution to jump to a different part of the program.

Break: terminates the existing structure.

continue : causes a loop to stop its current iteration and begin


the next one.

goto: causes the logic to jump to a different place in the


program.

return: causes a function to jump back to the function that


called it.
break statement

 terminates the existing structure


 break statement is used to terminate the for
loop or to take exit from the for loop.
void main() {
for(int i=1; i<=100; i++) {
if ( i==20 )
Break;
Cout<<“: " << i;
}
Continue statement

 Continue statement causes the current loop cycle to


terminate, the execution continues with the next loop cycle
void main() {
for(int i=1; i<=10; i++) {
if ( i%2 !=0 )
Continue;
cout<<“: “<< i;
}
goto: Labels lines of code and jump straight through to that code.
causes the logic to jump to a different place in the program.

int num;
cout<<"Enter a number: "; cin>>num;
if (num % 2==0){ goto print; }
else {
cout<<"Odd Number";
}
print: cout <<"Even Number"; return 0; }
int num;
cout<<"Enter a number: "; cin>>num;
if (num % 2==0){ goto print; }
else {
cout<<"Odd Number";
}
print:
cout <<"Even Number"; return 0; }

You might also like