SlideShare a Scribd company logo
1 of 10
C++ Programming
Conditions and loops
Mohammed Jehan
Lecturer
The While Statement
A loop repeatedly executes a set of statements until a particular condition is satisfied.
A while loop statement repeatedly executes a target statement as long as a given
condition remains true.
while (condition) {
statement(s);
}
The loop iterates while the condition is true. At the point when the condition becomes
false, program control is shifted to the line that immediately follows the loop.
The while loop
The loop's body is the block of statements within curly braces.
For example:
int num = 1;
while (num < 6)
{
cout << "Number: " << num << endl;
num = num + 1;
}
The while loop
#include <iostream>
using namespace std;
int main()
{
int num = 1;
while (num < 6)
{
cout << "Number: " << num << endl;
num = num + 1; }
return 0;
}
Using Increment or Decrement in while loop
The increment or decrement operators can be used to change values in the loop.
For example: int num = 1;
while (num < 6) {
cout << "Number: " << num << endl;
num++;
}
num++ is equivalent to num = num + 1.
Obtaining multiple values from the User
A loop can be used to obtain multiple inputs from the user.
Let's create a program that allows the user to enter a number 5 times, each time storing the input
in a variable.
int num = 1;
int number;
while (num <= 5) {
cin >> number;
num++;
}
The above code asks for user input 5 times, and each time saves the input in the number variable.
Multiple values in While Loop
Now let's modify our code to calculate the sum of the numbers the user has entered.
int num = 1;
int number;
int total = 0;
while (num <= 5) {
cin >> number;
total += number;
num++;
}
cout << total << endl;
Multiple values in While Loop
Now let's modify our code to calculate the sum of the numbers the user has entered.
int num = 1;
int number;
int total = 0;
while (num <= 5) {
cin >> number;
total += number;
num++;
}
cout << total << endl;
The code above adds the number entered by the user to the total variable with each loop
iteration. Once the loop stops executing, the value of total is printed. This value is the sum of all
of the numbers the user entered. Note that the variable total has an initial value of 0
Multiple values in While Loop
Now let's modify our code to calculate the sum of the numbers the user has entered.
int num = 1;
int number;
int total = 0;
while (num <= 5) {
cin >> number;
total += number;
num++;
}
cout << total << endl;
The code above adds the number entered by the user to the total variable with each loop
iteration. Once the loop stops executing, the value of total is printed. This value is the sum of all
of the numbers the user entered. Note that the variable total has an initial value of 0
THANK YOU

More Related Content

What's hot

c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2sajidpk92
 
c plus plus programsSlide
c plus plus programsSlidec plus plus programsSlide
c plus plus programsSlideharman kaur
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zSyed Umair
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structureRumman Ansari
 
Implement ERC20 on TestRPC
Implement ERC20 on TestRPCImplement ERC20 on TestRPC
Implement ERC20 on TestRPCKC Tam
 
Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...hwbloom42
 
Assignment#1
Assignment#1Assignment#1
Assignment#1NA000000
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Pythonprimeteacher32
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of RecursionRicha Sharma
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimizedWoody Pewitt
 
Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends? ?
 

What's hot (20)

Basic constructs ii
Basic constructs  iiBasic constructs  ii
Basic constructs ii
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
c plus plus programsSlide
c plus plus programsSlidec plus plus programsSlide
c plus plus programsSlide
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.z
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Implement ERC20 on TestRPC
Implement ERC20 on TestRPCImplement ERC20 on TestRPC
Implement ERC20 on TestRPC
 
Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...
 
Assignment#1
Assignment#1Assignment#1
Assignment#1
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Inc decsourcefile
Inc decsourcefileInc decsourcefile
Inc decsourcefile
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223
 
applet.docx
applet.docxapplet.docx
applet.docx
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
 

Similar to Lecture 3

C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptxNaumanRasheed11
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8REHAN IJAZ
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptxAqeelAbbas94
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.pptsanjay
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05IIUM
 

Similar to Lecture 3 (20)

C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Control structures
Control structuresControl structures
Control structures
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 

