SlideShare uma empresa Scribd logo
1 de 23
TOPICS IN PROGRAMMING




      http://eglobiotraining.com
SWITCH CASE
                 STATEMENT
    0 In programming, a switch, case, select or inspect stateme
     nt 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.

T
O
P
I                        http://eglobiotraining.com             NEXT
C
SWITCH CASE
                   STATEMENT
    0 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.
    0 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.
T
O
P
I       BACK                http://eglobiotraining.com                 NEXT
C
SWITCH CASE
                 STATEMENT
    0 Switch is used to choose a fragment of template
      depending on the value of an expression
    0 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.
    0 The value of the expressions in a switch-case
      statement must be an ordinal type
T     i.e. integer, char, short, long, etc. Float and double are
O
P     not allowed.
I       BACK             http://eglobiotraining.com          NEXT
C
T
O
                                        NEXT
P
           http://eglobiotraining.com
I   BACK
C
T
O
P
I   BACK   http://eglobiotraining.com   NEXT
C
Example of switch case IN C
        PROGRAMMING
    #include <iostream>                                                                     // switch statement based on the choice variable
    #include <stdlib.h>
    using namespace std;                                                                    switch (choice) // notice no semicolon
    void welcome();                                                                         {
    char getChar();                                                                           case 'A': // choice was the letter A
    void displayResponse(char choice);
    int main(int argc, char *argv[])                                                          case 'a': // choice was the letter a
    {                                                                                          cout << "your awesome dude.nn";
      char choice; // declares the choice variable                                             break; // this ends the statements for case A/a
      welcome(); // This calls the welcome function
      choice = getChar(); // calls getChar and returns the value for choice                   case 'B': // choice was the letter b
      displayResponse(choice); // passes choice to displayResponse function                   case 'b': // choice was the letter b
                                                                                               cout << "you will find your lovelife.nn";
      system("PAUSE");
      return 0;                                                                                break; // this ends the statements for case B/b
    } // end main                                                                             case 'C': // choice was the letter C
    // welcome function displays an opening message to                                        case 'c': // choice was the letter c
    // explain the program to the user
    void welcome()                                                                             cout << "your will won the lottery.nn";
    {                                                                                          break; // this ends the statements for case C/c
      cout << "This program displays different messages dependingn";                           case 'D': // choice was the letter D
      cout << "on which letter is entered by the user.n";
      cout << "Pick a letter a, b, c or d to see whatn";                                     case 'd': // choice was the letter d
      cout << "the program will say.nn";                                                     cout << "your so ugly!!.nn";
    } // end of welcome function                                                               break; // this ends the statements for case D/d
    // getChar asks the user for a letter a, b or c.
    // The character is returned to where the function was called.                            default: // used when choice falls out of the cases covered
    char getChar()                                                                         above
    {                                                                                          cout << "You didn't pick a letter a, b or c.nn";
        char response; // declares variable called response
      cout << "Please type a letter a, b, c and d: "; // prompt for letter
                                                                                               again = getChar(); // gives the user another try
      cin >> response; // gets input from user and assigns it to response                      displayResponse(again); // recalls displayResponse with new
      return response; // sends back the response value                                    character
    } // end getChar function                                                                  break;
    // displayResponse function takes the char variable and uses it
    // to determine which set of tasks will be performed.
                                                                                            } // end of switch statement
    void displayResponse(char choice)                                                      } // end displayResponse function
    {
      char again;

T
O
P
I                  BACK                                                       http://eglobiotraining.com                                                     NEXT
C
Running switch case IN C
       PROGRAMMING




T
O
P
I           http://eglobiotraining.com
C                                        BACK
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
T languages.
O
P
I
         LOOP            http://eglobiotraining.com
C                                                     NEXT
LOOPING
    C++ programming language provides
    following types of loop to handle
    looping requirements:




T
O
P
I
                 http://eglobiotraining.com
C
                                              BACK
T




             "FOR” LOOP
O
P
I
C




       0 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.
       0 The statements in the for loop repeat continuously for
         aspecific 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
LOOP            BACK                                       NEXT
T
O
P
I
C      EXAMPLE OF FOR LOOPING IN
           C PROGRAMMING
 #include <iostream>
 #include <cmath>
                                                                         }
 using namespace std;
                                                                         cout <<"nSeconds
 //prototype                                                             falling distancen";
 int fallingdistance();                                                  cout <<"---------------------------------------n";

                                                                          for ( count = 1; count <= time; count++)
 //main function
                                                                          {
 int main()                                                                              distance = .5 * 9.8 *
 {                                                         pow(time, 2.0);
            int count = 1 ;                                                              cout << count << "
            int time;                                      " << distance <<" meters"<< endl;
            double distance ;
                                                                        }
            cout << "Please enter time in 1
                                                             system ("pause");
 through 10 seconds.nn";                                              return 0;
                                                       }
   time = fallingdistance();                           // falling distance function for a return value in seconds
                                                       transfer to time
              while ( time < 1 || time > 10)           int fallingdistance ()
                                                       {
              { cout << "Must enter between 1 and
                                                                       int seconds;                            NEXT
 10 seconds, please re-enter.n";                                      cin >> seconds;
                time = fallingdistance(); http://eglobiotraining.com
LOOP                                                                   return seconds;     BACK
                                                       }
T
O
P
I
C
    RUNNING FOR LOOP IN IN
       C PROGRAMMING




           http://eglobiotraining.com
LOOP                                    BACK
“WHILE” LOOP
T
O
P
I
C




       0 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.
       0 The while loop can be used if you don’t know how
         many times a loop must run.
       0 A while loop statement repeatedly executes a target
         statement as long as a given condition is true.




              BACK        http://eglobiotraining.com
LOOP                                                     NEXT
T
O
P
I
C
    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;
       0    }




                              BACK                     http://eglobiotraining.com
