SlideShare une entreprise Scribd logo
1  sur  20
FINAL REQUIREMENT
   Programming




     http://eglobiotraining.com
Topics in Programming
1. Looping

2. Switch Case




                 http://eglobiotraining.com
SWITCH CASE STATEMENT
• In programming, a switch, case, select or inspect statement
   is a type of selection control mechanism that exists in
  most imperative programming languages such
  as Pascal, Ada, C/C++, C#, Java, and so on. It is also included
  in several other types of Programming languages. Its
  purpose is to allow the value of a variable or expression to
  control the flow of program execution via a multiway
  branch (or "go to", one of several labels). The main reasons
  for using a switch include improving clarity, by reducing
  otherwise repetitive coding, and (if the heuristics permit)
  also offering the potential for faster execution through
  easier compiler optimization in many cases.



                         http://eglobiotraining.com
SWITCH CASE STATEMENT
• Switch case statements are a substitute for long if statements that
  compare a variable to several "integral" values ("integral" values are
  simply values that can be expressed as an integer, such as the value
  of a char). The basic format for using the switch case in the
  programming is outlined below. The value of the variable given into
  switch is compared to the value following each of the cases, and
  when one value matches the value of the variable, the computer
  continues executing the program from that point.
• The switch-case statement is a multi-way decision statement.
  Unlike the multiple decision statement that can be created using if-
  else, the switch statement evaluates the conditional expression and
  tests it against numerous constant values. The branch
  corresponding to the value that the expression matches is taken
  during execution.



                            http://eglobiotraining.com
SWITCH CASE STATEMENT
• Switch is used to choose a fragment of template
  depending on the value of an expression
• This has a similar function as the If condition - but it is
  more useful in situations when there is many possible
  values for the variable. Switch will evaluate one of
  several statements, depending on the value of a given
  variable. If no given value matches the variable, the
  default statement is executed.
• The value of the expressions in a switch-case statement
  must be an ordinal type
  i.e. integer, char, short, long, etc. Float and double are
  not allowed.
                        http://eglobiotraining.com
Example of switch case IN C
               PROGRAMMING
#include <stdlib.h>
using namespace std;
void welcome();
char getChar();
void displayResponse(char choice);
int main(int argc, char *argv[])
{
  char choice; // declares the choice variable
  welcome(); // This calls the welcome function
  choice = getChar(); // calls getChar and returns the value for choice
  displayResponse(choice); // passes choice to displayResponse function

  system("PAUSE");
  return 0;
} // end main
// welcome function displays an opening message to
// explain the program to the user
void welcome()
{
  cout << "This program displays different messages dependingn";
  cout << "on which letter is entered by the user.n";
  cout << "Pick a letter a, b, c or d to see whatn";
  cout << "the program will say.nn";
} // end of welcome function
// getChar asks the user for a letter a, b or c.
// The character is returned to where the function was called.
char getChar()
{
    char response; // declares variable called response
  cout << "Please type a letter a, b, c and d: "; // prompt for letter
  cin >> response; // gets input from user and assigns it to response
  return response; // sends back the response value
} // end getChar function
// displayResponse function takes the char variable and uses it
// to determine which set of tasks will be performed.
void displayResponse(char choice)
{
  char again;                                                             http://eglobiotraining.com
Running switch case IN C
   PROGRAMMING




        http://eglobiotraining.com
LOOPING
• There may be a situation when you need to execute a block
  of code several number of times. In general statements are
  executed sequentially: The first statement in a function is
  executed first, followed by the second, and so on.

• Programming languages provide various control structures
  that allow for more complicated execution paths.
• A loop statement allows us to execute a statement or group
  of statements multiple times and following is the general
  from of a loop statement in most of the programming
  languages.



                        http://eglobiotraining.com
LOOPING
• C++ programming language provides following
  types of loop to handle looping requirements:
         a.) “FOR” LOOP
         b.) “WHILE” LOOP
         c.) “DO WHILE” LOOP




                  http://eglobiotraining.com
"FOR” LOOP
• A for loop is a repetition control structure that
  allows you to efficiently write a loop that needs
  to execute a specific number of times.
