Switchcaseandlooping 121012074703 Phpapp01 (1)

Embed Size (px)

Citation preview

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    1/56

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    2/56

    What is computer program?is a sequence of instruction written to perform aspecified task with a computer. A computer requiresprograms to function, typically executing theprogram's instructions in central processor.Theprogram has an executable form that the computer canuse directly to execute the instructions. The sameprogram in its human-readable source code form,from which executable programs are derived (e.g.,compiled), enables a programmer to study and developits algorithms.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    3/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    4/56

    Computer Programmer

    is a person who writes computer software. Theterm computer programmercan refer to a specialist inone area of computer programmingor to a generalistwho writes code for many kinds of software. One whopractices or professes a formal approach toprogrammingmay also be known as a programmeranalyst.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    5/56

    Programmers are people who design theprogram of events that turns input data intooutput data. It has been proved that such aprogram of events can be designed using a

    structure composed of sequences, iterationsand selections. Code is merely the way thatthe program is described.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    6/56

    Programmingis the iterative process ofwriting or editing source code. Editing sourcecode involves testing, analyzing, and refining, andsometimes coordinating with other programmerson a jointly developed program. A person whopractices this skill is referred to as acomputer programmer, software developer orcoder. The sometimes lengthy process ofcomputer programmingis usually referred to

    as software development. The term softwareengineering is becoming popular as the process isseen as an engineering discipline.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    7/56

    Programmingis instructing a computer to dosomething for you with the help of a programming language.The role of a programminglanguage can be described in two

    ways:

    Technical: It is a means for instructing a Computer toperform Tasks

    Conceptual: It is a framework within which we organize ourideas about things and processes.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    8/56

    A Programming Languageisan artificial language designed tocommunicate instructions to a machine,

    particularly a computer. Programminglanguages can be used to create programsthat control the behavior of a machine

    and/or to express algorithms precisely.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    9/56

    Programminglanguage is usually split into the twocomponents of syntax (form)and semantics(meaning). Some languages aredefined by a specification document (for example,

    the C programminglanguage is specified byan ISO Standard), while other languages, suchas Perl 5 and earlier, have adominant implementation that is used asa reference.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    10/56

    A programminglanguageis a computer languageprogrammers use to develop applications, scripts, orother set of instructions for a computer to execute.

    Below is a listing of several different programminglanguages and scripting languages currently listed inour database.

    http://eglobiotraining.com/

    ALGOLAdaAIMLAssemblyBASICBatchfileBCPLCC#

    C++COBOLCPLDDarkBASICdBASE

    FORTRANFoxProHDMLHTML

    JavaJavaScriptJCLLISPLive Script

    LOGOPascalPerlPHPPick

    PrologRubySGMLSmalltalkSQLTclTrue BASIC

    VHDLVisual Basic

    Visual FoxProWMLWHTMLXML

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/http://www.computerhope.com/jargon/a/algol.htmhttp://www.computerhope.com/jargon/a/ada.htmhttp://www.computerhope.com/jargon/a/aiml.htmhttp://www.computerhope.com/jargon/a/al.htmhttp://www.computerhope.com/jargon/b/basic.htmhttp://www.computerhope.com/jargon/b/batchfil.htmhttp://www.computerhope.com/jargon/b/bcpl.htmhttp://www.computerhope.com/jargon/c/c.htmhttp://www.computerhope.com/jargon/c/csharp.htmhttp://www.computerhope.com/jargon/c/cplus.htmhttp://www.computerhope.com/jargon/c/cobol.htmhttp://www.computerhope.com/jargon/c/cpl.htmhttp://www.computerhope.com/jargon/d/dlang.htmhttp://www.computerhope.com/jargon/d/darkbasi.htmhttp://www.computerhope.com/jargon/d/dbase.htmhttp://www.computerhope.com/jargon/f/fortran.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/h/hdml.htmhttp://www.computerhope.com/jargon/h/html.htmhttp://www.computerhope.com/jargon/j/java.htmhttp://www.computerhope.com/jargon/j/javascri.htmhttp://www.computerhope.com/jargon/j/jcl.htmhttp://www.computerhope.com/jargon/l/lisp.htmhttp://www.computerhope.com/jargon/l/livescri.htmhttp://www.computerhope.com/jargon/l/logo.htmhttp://www.computerhope.com/jargon/p/pascal.htmhttp://www.computerhope.com/jargon/p/perl.htmhttp://www.computerhope.com/jargon/p/php.htmhttp://www.computerhope.com/jargon/p/pick.htmhttp://www.computerhope.com/jargon/p/prolog.htmhttp://www.computerhope.com/jargon/r/ruby.htmhttp://www.computerhope.com/jargon/s/sgml.htmhttp://www.computerhope.com/jargon/s/smaltalk.htmhttp://www.computerhope.com/jargon/s/sql.htmhttp://www.computerhope.com/jargon/t/tcl.htmhttp://www.computerhope.com/jargon/t/truebasi.htmhttp://www.computerhope.com/jargon/v/vhdl.htmhttp://www.computerhope.com/software/vb.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/w/wml.htmhttp://www.computerhope.com/jargon/x/xhtml.htmhttp://www.computerhope.com/jargon/x/xml.htmhttp://www.computerhope.com/jargon/x/xml.htmhttp://www.computerhope.com/jargon/x/xhtml.htmhttp://www.computerhope.com/jargon/w/wml.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/software/vb.htmhttp://www.computerhope.com/jargon/v/vhdl.htmhttp://www.computerhope.com/jargon/t/truebasi.htmhttp://www.computerhope.com/jargon/t/tcl.htmhttp://www.computerhope.com/jargon/s/sql.htmhttp://www.computerhope.com/jargon/s/smaltalk.htmhttp://www.computerhope.com/jargon/s/sgml.htmhttp://www.computerhope.com/jargon/r/ruby.htmhttp://www.computerhope.com/jargon/p/prolog.htmhttp://www.computerhope.com/jargon/p/pick.htmhttp://www.computerhope.com/jargon/p/php.htmhttp://www.computerhope.com/jargon/p/perl.htmhttp://www.computerhope.com/jargon/p/pascal.htmhttp://www.computerhope.com/jargon/l/logo.htmhttp://www.computerhope.com/jargon/l/livescri.htmhttp://www.computerhope.com/jargon/l/lisp.htmhttp://www.computerhope.com/jargon/j/jcl.htmhttp://www.computerhope.com/jargon/j/javascri.htmhttp://www.computerhope.com/jargon/j/java.htmhttp://www.computerhope.com/jargon/h/html.htmhttp://www.computerhope.com/jargon/h/hdml.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/f/fortran.htmhttp://www.computerhope.com/jargon/d/dbase.htmhttp://www.computerhope.com/jargon/d/darkbasi.htmhttp://www.computerhope.com/jargon/d/dlang.htmhttp://www.computerhope.com/jargon/c/cpl.htmhttp://www.computerhope.com/jargon/c/cobol.htmhttp://www.computerhope.com/jargon/c/cplus.htmhttp://www.computerhope.com/jargon/c/csharp.htmhttp://www.computerhope.com/jargon/c/c.htmhttp://www.computerhope.com/jargon/b/bcpl.htmhttp://www.computerhope.com/jargon/b/batchfil.htmhttp://www.computerhope.com/jargon/b/basic.htmhttp://www.computerhope.com/jargon/a/al.htmhttp://www.computerhope.com/jargon/a/aiml.htmhttp://www.computerhope.com/jargon/a/ada.htmhttp://www.computerhope.com/jargon/a/algol.htmmailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    11/56

    The most important aspect of programmingis to analyzeand adopt specific solution while solving any problem.This needs a programming approach that defines themodularity of program that one writes and how it isrelated to others in an application.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    12/56

    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purposeprogramming

    language. It is regarded as an intermediate-level language,as it comprises a combination of both high-level and low-level language features.

    C++ is one of the most popular programming languages

    and is implemented on a wide variety of hardware andoperating system platforms.

    C++ has greatly influenced many other popularprogramming languages, most notably C# and Java. Othersuccessful languages such as Objective-C use a verydifferent syntax and approach to adding classes to C.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    13/56

    C++ is one of the most used programminglanguagesin the world. Also known as "C with Classes".

    C++ is a general-purpose programminglanguage withhigh-level and low-level capabilities. It is a staticallytyped, free-form, multi-paradigm, usually compiledlanguage supporting procedural programming, dataabstraction, object-oriented programming, andgeneric programming.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    14/56

    Switch Case In programming,

    a switch, case, selector inspectstatement is a typeof selection control mechanism that exists in mostimperative programminglanguages suchas Pascal, Ada, C/C++, C#, Java, and so on.

    The main reasons for using a switch include improvingclarity, by reducing otherwise repetitive coding, and (ifthe heuristics permit) also offering the potential forfaster execution through easier compileroptimization in many cases.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    15/56

    The switch-casestatement is a multi-waydecision statement. Unlike the multipledecision statement that can be created

    using if-else, the switchstatementevaluates the conditional expression andtests it against numerous constant values.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    16/56

    switch ( ) {case this-value:

    Code to execute if == this-valuebreak;case that-value:

    Code to execute if == that-value\break;

    ...

    default:Code to execute if does not equal the value following anyof the casesbreak;

    }

    http://eglobiotraining.com/

    Switch case statements are a substitute for long if statements that compare a variable toseveral "integral" values ("integral" values are simply values that can be expressed as aninteger, such as the value of a char). The basic format for using switch case is outlinedbelow. The value of the variable given into switch is compared to the value following eachof the cases, and when one value matches the value of the variable, the computercontinues executing the program from that point.

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    17/56

    The conditionof a switch statement is a value. The case

    says that if it has the value of whatever is after that casethen do whatever follows the colon.

    The break is used to break out of the case statements.

    Break is a keyword that breaks out of the code block,usually surrounded by braces, which it is in. In this case,break prevents the program from falling through andexecuting the code in all the other case statements. Animportant thing to note about the switch statement is that

    the case values may only be constant integral expressions.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    18/56

    Sadly, it isn't legal to use caselike this:

    http://eglobiotraining.com/

    int a = 10;int b = 10;

    int c = 20;

    switch ( a ) {case b:

    /* Code */break;

    case c:/* Code */break;

    default:/* Code */break;

    }

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    19/56

    The default case is optional, but it is wise toinclude it as it handles any unexpected cases. Itcan be useful to put some kind of output to alert

    you to the code entering the default case if you

    don't expect it to. Switch statements serve as asimple way to write long if statements when therequirements are met. Often it can be used toprocess input from a user.

    http://eglobiotraining.com/

    #include

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    20/56

    This is a sample program,

    in which not all of theproper functions areactually declared, butwhich shows how one

    would use switch in aprogram.

    http://eglobiotraining.com/

    #include

    using namespace std;

    void playgame(){

    cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    21/56

    This program will compile, but cannot be run until the

    undefined functions are given bodies, but it serves as amodel (albeit simple) for processing input. If you donot understand this then try mentally putting in ifstatements for the case statements. Default simplyskips out of the switch case construction and allowsthe program to terminate naturally. If you do not likethat, then you can make a loop around the whole thingto have it wait for valid input. You could easily make afew small functions if you wish to test the code.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    22/56

    Looping

    In computer programming, a loop is a sequenceof instructions that is continually repeated until a

    certain condition is reached. Typically, a certainprocess is done, such as getting an item of data andchanging it, and then some condition is checkedsuch as whether a counter has reached a

    prescribed number.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    23/56

    If it hasn't, the next instruction in the sequence isan instruction to return to the first instruction inthe sequence and repeat the sequence. If thecondition has been reached, the next instruction

    "falls through" to the next sequential instructionor branches outside the loop. A loop is afundamental programming idea that is commonlyused in writing programs.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    24/56

    Loops are used to repeat a block of code. Being able to

    have your program repeatedly execute a block of codeis one of the most basic but useful tasks inprogramming -- many programs or websites thatproduce extremely complex output (such as a messageboard) are really only executing a single task many

    times. (They may be executing a small number oftasks, but in principle, to produce a list of messagesonly requires repeating the operation of reading insome data and displaying it.) Now, think about whatthis means: a loop lets you write a very simplestatement to produce a significantly greater resultsimply by repetition.

    http://eglobiotraining.com/

    mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/mailto:http://eglobiotraining.com/
  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    25/56

    One Caveat: before going further, you shouldunderstand the concept of C++'s true and false,because it will be necessary when working with loops(the conditions are the same as with if statements).

    There are three types of loops:

    for

    While

    do..while

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    26/56

    For

    http://eglobiotraining.com/

    A for loop allows a

    statement to beexecuted a specifiednumber of times.

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    27/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    28/56

    While

    http://eglobiotraining.com/

    A while loop allows astatement to be executeduntil a given condition ismet. If the condition ismet prior to executingthe loop then the loop

    will not be executed. As

    soon as the condition ismet, execution continues

    with the statementfollowing the loop.

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    29/56

    http://eglobiotraining.com/

    WHILE loops are very simple.

    #include

    using namespace std; // So we can see cout and endl

    int main(){

    int x = 0; // Don't forget to declare variableswhile ( x < 10 ) { // While x is less than 10cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    30/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    31/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    32/56

    http://eglobiotraining.com/

    Codes and Explanations of the Programhave been tested

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    33/56

    Switch Case

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    34/56

    http://eglobiotraining.com/

    #include#includemain(){int choice;float x,y;printf("Enter the value of 1st:");scanf("%f",&x);

    printf("Enter the value of 2nd :");scanf("%f",&y);

    printf("1.Add\n");printf("2.Subtact\n");printf("3.Multification\n");printf("4.Division\n");printf("\n\n\nEnter your choice[1-4]: ");scanf("%d",&choice);

    switch(choice){case 1:

    printf("THE result is :%f",x+y);break;

    case 2:printf("The result is :%f",x-y);break;

    case 3:printf("The result is :%f",x*y);

    break;case 4:

    printf("The result is :%f",x/y);break;

    default:printf("unknown");}

    fflush(stdin);getchar();

    }

    Switch case 1

    #

    include

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    35/56

    http://eglobiotraining.com/

    using namespace std;

    int main(){cout number;switch(number){case 1: //if (number == 1)cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    36/56

    Switch case 3

    http://eglobiotraining.com/

    #include using namespace std;int main(void){

    char grade;cout > grade;switch (grade){case 'A':

    cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    37/56

    Switch case 4

    http://eglobiotraining.com/

    #include #include

    int main(void) {int n;printf("Please enter a number: ");

    scanf("%d", &n);switch (n) {

    case 1: {printf("n is equal to 1!\n");break;

    }case 2: {

    printf("n is equal to 2!\n");break;

    }case 3: {

    printf("n is equal to 3!\n");break;

    }default: {

    printf("n isn't equal to 1, 2, or 3.\n");break;

    }}system("PAUSE");return 0;

    }

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    38/56

    Switch case 5

    http://eglobiotraining.com/

    #include using namespace std;int main(void){int day;cout > day;switch(day){

    case 1:cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    39/56

    Looping

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    40/56

    Looping 1

    http://eglobiotraining.com/

    #include

    using namespace std; // So the program can see cout andendl

    int main(){// The loop goes while x < 10, and x increases by one

    every loopfor ( int x = 0; x < 10; x++ ) {// Keep in mind that the loop condition checks// the conditional statement before it loops again.// consequently, when x equals 10 the loop breaks.// x is updated before the condition is checked.cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    41/56

    Looping 2

    http://eglobiotraining.com/

    #include #include using namespace std;int main(){int x, score, tnum_stud, t_score, ave_score;

    char student_name[20];tnum_stud = 0, t_score = 0, ave_score = 0;cout>student_name;cout> score;t_score = t_score + score;for (x = 1; x score;t_score = t_score + score;}tnum_stud = x++;ave_score = t_score/tnum_stud;cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    42/56

    Looping 3

    http://eglobiotraining.com/

    #include

    int main(){

    int x = 0;do

    {cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    43/56

    Looping 4

    http://eglobiotraining.com/

    // custom countdown using while

    #include using namespace std;

    int main (){int n;cout > n;

    while (n>0) {cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    44/56

    Looping 5

    http://eglobiotraining.com/

    #include

    int main(){

    int x;while (x != 0){cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    45/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    46/56

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    47/56

    Switch case 2

    If you use an if statement in an if statement it is callednesting. Nesting if statements can make a program

    very complex, but sometimes there is no other way. Souse it wisely.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    48/56

    Switch case 3

    The if statement can be used to testconditions so that we can alter the flow of

    a program. In other words: if a specificstatement is true, execute someinstructions. If not true, execute theseinstructions.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    49/56

    The program's end functionality has not changed, onlythe method of implementation in the C++ sourcecode. The program reads user input from the

    keyboard until the return key is pressed. After the userinputs a number, that value is stored into the integervariable "n". The variable "n" is then tested by a switchstatement. If the user inputs the number 1, then "n is

    equal to 1!" is printed as shown below.

    http://eglobiotraining.com/

    Switch case 4

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    50/56

    Switch case 5

    In the above Control Structure example the"switch" statement is used to find the day of

    the week from the integer input got from theuser.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    51/56

    Looping 1

    This program is a very simple example of a for loop. x isset to zero, while x is less than 10 it calls cout

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    52/56

    Looping 2

    If the default block is last block in your switch order,

    then it isnt necessary to state break next to it.Consequence of falling through case labels in lack ofbreak order is that the same collection of orders isexecuted for more different case labels.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    53/56

    Looping 3

    Keep in mind that you must include a trailing semi-colon after thewhile in the above example. A common error is to forget that ado..while loop must be terminated with a semicolon (the other

    loops should not be terminated with a semicolon, adding to theconfusion). Notice that this loop will execute once, because itautomatically executes before checking the condition.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    54/56

    Looping 4

    When the program starts the user is prompted toinsert a starting number for the countdown. Thenthe while loop begins, if the value entered by theuser fulfills the condition n>0 (that n is greaterthan zero) the block that follows the condition willbe executed and repeated while the condition(n>0) remains being true.

    http://eglobiotraining.com/

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    55/56

    Looping 5http://eglobiotraining.com/

    The easiest way to think of the loop is that when it reaches thebrace at the end it jumps back up to the beginning of the loop,

    which checks the condition again and decides whether to repeatthe block another time, or stop and move to the next statementafter the block.

  • 8/11/2019 Switchcaseandlooping 121012074703 Phpapp01 (1)

    56/56

    Submitted by:

    Hidalgo, Patricia M.

    BM10203

    Submitted to:Prof. Erwin Globio

    http://eglobiotraining.com/