LOOP                                                                                NEXT
T
O
P
I
C   RUNNING WHILE LOOP IN
       C PROGRAMMING




LOOP       http://eglobiotraining.com
                                        BACK
T
O
P


               “DO WHILE” LOOP
I
C




       0 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.
       0 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.

LOOP             BACK        http://eglobiotraining.com
                                                                NEXT
T
O
P


               “DO WHILE” LOOP
I
C




       0 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.
       0 A do...while loop is similar to a while loop, except that
         a do...while loop is guaranteed to execute at least one
         time.
       0 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.
LOOP                       http://eglobiotraining.com
              BACK                                          NEXT
T
O
P
I
C
        EXAMPLE OF A DO WHILE
       LOOP IN C PROGRAMMING
       0   #include <iostream>
       0   using namespace std;
       0   main()
       0   { int num1, num2;
       0   char again = 'y';

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


LOOP                    BACK          http://eglobiotraining.com
                                                                   NEXT
T
O
P
I
C
    RUNNING DO WHILE LOOP IN C
          PROGRAMMING




             http://eglobiotraining.com
LOOP                                      BACK
http://www.slideshare.net/ni
        colecastro21



          http://eglobiotraining.com
Final requirement

Mais conteúdo relacionado

Mais procurados

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programmingtrish_maxine
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 
Oop10 6
Oop10 6Oop10 6
Oop10 6schwaa
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
Elements of c program....by thanveer danish
Elements of c program....by thanveer danishElements of c program....by thanveer danish
Elements of c program....by thanveer danishMuhammed Thanveer M
 
Computer programming
Computer programmingComputer programming
Computer programmingXhyna Delfin
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8REHAN IJAZ
 

Mais procurados (20)

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Oop10 6
Oop10 6Oop10 6
Oop10 6
 
Ref cursor
Ref cursorRef cursor
Ref cursor
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
C operators
C operatorsC operators
C operators
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Perl_Part2
Perl_Part2Perl_Part2
Perl_Part2
 
C if else
C if elseC if else
C if else
 
Elements of c program....by thanveer danish
Elements of c program....by thanveer danishElements of c program....by thanveer danish
Elements of c program....by thanveer danish
 
Computer programming
Computer programmingComputer programming
Computer programming
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
PLSQL
PLSQLPLSQL
PLSQL
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Introduction to Swift
Introduction to SwiftIntroduction to Swift
Introduction to Swift
 
Cpu
CpuCpu
Cpu
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
Textmate shortcuts
Textmate shortcutsTextmate shortcuts
Textmate shortcuts
 

Destaque

Panorama histórico en méxico
Panorama histórico en méxicoPanorama histórico en méxico
Panorama histórico en méxicotvcarlos
 
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)danicapagkalinawan
 
Asma curso medicos_generales_dr_gutierrez
Asma  curso medicos_generales_dr_gutierrezAsma  curso medicos_generales_dr_gutierrez
Asma curso medicos_generales_dr_gutierrezpedrogal33
 
Mon album de famille
Mon album de familleMon album de famille
Mon album de famillejesilmora15_
 
Comandos (1)
Comandos (1)Comandos (1)
Comandos (1)debby4587
 
Una fórmula sencilla para manejar el estrés
Una fórmula sencilla para manejar el estrésUna fórmula sencilla para manejar el estrés
Una fórmula sencilla para manejar el estrésJOSE SALAS
 

Destaque (9)

Panorama histórico en méxico
Panorama histórico en méxicoPanorama histórico en méxico
Panorama histórico en méxico
 
Gagan international
Gagan internationalGagan international
Gagan international
 
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)
Httpwww.slideshare.netuploadfrom source=loggedin newsignup (1)
 
Virusppt
ViruspptVirusppt
Virusppt
 
Asma curso medicos_generales_dr_gutierrez
Asma  curso medicos_generales_dr_gutierrezAsma  curso medicos_generales_dr_gutierrez
Asma curso medicos_generales_dr_gutierrez
 
Rutas del aprendizaje yanahuanca
Rutas del aprendizaje   yanahuancaRutas del aprendizaje   yanahuanca
Rutas del aprendizaje yanahuanca
 
Mon album de famille
Mon album de familleMon album de famille
Mon album de famille
 
Comandos (1)
Comandos (1)Comandos (1)
Comandos (1)
 
Una fórmula sencilla para manejar el estrés
Una fórmula sencilla para manejar el estrésUna fórmula sencilla para manejar el estrés
Una fórmula sencilla para manejar el estrés
 

Semelhante a Final requirement

presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinsonmonstergeorge
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming FundamentalsKIU
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingaprilyyy
 
Lecture#4 Algorithm and computing
Lecture#4 Algorithm and computingLecture#4 Algorithm and computing
Lecture#4 Algorithm and computingNUST Stuff
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingChaAstillas
 

Semelhante a Final requirement (20)

Final requirement
Final requirementFinal requirement
Final requirement
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinson
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Looping statements
Looping statementsLooping statements
Looping statements
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Lecture#4 Algorithm and computing
Lecture#4 Algorithm and computingLecture#4 Algorithm and computing
Lecture#4 Algorithm and computing
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 

Final requirement

  • 1.
  • 2. TOPICS IN PROGRAMMING http://eglobiotraining.com
  • 3. SWITCH CASE STATEMENT 0 In programming, a switch, case, select or inspect stateme nt 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. T O P I http://eglobiotraining.com NEXT C
  • 4. SWITCH CASE STATEMENT 0 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. 0 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. T O P I BACK http://eglobiotraining.com NEXT C
  • 5. SWITCH CASE STATEMENT 0 Switch is used to choose a fragment of template depending on the value of an expression 0 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. 0 The value of the expressions in a switch-case statement must be an ordinal type T i.e. integer, char, short, long, etc. Float and double are O P not allowed. I BACK http://eglobiotraining.com NEXT C
  • 6. T O NEXT P http://eglobiotraining.com I BACK C
  • 7. T O P I BACK http://eglobiotraining.com NEXT C
  • 8. Example of switch case IN C PROGRAMMING #include <iostream> // switch statement based on the choice variable #include <stdlib.h> using namespace std; switch (choice) // notice no semicolon void welcome(); { char getChar(); case 'A': // choice was the letter A void displayResponse(char choice); int main(int argc, char *argv[]) case 'a': // choice was the letter a { cout << "your awesome dude.nn"; char choice; // declares the choice variable break; // this ends the statements for case A/a welcome(); // This calls the welcome function choice = getChar(); // calls getChar and returns the value for choice case 'B': // choice was the letter b displayResponse(choice); // passes choice to displayResponse function case 'b': // choice was the letter b cout << "you will find your lovelife.nn"; system("PAUSE"); return 0; break; // this ends the statements for case B/b } // end main case 'C': // choice was the letter C // welcome function displays an opening message to case 'c': // choice was the letter c // explain the program to the user void welcome() cout << "your will won the lottery.nn"; { break; // this ends the statements for case C/c cout << "This program displays different messages dependingn"; case 'D': // choice was the letter D cout << "on which letter is entered by the user.n"; cout << "Pick a letter a, b, c or d to see whatn"; case 'd': // choice was the letter d cout << "the program will say.nn"; cout << "your so ugly!!.nn"; } // end of welcome function break; // this ends the statements for case D/d // getChar asks the user for a letter a, b or c. // The character is returned to where the function was called. default: // used when choice falls out of the cases covered char getChar() above { cout << "You didn't pick a letter a, b or c.nn"; char response; // declares variable called response cout << "Please type a letter a, b, c and d: "; // prompt for letter again = getChar(); // gives the user another try cin >> response; // gets input from user and assigns it to response displayResponse(again); // recalls displayResponse with new return response; // sends back the response value character } // end getChar function break; // displayResponse function takes the char variable and uses it // to determine which set of tasks will be performed. } // end of switch statement void displayResponse(char choice) } // end displayResponse function { char again; T O P I BACK http://eglobiotraining.com NEXT C
  • 9. Running switch case IN C PROGRAMMING T O P I http://eglobiotraining.com C BACK
  • 10. 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 T languages. O P I LOOP http://eglobiotraining.com C NEXT
  • 11. LOOPING C++ programming language provides following types of loop to handle looping requirements: T O P I http://eglobiotraining.com C BACK
  • 12. T "FOR” LOOP O P I C 0 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. 0 The statements in the for loop repeat continuously for aspecific 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 LOOP BACK NEXT
  • 13. T O P I C EXAMPLE OF FOR LOOPING IN C PROGRAMMING #include <iostream> #include <cmath> } using namespace std; cout <<"nSeconds //prototype falling distancen"; int fallingdistance(); cout <<"---------------------------------------n"; for ( count = 1; count <= time; count++) //main function { int main() distance = .5 * 9.8 * { pow(time, 2.0); int count = 1 ; cout << count << " int time; " << distance <<" meters"<< endl; double distance ; } cout << "Please enter time in 1 system ("pause"); through 10 seconds.nn"; return 0; } time = fallingdistance(); // falling distance function for a return value in seconds transfer to time while ( time < 1 || time > 10) int fallingdistance () { { cout << "Must enter between 1 and int seconds; NEXT 10 seconds, please re-enter.n"; cin >> seconds; time = fallingdistance(); http://eglobiotraining.com LOOP return seconds; BACK }
  • 14. T O P I C RUNNING FOR LOOP IN IN C PROGRAMMING http://eglobiotraining.com LOOP BACK
  • 15. “WHILE” LOOP T O P I C 0 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. 0 The while loop can be used if you don’t know how many times a loop must run. 0 A while loop statement repeatedly executes a target statement as long as a given condition is true. BACK http://eglobiotraining.com LOOP NEXT
  • 16. T O P I C 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; 0 } BACK http://eglobiotraining.com LOOP NEXT
  • 17. T O P I C RUNNING WHILE LOOP IN C PROGRAMMING LOOP http://eglobiotraining.com BACK
  • 18. T O P “DO WHILE” LOOP I C 0 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. 0 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. LOOP BACK http://eglobiotraining.com NEXT
  • 19. T O P “DO WHILE” LOOP I C 0 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. 0 A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. 0 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. LOOP http://eglobiotraining.com BACK NEXT
  • 20. T O P I C EXAMPLE OF A DO WHILE LOOP IN C PROGRAMMING 0 #include <iostream> 0 using namespace std; 0 main() 0 { int num1, num2; 0 char again = 'y'; 0 while (again == 'y' || again == 'Y') { 0 cout << "Enter a number: "; 0 cin >> num1; 0 cout << "Enter another number: "; 0 cin >> num2; 0 cout << "Their sum is " << (num1 + num2) << endl; 0 cout << "Do you want to do this again? "; 0 cin >> again; } 0 return 0; 0 } LOOP BACK http://eglobiotraining.com NEXT
  • 21. T O P I C RUNNING DO WHILE LOOP IN C PROGRAMMING http://eglobiotraining.com LOOP BACK
  • 22. http://www.slideshare.net/ni colecastro21 http://eglobiotraining.com