• The statements in the for loop repeat
  continuously for a specific number of
  times. The while and do-while loops repeat until
  a certain condition is met. The for loop repeats
  until a specific count is met. Use a for loop when
  the number of repetition is know, or can be
  supplied by the user.

                    http://eglobiotraining.com
EXAMPLE OF FOR LOOPING IN
        C PROGRAMMING
•   #include <iostream>
•   #include <cmath>
•   using namespace std;

•   //prototype
•   int fallingdistance();

•   //main function
•   int main()
•   {
•           int count = 1 ;
•           int time;
•           double distance ;
•           cout << "Please enter time in 1 through 10 seconds.nn";
•
•     time = fallingdistance();
•
•            while ( time < 1 || time > 10)
•            { cout << "Must enter between 1 and 10 seconds, please re-enter.n";
•              time = fallingdistance();




                                              http://eglobiotraining.com
RUNNING FOR LOOP IN IN C
    PROGRAMMING




         http://eglobiotraining.com
“WHILE” LOOP
• The while loop allows programs to repeat a
  statement or series of statements, over and
  over, as long as a certain test condition is true.
• The while loop can be used if you don’t know
  how many times a loop must run.
• A while loop statement repeatedly executes a
  target statement as long as a given condition
  is true.

                    http://eglobiotraining.com
EXAMPLE OF WHILE LOOP IN C
         PROGRAMMING
#include <iostream.h>

int main(void) {
    int x = 0;
    int y = 0;
    bool validNumber = false;

    while (validNumber == false) {
       cout << "Please enter an integer between 1 and 10: ";
       cin >> x;
       cout << "You entered: " << x << endl << endl;

        if ((x < 1) || (x > 10)) {
             cout << "Your value for x is not between 1 and 10!"
              << endl;
             cout << "Please re-enter the number!" << endl << endl;
        }
        else
             validNumber = true;
    }

    cout << "Thank you for entering a valid number!" << endl;

    return 0;
•      }




                                                                http://eglobiotraining.com
RUNNING WHILE LOOP IN C
    PROGRAMMING




        http://eglobiotraining.com
“DO WHILE” LOOP
• In most computer programming languages, a do while
  loop, sometimes just called a while loop, is a control
  flow statement that allows code to be executed once based
  on a given Boolean condition.
• The do while construct consists of a process symbol and a
  condition. First, the code within the block is executed, and
  then the condition is evaluated. If the condition is true the
  code within the block is executed again. This repeats until
  the condition becomes false. Because do while loops check
  the condition after the block is executed, the control
  structure is often also known as a post-test loop. Contrast
  with the while loop, which tests the condition before the
  code within the block is executed.


                         http://eglobiotraining.com
“DO WHILE” LOOP
• Unlike for and while loops, which test the loop
  condition at the top of the loop, the do...while loop
  checks its condition at the bottom of the loop.
• A do...while loop is similar to a while loop, except that
  a do...while loop is guaranteed to execute at least one
  time.
• The do-while loop is similar to the while loop, except
  that the test condition occurs at the end of the
  loop. Having the test condition at the end, guarantees
  that the body of the loop always executes at least one
  time.

                       http://eglobiotraining.com
EXAMPLE OF A DO WHILE LOOP
        IN C PROGRAMMING
•   #include <iostream>
•   using namespace std;
•   main()
•   { int num1, num2;
•   char again = 'y';

•   while (again == 'y' || again == 'Y') {
•   cout << "Enter a number: ";
•   cin >> num1;
•   cout << "Enter another number: ";
•   cin >> num2;
•   cout << "Their sum is " << (num1 + num2) << endl;
•   cout << "Do you want to do this again? ";
•   cin >> again; }
•   return 0;
•   }

                                 http://eglobiotraining.com
RUNNING DO WHILE LOOP IN C
      PROGRAMMING




          http://eglobiotraining.com
http://eglobiotraining.com

Contenu connexe

Tendances

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programmingtrish_maxine
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.gerrell
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinsonmonstergeorge
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!olracoatalub
 
Jangsehyun final requirement
Jangsehyun final requirementJangsehyun final requirement
Jangsehyun final requirementSehyun Jang
 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statementsharsh kothari
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperosmarkings17
 
Linux System Administration
Linux System AdministrationLinux System Administration
Linux System AdministrationJayant Dalvi
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements IReem Alattas
 

Tendances (20)

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Macaraeg
MacaraegMacaraeg
Macaraeg
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinson
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Jangsehyun final requirement
Jangsehyun final requirementJangsehyun final requirement
Jangsehyun final requirement
 
Castro
CastroCastro
Castro
 
Final requirement
Final requirementFinal requirement
Final requirement
 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
 
Janakiram web
Janakiram webJanakiram web
Janakiram web
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperos
 
neiljaysonching
neiljaysonchingneiljaysonching
neiljaysonching
 
C++basics
C++basicsC++basics
C++basics
 
Kotlin
KotlinKotlin
Kotlin
 
Linux System Administration
Linux System AdministrationLinux System Administration
Linux System Administration
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Vbscript
VbscriptVbscript
Vbscript
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
 

En vedette

Resource allocation and empowerment practices
Resource allocation and empowerment practicesResource allocation and empowerment practices
Resource allocation and empowerment practicespaulyeboah
 
Uploads 178 entrepreneurship education-can business meet the challange
Uploads 178 entrepreneurship education-can business meet the challangeUploads 178 entrepreneurship education-can business meet the challange
Uploads 178 entrepreneurship education-can business meet the challangepaulyeboah
 
ACCESSING FINANCIAL RESORCES
ACCESSING FINANCIAL RESORCESACCESSING FINANCIAL RESORCES
ACCESSING FINANCIAL RESORCESpaulyeboah
 
Drs 255 skills in job matching and placement
Drs 255 skills in job matching and placementDrs 255 skills in job matching and placement
Drs 255 skills in job matching and placementpaulyeboah
 
What does it mean to be enterprising
What does it mean to be enterprisingWhat does it mean to be enterprising
What does it mean to be enterprisingpaulyeboah
 
Glossary of terminologies for vocational evaluation
Glossary of terminologies for vocational evaluationGlossary of terminologies for vocational evaluation
Glossary of terminologies for vocational evaluationpaulyeboah
 
Vocational training skills
Vocational training skillsVocational training skills
Vocational training skillspaulyeboah
 
Drs 255 skills in vocational assessment
Drs 255 skills in vocational assessmentDrs 255 skills in vocational assessment
Drs 255 skills in vocational assessmentpaulyeboah
 
Drs 255 project management skills
Drs 255 project management skillsDrs 255 project management skills
Drs 255 project management skillspaulyeboah
 
Skills matching challenge
Skills matching challengeSkills matching challenge
Skills matching challengepaulyeboah
 
Ghana shared growth & development agenda
Ghana shared growth & development agendaGhana shared growth & development agenda
Ghana shared growth & development agendapaulyeboah
 
Makalah Corporate Social Responsibility (CSR) - PT. Pertamina
Makalah Corporate Social Responsibility (CSR) - PT. PertaminaMakalah Corporate Social Responsibility (CSR) - PT. Pertamina
Makalah Corporate Social Responsibility (CSR) - PT. PertaminaFebri Alif Pratama
 
Technology Profile CRA 2014
Technology Profile CRA 2014Technology Profile CRA 2014
Technology Profile CRA 2014Mike Agostinelli
 
Inspiro institute profile
Inspiro institute profileInspiro institute profile
Inspiro institute profileSERV-SMART
 
American institute for Professional Studies Profile
American institute for Professional Studies   ProfileAmerican institute for Professional Studies   Profile
American institute for Professional Studies ProfileDalia Shalibi
 
Instructional Design and Technology Profile Assignment on Dr. Sharon Smaldino
Instructional Design and Technology Profile Assignment on Dr. Sharon SmaldinoInstructional Design and Technology Profile Assignment on Dr. Sharon Smaldino
Instructional Design and Technology Profile Assignment on Dr. Sharon SmaldinoGenevaD
 

En vedette (20)

Resource allocation and empowerment practices
Resource allocation and empowerment practicesResource allocation and empowerment practices
Resource allocation and empowerment practices
 
Uploads 178 entrepreneurship education-can business meet the challange
Uploads 178 entrepreneurship education-can business meet the challangeUploads 178 entrepreneurship education-can business meet the challange
Uploads 178 entrepreneurship education-can business meet the challange
 
ACCESSING FINANCIAL RESORCES
ACCESSING FINANCIAL RESORCESACCESSING FINANCIAL RESORCES
ACCESSING FINANCIAL RESORCES
 
Drs 255 skills in job matching and placement
Drs 255 skills in job matching and placementDrs 255 skills in job matching and placement
Drs 255 skills in job matching and placement
 
What does it mean to be enterprising
What does it mean to be enterprisingWhat does it mean to be enterprising
What does it mean to be enterprising
 
Glossary of terminologies for vocational evaluation
Glossary of terminologies for vocational evaluationGlossary of terminologies for vocational evaluation
Glossary of terminologies for vocational evaluation
 
Vocational training skills
Vocational training skillsVocational training skills
Vocational training skills
 
Drs 255 skills in vocational assessment
Drs 255 skills in vocational assessmentDrs 255 skills in vocational assessment
Drs 255 skills in vocational assessment
 
Drs 255 project management skills
Drs 255 project management skillsDrs 255 project management skills
Drs 255 project management skills
 
Skills matching challenge
Skills matching challengeSkills matching challenge
Skills matching challenge
 
Ghana shared growth & development agenda
Ghana shared growth & development agendaGhana shared growth & development agenda
Ghana shared growth & development agenda
 
Makalah Corporate Social Responsibility (CSR) - PT. Pertamina
Makalah Corporate Social Responsibility (CSR) - PT. PertaminaMakalah Corporate Social Responsibility (CSR) - PT. Pertamina
Makalah Corporate Social Responsibility (CSR) - PT. Pertamina
 
Makalah File , Database
Makalah File , DatabaseMakalah File , Database
Makalah File , Database
 
Technology Profile CRA 2014
Technology Profile CRA 2014Technology Profile CRA 2014
Technology Profile CRA 2014
 
Inspiro institute profile
Inspiro institute profileInspiro institute profile
Inspiro institute profile
 
American institute for Professional Studies Profile
American institute for Professional Studies   ProfileAmerican institute for Professional Studies   Profile
American institute for Professional Studies Profile
 
Introduction to programming languages part 1
Introduction to programming languages   part 1Introduction to programming languages   part 1
Introduction to programming languages part 1
 
RU Institute - Firm Profile
RU Institute - Firm ProfileRU Institute - Firm Profile
RU Institute - Firm Profile
 
Ofertaoi
OfertaoiOfertaoi
Ofertaoi
 
Instructional Design and Technology Profile Assignment on Dr. Sharon Smaldino
Instructional Design and Technology Profile Assignment on Dr. Sharon SmaldinoInstructional Design and Technology Profile Assignment on Dr. Sharon Smaldino
Instructional Design and Technology Profile Assignment on Dr. Sharon Smaldino
 

Similaire à Final project powerpoint template (fndprg) (1)

Similaire à Final project powerpoint template (fndprg) (1) (20)

C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
C loops
C loopsC loops
C loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
neiljaysonching
neiljaysonchingneiljaysonching
neiljaysonching
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angeli
 
Fundamentals of programming
Fundamentals of programmingFundamentals of programming
Fundamentals of programming
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scripting
 
While loop
While loopWhile loop
While loop
 
Loops c++
Loops c++Loops c++
Loops c++
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
The Loops
The LoopsThe Loops
The Loops
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 

Final project powerpoint template (fndprg) (1)

  • 1. FINAL REQUIREMENT Programming http://eglobiotraining.com
  • 2. Topics in Programming 1. Looping 2. Switch Case http://eglobiotraining.com
  • 3. SWITCH CASE STATEMENT • In programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C/C++, C#, Java, and so on. It is also included in several other types of Programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "go to", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. http://eglobiotraining.com
  • 4. SWITCH CASE STATEMENT • Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using the switch case in the programming is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. • The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if- else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution. http://eglobiotraining.com
  • 5. SWITCH CASE STATEMENT • Switch is used to choose a fragment of template depending on the value of an expression • This has a similar function as the If condition - but it is more useful in situations when there is many possible values for the variable. Switch will evaluate one of several statements, depending on the value of a given variable. If no given value matches the variable, the default statement is executed. • The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed. http://eglobiotraining.com
  • 6. Example of switch case IN C PROGRAMMING #include <stdlib.h> using namespace std; void welcome(); char getChar(); void displayResponse(char choice); int main(int argc, char *argv[]) { char choice; // declares the choice variable welcome(); // This calls the welcome function choice = getChar(); // calls getChar and returns the value for choice displayResponse(choice); // passes choice to displayResponse function system("PAUSE"); return 0; } // end main // welcome function displays an opening message to // explain the program to the user void welcome() { cout << "This program displays different messages dependingn"; cout << "on which letter is entered by the user.n"; cout << "Pick a letter a, b, c or d to see whatn"; cout << "the program will say.nn"; } // end of welcome function // getChar asks the user for a letter a, b or c. // The character is returned to where the function was called. char getChar() { char response; // declares variable called response cout << "Please type a letter a, b, c and d: "; // prompt for letter cin >> response; // gets input from user and assigns it to response return response; // sends back the response value } // end getChar function // displayResponse function takes the char variable and uses it // to determine which set of tasks will be performed. void displayResponse(char choice) { char again; http://eglobiotraining.com
  • 7. Running switch case IN C PROGRAMMING http://eglobiotraining.com
  • 8. LOOPING • There may be a situation when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. • Programming languages provide various control structures that allow for more complicated execution paths. • A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages. http://eglobiotraining.com
  • 9. LOOPING • C++ programming language provides following types of loop to handle looping requirements: a.) “FOR” LOOP b.) “WHILE” LOOP c.) “DO WHILE” LOOP http://eglobiotraining.com
  • 10. "FOR” LOOP • A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. • The statements in the for loop repeat continuously for a specific number of times. The while and do-while loops repeat until a certain condition is met. The for loop repeats until a specific count is met. Use a for loop when the number of repetition is know, or can be supplied by the user. http://eglobiotraining.com
  • 11. EXAMPLE OF FOR LOOPING IN C PROGRAMMING • #include <iostream> • #include <cmath> • using namespace std; • //prototype • int fallingdistance(); • //main function • int main() • { • int count = 1 ; • int time; • double distance ; • cout << "Please enter time in 1 through 10 seconds.nn"; • • time = fallingdistance(); • • while ( time < 1 || time > 10) • { cout << "Must enter between 1 and 10 seconds, please re-enter.n"; • time = fallingdistance(); http://eglobiotraining.com
  • 12. RUNNING FOR LOOP IN IN C PROGRAMMING http://eglobiotraining.com
  • 13. “WHILE” LOOP • The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true. • The while loop can be used if you don’t know how many times a loop must run. • A while loop statement repeatedly executes a target statement as long as a given condition is true. http://eglobiotraining.com
  • 14. EXAMPLE OF WHILE LOOP IN C PROGRAMMING #include <iostream.h> int main(void) { int x = 0; int y = 0; bool validNumber = false; while (validNumber == false) { cout << "Please enter an integer between 1 and 10: "; cin >> x; cout << "You entered: " << x << endl << endl; if ((x < 1) || (x > 10)) { cout << "Your value for x is not between 1 and 10!" << endl; cout << "Please re-enter the number!" << endl << endl; } else validNumber = true; } cout << "Thank you for entering a valid number!" << endl; return 0; • } http://eglobiotraining.com
  • 15. RUNNING WHILE LOOP IN C PROGRAMMING http://eglobiotraining.com
  • 16. “DO WHILE” LOOP • In most computer programming languages, a do while loop, sometimes just called a while loop, is a control flow statement that allows code to be executed once based on a given Boolean condition. • The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed. http://eglobiotraining.com
  • 17. “DO WHILE” LOOP • Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. • A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. • The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time. http://eglobiotraining.com
  • 18. EXAMPLE OF A DO WHILE LOOP IN C PROGRAMMING • #include <iostream> • using namespace std; • main() • { int num1, num2; • char again = 'y'; • while (again == 'y' || again == 'Y') { • cout << "Enter a number: "; • cin >> num1; • cout << "Enter another number: "; • cin >> num2; • cout << "Their sum is " << (num1 + num2) << endl; • cout << "Do you want to do this again? "; • cin >> again; } • return 0; • } http://eglobiotraining.com
  • 19. RUNNING DO WHILE LOOP IN C PROGRAMMING http://eglobiotraining.com