Professional Documents
Culture Documents
Looping
Looping
Don’t forget:
★ ***** You have to study the previous session before the next session *****
★ It’s better to show your face and open camera.
★ Raise your hand if you want to ask a questions.
★ Stick with discussion time and don’t interrupt the session there is a time for Q / A after every topic
★ Attending sessions is not optional and it’s a part of your graduation
★ You have to attend with PC or Laptop, Phones are not allowed anymore.
SESSION 1 OUTLINE
• Looping
• For Loop
• While Loop
• Do While Loop
• Nested Loop
• Break and Continue
• Exercises
• Nice To Search
NICE TO SEARCH NEXT SESSION
• While Loop
• Do-while
• Nestet Loop
LOOPING
In Programming, sometimes there is a need to perform some operation more than once or (say) n number of
times, Loops come into use when we need to repeatedly execute a block of statements.
• For example: Suppose we want to print “Hello World” 10 times ???
• We execute loops for numbers of times , while the condition is true or until it
becomes false.
• Initialization is executed (one time) before the execution of the code block.
• Condition This statement gets evaluated ahead of each execution
of the loop body, and abort the execution if the given condition get false.
• Increment / Decrements executed (every time) after the code block has been executed
and it gets executed after the loop body, ahead of the next condition evaluated.
• Ex / Take an integer then print all divisor of this number
• example1 user insert 8 then print 1 2 4 8
• example2 user insert 9 then print 1 3 9
While Loop
While Loop
● Repeat a block of code as long as the expression is true.
● Syntax:
#include <iostream>
while (Boolean Expression) { using namespace std;
// code to be executed
} void main()
{
int i = 1;
#include <iostream>
using namespace std;
void main() 11111
{ 22222
int i, j, n = 5; 33333
i = 1; 44444
while (i <= n)
{ 55555
j = 1;
while (j <= n)
{
cout << i;
j++;
}
cout << endl;
i++; 16
}
}
EXERCISES
18
BREAK
Break statement is used to end execution of the
nearest loop that the break statement lies in.
19
CONTINUE
Continue statement is used to pass the section of
code underneath it and make a shortcut to the
next iteration of the loop.
20
EXERCISES
• (Advanced) Print all numbers between 2 to n that has more than 2 divisors. 4 <= n <= 100
• Let the user insert infinity numbers then sum all numbers except 50 and stop when the user inserts negative
numbers and print sum.
MEMORY SCOPE
• C++ is a block language. { … }
• Variables declared in a block are called local variables.
• Variables declared outside the block, but not within another inner block, are
called global variables.
{
int x;
const double pi = 3.14;
x = 1;
x {
pi int p = 4;
p x = p;
cout << x << p;
}
int y = 12;
y cout << x << y << pi;
}
SCOPE
23
LOCAL VARIABLES
24
WHAT IS THE OUTPUT OF THE FOLLOWING SEGMENTS?
#include <iostream>
using namespace std;
int main()
{
int x = 5, y = 3;
cout << x + y << endl;
{
int y = 12;
cout << x + y << endl;
}
cout << x + y << endl;
return 0;
} 25
SCOPE
Correct Wrong
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int i;
for(i=0; i<10; i++) for(int i=0; i<10; i++)
{ i {
i cout << i; cout << i;
} }
cout << i; cout << i; undeclared
return 0; return 0; identifier
} }
27
NICE TO SEARCH NEXT SESSION
• Array
• Array 1 D
• Array 2 D