More from Mohammed Khan

Module 11 therapeutic intervention play yoga
Module 11 therapeutic  intervention  play yogaModule 11 therapeutic  intervention  play yoga
Module 11 therapeutic intervention play yogaMohammed Khan
 
Module 10 principles of learning, practice, reinforcement understanding spec...
Module 10 principles of learning, practice, reinforcement understanding  spec...Module 10 principles of learning, practice, reinforcement understanding  spec...
Module 10 principles of learning, practice, reinforcement understanding spec...Mohammed Khan
 
Module 3 special education
Module 3 special educationModule 3 special education
Module 3 special educationMohammed Khan
 

More from Mohammed Khan (17)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Module 11 therapeutic intervention play yoga
Module 11 therapeutic  intervention  play yogaModule 11 therapeutic  intervention  play yoga
Module 11 therapeutic intervention play yoga
 
Module 10 principles of learning, practice, reinforcement understanding spec...
Module 10 principles of learning, practice, reinforcement understanding  spec...Module 10 principles of learning, practice, reinforcement understanding  spec...
Module 10 principles of learning, practice, reinforcement understanding spec...
 
Module 9 speci ed
Module 9 speci edModule 9 speci ed
Module 9 speci ed
 
Module 8 spec ed
Module 8 spec edModule 8 spec ed
Module 8 spec ed
 
Module 7 special ed
Module 7  special edModule 7  special ed
Module 7 special ed
 
Module 6 spec ed
Module 6 spec edModule 6 spec ed
Module 6 spec ed
 
Module 5 special ed
Module 5 special edModule 5 special ed
Module 5 special ed
 
Module 4 spec needs
Module 4 spec needsModule 4 spec needs
Module 4 spec needs
 
Module 3 special education
Module 3 special educationModule 3 special education
Module 3 special education
 
Module 2 special ed
Module 2 special edModule 2 special ed
Module 2 special ed
 
Module 1 special ed
Module 1 special edModule 1 special ed
Module 1 special ed
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Lecture 3

  • 1. C++ Programming Conditions and loops Mohammed Jehan Lecturer
  • 2. The While Statement A loop repeatedly executes a set of statements until a particular condition is satisfied. A while loop statement repeatedly executes a target statement as long as a given condition remains true. while (condition) { statement(s); } The loop iterates while the condition is true. At the point when the condition becomes false, program control is shifted to the line that immediately follows the loop.
  • 3. The while loop The loop's body is the block of statements within curly braces. For example: int num = 1; while (num < 6) { cout << "Number: " << num << endl; num = num + 1; }
  • 4. The while loop #include <iostream> using namespace std; int main() { int num = 1; while (num < 6) { cout << "Number: " << num << endl; num = num + 1; } return 0; }
  • 5. Using Increment or Decrement in while loop The increment or decrement operators can be used to change values in the loop. For example: int num = 1; while (num < 6) { cout << "Number: " << num << endl; num++; } num++ is equivalent to num = num + 1.
  • 6. Obtaining multiple values from the User A loop can be used to obtain multiple inputs from the user. Let's create a program that allows the user to enter a number 5 times, each time storing the input in a variable. int num = 1; int number; while (num <= 5) { cin >> number; num++; } The above code asks for user input 5 times, and each time saves the input in the number variable.
  • 7. Multiple values in While Loop Now let's modify our code to calculate the sum of the numbers the user has entered. int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl;
  • 8. Multiple values in While Loop Now let's modify our code to calculate the sum of the numbers the user has entered. int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; The code above adds the number entered by the user to the total variable with each loop iteration. Once the loop stops executing, the value of total is printed. This value is the sum of all of the numbers the user entered. Note that the variable total has an initial value of 0
  • 9. Multiple values in While Loop Now let's modify our code to calculate the sum of the numbers the user has entered. int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; The code above adds the number entered by the user to the total variable with each loop iteration. Once the loop stops executing, the value of total is printed. This value is the sum of all of the numbers the user entered. Note that the variable total has an initial value of 0