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

PROGRAMMING WITH C++

BY/ MOHAMED HASHEM


SESSION RULES

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 will use Repetition / Looping !!


It’s referred to the ability of repeating a statement or a set of statements as many times.
LOOP
C++ programming language provides the following type of loops to handle looping requirements.
• for loop
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
• while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before
executing the loop body.
• do...while
loop Like a ‘while’ statement, except that it tests the condition at the end of the loop body.
• nested loops
You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop.
FOR - LOOP

• 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;

while (<Condition>){ while (i < 11)


{
<Loop body> ; i += 3;
} cout << i << endl;
} 8
}
HOW IT WORKS
EXERCISES
What is the output of the following segments?
(a) int i = -3;
while (i != 3)
{
cout << i << ” ”;
i = i + 1;
}

(b) int i = 0, sum = 0;


while (i <= 10)
{
sum += i;
i++;
}
cout << ”Sum = ” << sum;
EXERCISES
• Print all numbers from 1 to 9?
• Print all numbers between 1 to n that aren't divisible by 5.
• Print the odd numbers from 1 to 101, and also to display their sum.
• Check if the number is prime or not
• Print the digits of an integer

• (Advanced) Write a program to calculate the sum of: 12 + 22 + 32 + … + Ν2


DO WHILE
• The do/while loop is a variant of the while loop.
This loop will execute the code block once, before
checking if the condition is true, then it will repeat
the loop as long as the condition is true.

• Even though the condition is false, the body


Is executed once.
WHILE VS DO WHILE
EXERCISES

● Print the max of 10 integers


● Reverse an integer
INFINITE LOOP
NESTED LOOPS
A loop within another loop is called a nested loop

#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

• Print each day of a week for 3 weeks.


• Print Square
****
****
****
****
THE BREAK AND CONTINUE
STATEMENTS
• A break statement is used to “break” out of a loop or a switch
statement. When it is executed, it causes the flow of control to
immediately exit the innermost switch statement or loop.

• A continue statement can only be used inside loops and it


causes the execution to skip to the end of the loop, ready to
start a new insertion.

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

● A scope is a region in the program that


has a start and an end.

● The scope’s start and end is determined by


two braces { }.

● The outer scope is called the Global


Scope.

23
LOCAL VARIABLES

● Variables that are declared inside a certain


scope are called local variables.

● The local variable can only be used inside


it’s scope.

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
} }

for(int i=0; i<10; i++)


Wrong 26
cout << i;
cout << i;
GLOBAL VARIABLES

● Global variables are defined outside all the


program functions.

● Global variables can be used in the entire


program.

● It’s not preferred to use global variables.*

27
NICE TO SEARCH NEXT SESSION

• Array
• Array 1 D
• Array 2 D

You might also like