Week 06
Week 06
COMPUTER PROGRAMMING
Muzammil Ahmad Khan
[email protected]
2
C++ Infinite Loop
• C++ Infinite Loop: This loop can also be called as “endless loop”.
This loop in C++ can be defined as a piece of code which lacks
the exit such that it repeats repeatedly. This loop occurs when it is
always executing true. In C++, if you pass true in the do-while
loop it will be known as “infinitive do-while loop”. Even if you
are having an initialization and increment expression but in C++
we use the ‘for(; ;)’ construct to signify the infinite loop.
3
Flowchart – Infinite For Loop
As the condition is never going to be
false, the control never comes out of
the loop, and forms an Infinite Loop as
shown in the above diagram, with blue
paths representing flow of infinite for
loop.
4
C++ Infinite Loop
• In case if you want we can terminate Output:
this loop by pressing the Ctrl + C keys. This loop will never end
#include <iostream> This loop will never end
int main(){ This loop will never end
This loop will never end
using namespace std; This loop will never end
for( ; ; ){ This loop will never end
cout<<"This loop will never end"<<endl; This loop will never end
} This loop will never end
This loop will never end
return 0; This loop will never end
}
5
Infinite For loop with condition=true
• For Loop condition is a boolean #include <iostream>
using namespace std;
expression that evaluates to true or
int main() {
false. So, instead of providing an for (; true; ) {
cout << "hello" << endl;
expression, we can provide the
}
Note: You will see the string hello print
boolean value true, in place of } to the console infinitely, one line after
another. If you are running from
Output
condition, and the result is an command prompt or terminal, to
terminate the execution of the program,
hello
infinite for loop. enter Ctrl+C from keyboard. If you are
hello
running the program from an IDE, click
hello
on stop button provided by the IDE.
hello
6
Infinite For loop with condition that always
evaluates to true
• Instead of giving true boolean value for #include <iostream>
using namespace std;
the condition in for loop, you can also
give a condition that always evaluates int main() {
for (; 1 == 1; ) {
to true. For example, the condition 1 cout << "hello" << endl;
}
== 1 or 0 == 0 is always true. No matter }
how many times the loop runs, the Output
hello
condition is always true and the for hello
hello
loop will run forever. Hello
.
. 7
Infinite For loop with no update to control
variables
• These type of infinite for loops may result when #include <iostream>
you forget to update the variables participating in using namespace std;
the condition.
9
Infinite While loop with true for condition
While Loop condition is a boolean #include <iostream>
expression that evaluates to true or using namespace std;
false. So, instead of providing an int main() {
expression, we can provide the while (true) {
boolean value true, in place of cout << "hello" << endl;
condition, and the result is an infinite }
}
while loop. Output Note: You will see the string hello print
hello to the console infinitely. If you are
running from command prompt or
hello terminal, to terminate the execution of
hello the program, enter Ctrl+C from
hello keyboard.
10
Infinite While loop with true for condition
Instead of true, you can also give a non- Output
zero integer. hello
hello
hello
#include <iostream> hello
using namespace std;
Note: You will see the string
int main() { hello print to the console
while (1) { infinitely. If you are running
from command prompt or
cout << "hello" << endl;
terminal, to terminate the
} execution of the program,
} enter Ctrl+C from keyboard.
11
Infinite While loop with condition that always
evaluates to true
Instead of giving true boolean value #include <iostream>
or a non-zero integer in place of while using namespace std;
loop condition, you can also give a int main() {
condition that always evaluates to while (1 == 1) {
true. For example, the condition 1 == cout << "hello" << endl;
1 or 0 == 0 is always true. No matter }
}
how many times the loop runs, the
Output
condition is always true. hello
hello
hello
hello
12
Infinite While loop with no update to control
variables
• These type of infinite while loops may #include <iostream>
result when you forget to update the using namespace std;
variables participating in the while loop
condition. int main() {
• In the following example, we have int i = 0;
initialized variable i to 0 and would like to while (i < 10) {
print a string to console while the i is less cout << "hello" << endl;
than 10. Typically, in the following }
example, one would increment i inside }
Output
while loop body, to print hello 10 times. hello
But, if we forget this update statement in hello
the while body, i is never changed. This hello
could make the loop an infinite while loop. hello
13
Infinite While loop while working with
continue statement
• This also is a typical scenario where we use a include <iostream>
continue statement in the while loop body, using namespace std;
but forget to modify the control variable.
• Extending the previous example, consider we int main() {
have added the increment statement. But, int i = 0;
now we have an additional functionality that while (i < 10) {
the loop has to continue when i becomes 5. if (i == 5) { Output
When continue statement is executed, the continue; hello
executes goes to the while condition, and the } hello
increment statement is not executed. This cout << "hello" << endl; hello
results in the scenario where i is never i++; hello
incremented again. And the while loop } hello
executes infinitely. }
14
C++ Infinite Loop
#include <iostream> Output:
using namespace std;
Output Limit Exceeded
int main() {
do{
cout<<"Infinitive do-while Loop";
} while(true);
}
15
break & continue Statements
• break and continue alter the flow of control
• When the break statement executes in a repetition structure, it immediately exits
• The break statement, in a switch structure, provides an immediate exit
• The break statement can be used in while, for, and do...while loops
• The break statement is used for two purposes:
1. To exit early from a loop
2. To skip the remainder of the switch structure
• After the break statement executes, the program continues with the first statement
after the structure
• The use of a break statement in a loop can eliminate the use of certain (flag)
variables
16
break & continue Statements
• continue is used in while, for, and do…while structures
− It skips remaining statements and proceeds with the next iteration of the loop
• if the break is inside nested structures, control exits only the innermost
structure containing it
18
Continue Statement
• is valid only within loops
• terminates the current loop iteration, but not the entire loop
• in a do-while, the exit condition is tested, and if true, the next loop
iteration is begun
19
The break Statement
int j = 40;
while (j < 80){
j += 10;
if (j == 70)
break;
cout << “j is “ << j<< ‘\n’;
}
cout << “We are out of the loop as j=70.\n”;
20
The continue Statement
int j = 40;
while (j < 80){
j += 10;
if (j == 70)
continue; //skips the 70
cout << “j is “ << j<< ‘\n’;
}//see UseContinue1.cpp
cout << “We are out of the loop” << endl;
21
break and continue
22
Break Statement
23
Continue Statement
24
Break and Continue in While Loop
• You can also use break and continue in 3int i = 0;
while loops: while (i < 10) {
if (i == 4) {
int i = 0;
i++;
while (i < 10) { continue;
cout << i << "\n"; }
i++; cout << i << "\n";
if (i == 4) { i++;
break; }
} Output
} 0
1
2
Output 5
0 6
1 7
2 8
9 25
Exercise:
26
exit( ) function
27
exit( ) function
• Today we’ll learn about exit() in C++. We know we can break out
of loops using the built-in break function in C++. Similarly, we
can also break out of a whole C++ program using the exit()
function.
• Think of a situation where you are to come to a conclusion in your
program. You get the result and conclude something before the
whole program has been compiled or evaluated.
28
How do you terminate the program as soon as you get
your required information or result?
• The answer to the above question is using the built-in exit() function in
C++. So let us take a closer look at the function and how it works.
• exit( exit_value );
• firstly we took a user input for the number. We need to check whether this number, num is
prime or not.
• After that, we apply a for loop which works from 2 to n/2. This is because we already know
that all numbers are divisible by 1 as well as a number is indivisible by numbers above its half.
• Inside the for loop, we check whether the number is divisible by the loop iterator at that instant.
If so, we can directly conclude that the number is not prime and terminate the program using
the exit() function,
• Else, the loop goes on checking. After the execution of the whole loop structure, we declare the
number as a prime one.
34
Flow-diagram of exit statement in C++
35
Flow-diagram of exit statement in C++
• Post-increment operator:
A post-increment operator is used to increment the value of a variable
after executing the expression in which the operator is used. With the
post-increment operator, the value of the variable is first used in an
expression and then incremented.
int x = 10;
int a;
a = x++;
• The value of a will be 10 because the value of x is assigned to a and then
x is incremented
37
Post-decrement operator:
38
Prefix Operators
• Pre-increment operator:
A pre-increment operator is used to increment the value of a
variable before using it in a expression. With the pre-increment
operator, the value of the variable is first incremented and then
used in an expression.
int x = 10;
int a;
a = ++x;
• The value of a will be 11 because the value of x is incremented before it is
assigned to a. 39
Pre-decrement operator: