SlideShare a Scribd company logo
1 of 51
Introduction to Computers
Lab 8
First Year (2016– 2017)
Introduction to Computers Lab First Year 2016– 2017
1
Agenda
• Repetition Structures (Loops)
1) WHILE Loop
2) DO .. WHILE Loop
3) FOR Loop
• Introduction to Visual Studio.
• C++ Programs.
2
Repetition Structures
(Loops)
3
Repetition Structure
• A repetition structure represents part of the
program that repeats. This type of structure is
commonly known as a loop.
4
Repetition Structure
• Notice the use of the diamond symbol. A loop tests a
condition, and if the condition exists, it performs an
action. Then it tests the condition again. If the
condition still exists, the action is repeated. This
continues until the condition no longer exists.
5
Repetition Structure
• In the flowchart segment, the question “is x < y?” is
asked.
• If the answer is yes, then Process A is performed.
• The question “is x < y?” is asked again. Process A is
repeated as long as x is less than y.
• When x is no longer less than y, the repetition stops
and the structure is exited.
x < y? Process A
YES
6
NO
Repetition Structure
• The flowchart segment below shows a
repetition structure expressed in C++ as a
while loop.
while (x < y)
x++;
Flowchart C++ Code
x < y? Add 1 to x
YES
7
NO
Controlling a Repetition
Structure
• The action performed by a repetition structure
must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
• In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
x < y? Display x
YES
8
QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop? NO
Controlling a Repetition
Structure
• ANSWER: By adding an action within
the repetition that changes the value of x.
x < y? Display x Add 1 to x
YES
9
NO
A Pre-Test Repetition
Structure
• This type of structure is known as a pre-test
repetition structure. The condition is tested
BEFORE any actions are performed.
x < y? Display x Add 1 to x
YES
10
NO
A Pre-Test Repetition
Structure
• In a pre-test repetition structure, if the
condition does not exist, the loop will
never begin.
x < y? Display x Add 1 to x
YES
11
NO
A Post-Test Repetition
Structure
• This flowchart segment shows a post-
test repetition structure.
• The condition is tested AFTER the
actions are performed.
• A post-test repetition structure always
performs its actions at least once.
Display x
Add 1 to x
YES
x < y?
12
NO
A Post-Test Repetition
Structure
• The flowchart segment below shows a post-test
repetition structure expressed in C++ as a do-while
loop.
do
{
cout << x << endl;
x++;
} while (x < y);
Flowchart
C++ Code
Display x
Add 1 to x
YES
x < y?
13
NO
WHILE Expression
Action
ENDWHILE
1. WHILE Loop
Logical expression that
determine whether the
action is to be executed
Action to be
iteratively
performed until
logical expression
is false 14
Expression
Action
true
false
Expression is
evaluated at the start
of each iteration of
the loop
If expression is
false, program
execution
continues with next
statement outside
the loop
If expression is
true ,Action is
executed
15
Exercise 1
• Write down an algorithm and draw a flowchart to find and
print the largest of N numbers.
16
Print max
Set max = x ,
counter = 1
counter<n
Input x
counter=counter+1
x>max
START
max = x
True
Stop
Input n , x
True
false
false
17
Exercise 2
• Write a program that prompts the user to enter two positive
integers and finds their greatest common divisor.
• The greatest common divisor of integers 16 and 24 is 8.
So , how do you find the greatest common divisor ?
• Let the two input integers be n1 and n2. Number 1 is a
common divisor, but it may not be the greatest commons
divisor.
• So, you can check whether k (for k = 2, 3, 4, and so on) is
a common divisor for n1 and n2, until k is greater than n1
or n2.
18
START
Input N1,N2
gcd =1 , K =2
K<=N1
&&
K<=N2
Stop
gcd=K
K+1
Print gcd
False
True
19
N1%K==0
&&
N2%K==0
False
True
2. Do .. While
DO
Action
WHILE Expression
ENDWHILE
Execute Action
If Expression is true then
execute Action again ,
Repeat this process until
Expression evaluates to
false
20
Action
true
false
Expressio
n
Action
Expression
Execute Action first
Expression is evaluated at
the End , If Expression is
true then execute Action
again , Repeat this process
until Expression evaluates to
false
21
Exercise 3
• Write down an algorithm and draw a flowchart to convert from
Positive decimal number to binary number.
22
START
Input N
remainder= N% 2
N = N/2
Print remainder
N <> 0
END
false
True
23
X=1
binaryNum = 0
binaryNum += remainder*x
X *= 10
3. For Loop
FOR( initialization , condition , increment )
statements
ENDFOR
The increment portion is
executed at the end of
each iteration
The statements are executed until
this condition becomes false
The initialization is executed once
before the loop begins
24
Condition is evaluated at the start
of each iteration of the loop
If condition is true the
Action is executed
Initialization
Action
Increment
Condition
Executed once at the beginning of
for loop
True
False
If condition is false ,
program execution
continues with next
statements.
After the action has
completed , the increment is
evaluated , then the next
iteration of the loop starts.
25
Exercise 4
• Draw a flowchart to find factorial N: N!
N! = N-1 * N-2 * N-1 …. 1
For example: 5!
5 * 4 * 3 * 2 * 1 = 120.
26
START
Input num
Stop
result=1
i=1
result = result * i
True
i+1
i<=num
Print result
False
27
Introduction to Visual Studio
28
What is Visual Studio?
• Visual Studio is a complete set of development tools for
building ASP.NET Web applications, XML Web Services,
desktop applications, and mobile applications. Visual
Basic, Visual C++, Visual C#, and Visual J#.
29
Containers
( Solutions and Projects )
• Visual Studio provides two containers to help you
efficiently manage the items that are required by your
development effort, such as references, data
connections, folders, and files. These containers are
called solutions and projects.
30
Solutions as Containers
• A solution can contain multiple projects and a project
typically contains multiple items (Items can be files and
other parts of your project such as references, data
connections, or folders).
• Visual Studio automatically generates a solution when
you create a new project. As needed, you can then add
other projects to the solution.
31
Projects as Containers
• Visual Studio projects are used as containers within a
solution to logically manage, build, and debug the items
that comprise your application.
• The output of a project is usually an executable program
(.exe), a dynamic-link library (.dll) file or a module,
among others
32
Program components
• Header Files : (file.h) contain constant, variable, and
function declarations needed by a program.
• Source Files : (file.cpp) consists of the program
statements comprising a C++ or other programming
language program.
Break 10 Minutes 
34
C++ Programs
35
Exercise 1
• Algorithm for program that reads a number and
determines whether it is positive, negative or zero.
36
Exercise 1: Solution
#include<iostream>
using namespace std;
int main()
{
int number ;
cout<<"Please enter number: ";
cin>> number;
if (number>0)
cout<< "Number is positiven";
else if(number == 0)
cout<< "Number is zeron";
else
cout<< "Number is Negativen";
return 0;
}
37
Exercise 2
• Algorithm for program that reads a number and
determines whether it is even or odd.
38
Exercise 2: Solution
#include<iostream>
using namespace std;
int main()
{
int number ;
cout<<"Please enter number: ";
cin>> number;
if (number%2 == 0)
cout<< "Number is evenn";
else
cout<< "Number is oddn";
return 0;
}
39
Exercise 3
• Algorithm for program that checks whether an input
number lies between specified range. For example: If
user enters range (20, 50) and queried number 34, the
program should display “In range”. On the other hand, if
he enters 76, the program should display “Not in
Range”.
40
Exercise 3: Solution
#include<iostream>
using namespace std;
int main()
{
int start,end,number;
cout<<"Please enter range: ";
cin>> start >> end;
cout<<"Please enter number: ";
cin>> number;
if(number >= start )
{
if (number <= end)
cout<<"In rangen";
else
cout<<"Not in rangen";
}
else
cout<<"Not in rangen";
return 0;
}
41
Exercise 3: Another Solution
#include<iostream>
using namespace std;
int main()
{
int s,e,number;
cout<<"Please enter range: ";
cin>> s >> e;
cout<<"Please enter number: ";
cin>> number;
if(number>= s && number <= e)
cout<<"In rangen";
else
cout<<"Not in rangen";
return 0;
}
42
Exercise 4
• Algorithm for program that determines whether a baby’s
weight is normal or not. For girls, normal babies weight
are 2.5 to 4.5 KG. On the other hand, for boys the
normal weights are 4 to 5.5 KG.
43
Exercise 4: Solution
#include<iostream>
using namespace std;
int main()
{
char gender;
float weight;
cout<<"Please enter gender(m | F): ";
cin>> gender;
cout<<"Please enter weight : ";
cin>> weight;
if ((gender == 'F'|| gender == 'f') && weight >=2.5 && weight <=4.5)
cout<<"Normaln";
else if((gender == 'M'|| gender == 'm') && weight >=4 && weight <=5.5)
cout<<"Normaln";
else
cout <<"not normal n";
return 0;
}
44
Exercise 5
• Write an algorithm for electricity company, which charges
customers according to their usage rank (1,2 or 3) and
reading (watt). In rank 1, customers pay 1 L.E./100 watt
with minimum 10 L.E. In rank 2, customers pay 2
L.E./100 watt, but pay at maximum 400 L.E. In rank 3,
customers pay 2.5 L.E./100 watt. Program reads the
usage rank and reading and displays the charged
amount.
45
#include<iostream>
using namespace std;
int main()
{
int rank , reading;
cout <<"Enter rank: ";
cin>> rank;
cout<<"Enter Reading watt: ";
cin>> reading;
float price = 0;
switch (rank)
{
case 1:
price = (reading/100.0) *1 ;
if(price < 10)
` cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anthor rankn";
break;
case 2:
price = (reading/100.0)*2;
if(price <= 400)
cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anther rankn";
break;
case 3:
price = (reading/100.0)*2.5;
if(price > 400)
cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anther rankn";
break;
default:
break;
}
return 0;
}
Exercise 5: Solution
46
Exercise 6
• Write a program for a calculator that works on integer
numbers. The user enters two numbers to perform only
one of three basic arithmetic operations (+, -, * and /).
• The interaction with the user might look like this: Enter
your Fractional expression: 1 + 2 The result = 3
• Hint: The user is allowed to do only one operation at a
time.
47
Exercise 6: Solution
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op)
{ case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default: // If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
48
Lab Exercise
• A program to swap the values of two integers
using third variable.
49
Lab Exercise: Solution
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
50
Thank You 
51

More Related Content

Similar to Introduction to Computers Lab Loops and Visual Studio

COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilizationJAYDEV PATEL
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4Vishal Patil
 
White Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR ChandigarhWhite Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR ChandigarhPankaj Thakur
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0PMILebanonChapter
 
Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Ha Nguyen
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
more loops lecture by Professor Evan korth
more loops  lecture by Professor Evan korth more loops  lecture by Professor Evan korth
more loops lecture by Professor Evan korth hammad ali
 

Similar to Introduction to Computers Lab Loops and Visual Studio (20)

Repair dagstuhl jan2017
Repair dagstuhl jan2017Repair dagstuhl jan2017
Repair dagstuhl jan2017
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
C# Loops
C# LoopsC# Loops
C# Loops
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
Lecture 7.pptx
Lecture 7.pptxLecture 7.pptx
Lecture 7.pptx
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
White Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR ChandigarhWhite Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR Chandigarh
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Debug - MITX60012016-V005100
Debug - MITX60012016-V005100
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
03b loops
03b   loops03b   loops
03b loops
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
more loops lecture by Professor Evan korth
more loops  lecture by Professor Evan korth more loops  lecture by Professor Evan korth
more loops lecture by Professor Evan korth
 

More from ShimoFcis

Motif Finding.pdf
Motif Finding.pdfMotif Finding.pdf
Motif Finding.pdfShimoFcis
 
05_SQA_Overview.ppt
05_SQA_Overview.ppt05_SQA_Overview.ppt
05_SQA_Overview.pptShimoFcis
 
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptxTopic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptxShimoFcis
 
Lab-11-C-Problems.pptx
Lab-11-C-Problems.pptxLab-11-C-Problems.pptx
Lab-11-C-Problems.pptxShimoFcis
 
Mid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptxMid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptxShimoFcis
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptxShimoFcis
 
Lecture Cloud Security.pptx
Lecture Cloud Security.pptxLecture Cloud Security.pptx
Lecture Cloud Security.pptxShimoFcis
 
mapreduce.pptx
mapreduce.pptxmapreduce.pptx
mapreduce.pptxShimoFcis
 
storage-systems.pptx
storage-systems.pptxstorage-systems.pptx
storage-systems.pptxShimoFcis
 
mapreduce-advanced.pptx
mapreduce-advanced.pptxmapreduce-advanced.pptx
mapreduce-advanced.pptxShimoFcis
 

More from ShimoFcis (11)

Motif Finding.pdf
Motif Finding.pdfMotif Finding.pdf
Motif Finding.pdf
 
05_SQA_Overview.ppt
05_SQA_Overview.ppt05_SQA_Overview.ppt
05_SQA_Overview.ppt
 
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptxTopic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
 
4-DES.pdf
4-DES.pdf4-DES.pdf
4-DES.pdf
 
Lab-11-C-Problems.pptx
Lab-11-C-Problems.pptxLab-11-C-Problems.pptx
Lab-11-C-Problems.pptx
 
Mid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptxMid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptx
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Lecture Cloud Security.pptx
Lecture Cloud Security.pptxLecture Cloud Security.pptx
Lecture Cloud Security.pptx
 
mapreduce.pptx
mapreduce.pptxmapreduce.pptx
mapreduce.pptx
 
storage-systems.pptx
storage-systems.pptxstorage-systems.pptx
storage-systems.pptx
 
mapreduce-advanced.pptx
mapreduce-advanced.pptxmapreduce-advanced.pptx
mapreduce-advanced.pptx
 

Recently uploaded

Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad EscortsCall girls in Ahmedabad High profile
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 

Recently uploaded (20)

Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts
(ISHITA) Call Girls Service Hyderabad Call Now 8617697112 Hyderabad Escorts
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 

Introduction to Computers Lab Loops and Visual Studio

  • 1. Introduction to Computers Lab 8 First Year (2016– 2017) Introduction to Computers Lab First Year 2016– 2017 1
  • 2. Agenda • Repetition Structures (Loops) 1) WHILE Loop 2) DO .. WHILE Loop 3) FOR Loop • Introduction to Visual Studio. • C++ Programs. 2
  • 4. Repetition Structure • A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop. 4
  • 5. Repetition Structure • Notice the use of the diamond symbol. A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists. 5
  • 6. Repetition Structure • In the flowchart segment, the question “is x < y?” is asked. • If the answer is yes, then Process A is performed. • The question “is x < y?” is asked again. Process A is repeated as long as x is less than y. • When x is no longer less than y, the repetition stops and the structure is exited. x < y? Process A YES 6 NO
  • 7. Repetition Structure • The flowchart segment below shows a repetition structure expressed in C++ as a while loop. while (x < y) x++; Flowchart C++ Code x < y? Add 1 to x YES 7 NO
  • 8. Controlling a Repetition Structure • The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created. • In this flowchart segment, x is never changed. Once the loop starts, it will never end. x < y? Display x YES 8 QUESTION: How can this flowchart be modified so it is no longer an infinite loop? NO
  • 9. Controlling a Repetition Structure • ANSWER: By adding an action within the repetition that changes the value of x. x < y? Display x Add 1 to x YES 9 NO
  • 10. A Pre-Test Repetition Structure • This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed. x < y? Display x Add 1 to x YES 10 NO
  • 11. A Pre-Test Repetition Structure • In a pre-test repetition structure, if the condition does not exist, the loop will never begin. x < y? Display x Add 1 to x YES 11 NO
  • 12. A Post-Test Repetition Structure • This flowchart segment shows a post- test repetition structure. • The condition is tested AFTER the actions are performed. • A post-test repetition structure always performs its actions at least once. Display x Add 1 to x YES x < y? 12 NO
  • 13. A Post-Test Repetition Structure • The flowchart segment below shows a post-test repetition structure expressed in C++ as a do-while loop. do { cout << x << endl; x++; } while (x < y); Flowchart C++ Code Display x Add 1 to x YES x < y? 13 NO
  • 14. WHILE Expression Action ENDWHILE 1. WHILE Loop Logical expression that determine whether the action is to be executed Action to be iteratively performed until logical expression is false 14
  • 15. Expression Action true false Expression is evaluated at the start of each iteration of the loop If expression is false, program execution continues with next statement outside the loop If expression is true ,Action is executed 15
  • 16. Exercise 1 • Write down an algorithm and draw a flowchart to find and print the largest of N numbers. 16
  • 17. Print max Set max = x , counter = 1 counter<n Input x counter=counter+1 x>max START max = x True Stop Input n , x True false false 17
  • 18. Exercise 2 • Write a program that prompts the user to enter two positive integers and finds their greatest common divisor. • The greatest common divisor of integers 16 and 24 is 8. So , how do you find the greatest common divisor ? • Let the two input integers be n1 and n2. Number 1 is a common divisor, but it may not be the greatest commons divisor. • So, you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2. 18
  • 19. START Input N1,N2 gcd =1 , K =2 K<=N1 && K<=N2 Stop gcd=K K+1 Print gcd False True 19 N1%K==0 && N2%K==0 False True
  • 20. 2. Do .. While DO Action WHILE Expression ENDWHILE Execute Action If Expression is true then execute Action again , Repeat this process until Expression evaluates to false 20
  • 21. Action true false Expressio n Action Expression Execute Action first Expression is evaluated at the End , If Expression is true then execute Action again , Repeat this process until Expression evaluates to false 21
  • 22. Exercise 3 • Write down an algorithm and draw a flowchart to convert from Positive decimal number to binary number. 22
  • 23. START Input N remainder= N% 2 N = N/2 Print remainder N <> 0 END false True 23 X=1 binaryNum = 0 binaryNum += remainder*x X *= 10
  • 24. 3. For Loop FOR( initialization , condition , increment ) statements ENDFOR The increment portion is executed at the end of each iteration The statements are executed until this condition becomes false The initialization is executed once before the loop begins 24
  • 25. Condition is evaluated at the start of each iteration of the loop If condition is true the Action is executed Initialization Action Increment Condition Executed once at the beginning of for loop True False If condition is false , program execution continues with next statements. After the action has completed , the increment is evaluated , then the next iteration of the loop starts. 25
  • 26. Exercise 4 • Draw a flowchart to find factorial N: N! N! = N-1 * N-2 * N-1 …. 1 For example: 5! 5 * 4 * 3 * 2 * 1 = 120. 26
  • 27. START Input num Stop result=1 i=1 result = result * i True i+1 i<=num Print result False 27
  • 29. What is Visual Studio? • Visual Studio is a complete set of development tools for building ASP.NET Web applications, XML Web Services, desktop applications, and mobile applications. Visual Basic, Visual C++, Visual C#, and Visual J#. 29
  • 30. Containers ( Solutions and Projects ) • Visual Studio provides two containers to help you efficiently manage the items that are required by your development effort, such as references, data connections, folders, and files. These containers are called solutions and projects. 30
  • 31. Solutions as Containers • A solution can contain multiple projects and a project typically contains multiple items (Items can be files and other parts of your project such as references, data connections, or folders). • Visual Studio automatically generates a solution when you create a new project. As needed, you can then add other projects to the solution. 31
  • 32. Projects as Containers • Visual Studio projects are used as containers within a solution to logically manage, build, and debug the items that comprise your application. • The output of a project is usually an executable program (.exe), a dynamic-link library (.dll) file or a module, among others 32
  • 33. Program components • Header Files : (file.h) contain constant, variable, and function declarations needed by a program. • Source Files : (file.cpp) consists of the program statements comprising a C++ or other programming language program.
  • 36. Exercise 1 • Algorithm for program that reads a number and determines whether it is positive, negative or zero. 36
  • 37. Exercise 1: Solution #include<iostream> using namespace std; int main() { int number ; cout<<"Please enter number: "; cin>> number; if (number>0) cout<< "Number is positiven"; else if(number == 0) cout<< "Number is zeron"; else cout<< "Number is Negativen"; return 0; } 37
  • 38. Exercise 2 • Algorithm for program that reads a number and determines whether it is even or odd. 38
  • 39. Exercise 2: Solution #include<iostream> using namespace std; int main() { int number ; cout<<"Please enter number: "; cin>> number; if (number%2 == 0) cout<< "Number is evenn"; else cout<< "Number is oddn"; return 0; } 39
  • 40. Exercise 3 • Algorithm for program that checks whether an input number lies between specified range. For example: If user enters range (20, 50) and queried number 34, the program should display “In range”. On the other hand, if he enters 76, the program should display “Not in Range”. 40
  • 41. Exercise 3: Solution #include<iostream> using namespace std; int main() { int start,end,number; cout<<"Please enter range: "; cin>> start >> end; cout<<"Please enter number: "; cin>> number; if(number >= start ) { if (number <= end) cout<<"In rangen"; else cout<<"Not in rangen"; } else cout<<"Not in rangen"; return 0; } 41
  • 42. Exercise 3: Another Solution #include<iostream> using namespace std; int main() { int s,e,number; cout<<"Please enter range: "; cin>> s >> e; cout<<"Please enter number: "; cin>> number; if(number>= s && number <= e) cout<<"In rangen"; else cout<<"Not in rangen"; return 0; } 42
  • 43. Exercise 4 • Algorithm for program that determines whether a baby’s weight is normal or not. For girls, normal babies weight are 2.5 to 4.5 KG. On the other hand, for boys the normal weights are 4 to 5.5 KG. 43
  • 44. Exercise 4: Solution #include<iostream> using namespace std; int main() { char gender; float weight; cout<<"Please enter gender(m | F): "; cin>> gender; cout<<"Please enter weight : "; cin>> weight; if ((gender == 'F'|| gender == 'f') && weight >=2.5 && weight <=4.5) cout<<"Normaln"; else if((gender == 'M'|| gender == 'm') && weight >=4 && weight <=5.5) cout<<"Normaln"; else cout <<"not normal n"; return 0; } 44
  • 45. Exercise 5 • Write an algorithm for electricity company, which charges customers according to their usage rank (1,2 or 3) and reading (watt). In rank 1, customers pay 1 L.E./100 watt with minimum 10 L.E. In rank 2, customers pay 2 L.E./100 watt, but pay at maximum 400 L.E. In rank 3, customers pay 2.5 L.E./100 watt. Program reads the usage rank and reading and displays the charged amount. 45
  • 46. #include<iostream> using namespace std; int main() { int rank , reading; cout <<"Enter rank: "; cin>> rank; cout<<"Enter Reading watt: "; cin>> reading; float price = 0; switch (rank) { case 1: price = (reading/100.0) *1 ; if(price < 10) ` cout<<"Total cost is: "<<price<<endl; else cout<<"The custmer is in anthor rankn"; break; case 2: price = (reading/100.0)*2; if(price <= 400) cout<<"Total cost is: "<<price<<endl; else cout<<"The custmer is in anther rankn"; break; case 3: price = (reading/100.0)*2.5; if(price > 400) cout<<"Total cost is: "<<price<<endl; else cout<<"The custmer is in anther rankn"; break; default: break; } return 0; } Exercise 5: Solution 46
  • 47. Exercise 6 • Write a program for a calculator that works on integer numbers. The user enters two numbers to perform only one of three basic arithmetic operations (+, -, * and /). • The interaction with the user might look like this: Enter your Fractional expression: 1 + 2 The result = 3 • Hint: The user is allowed to do only one operation at a time. 47
  • 48. Exercise 6: Solution # include <iostream> using namespace std; int main() { char op; float num1, num2; cout << "Enter operator either + or - or * or /: "; cin >> op; cout << "Enter two operands: "; cin >> num1 >> num2; switch(op) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': cout << num1/num2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } return 0; } 48
  • 49. Lab Exercise • A program to swap the values of two integers using third variable. 49
  • 50. Lab Exercise: Solution #include <iostream> using namespace std; int main() { int a = 5, b = 10, temp; cout << "Before swapping." << endl; cout << "a = " << a << ", b = " << b << endl; temp = a; a = b; b = temp; cout << "nAfter swapping." << endl; cout << "a = " << a << ", b = " << b << endl; return 0; } 50

Editor's Notes

  1. The output should be reversed