SlideShare uma empresa Scribd logo
1 de 20
Switch Case and
              Looping Statements



                    Fae Loise Salosagcol
FM10205 – Fundamentals of Programming :)
                  Far Eastern University

  http://eglobiotraining.com
Switch Statements :)




          http://eglobiotraining.com
Switch Case
The switch/case statement in programming usually is
used to replace multiple if/else statements.
The general syntax is:




               http://eglobiotraining.com
Now, let's see it working: Let's say that we want to write a program
that will ask for a number between 1 and 5, and it will write out
the number in text format (1 = one, 2 = two, 3 = three ....).
Using the if/else if statements in programming it will look like in
the below example:




                   http://eglobiotraining.com
And using the switch/case            break;
statement:                           In programming, The break is used to break
                                     out from the switch/case statement.
                                     Without break the program will execute all
                                     the code after the selected one. For
                                     example, if we don't use breaks in the
                                     above code, and the user inputs 3, the
                                     program will show:threefourfive3 is not
                                     between 1 and 5! instead of three

                                     default:
                                     The default case in programming is
                                     optional, this is executed when none of the
                                     previous cases are executed or when you
                                     forget the break;.

                                     Note: The cases (including the default case)
                                     are followed by a colon, not semicolon!
                                     Note: This is very important in
                                     Programming! A variable can't be used as
                                     possible value of a case statement! Example
                                     of how NOT to do:

                                     int myvariable = 1;
                                     switch (x){
                                     case myvariable: //this is NOT valid
                                     //code to execute
                                     }
                http://eglobiotraining.com
Switch Statement
                         In programming, Switch Statement type of
                         conditional statement. It allows users when there
                         are many options available and only one option
                         should be executed.

                         Instead of using a series of if...else if statements in
                         programming we can use switch statement. The
                         switch statement controls a program by executing
                         statements. These statements depend on the value
                         of the expression. In programming, the value of the
                         expression should be integer or character data type.
                         It uses single expression for multiple options. It
                         cannot replace the nested if…else completely but it
                         can be helpful for users.

                         The value of the switch expression is compared with
                         each of the given case. When a match found then it
                         execute the statements. Otherwise it does not
                         execute the statements and then the default
                         statements are executed. In programming,
                         break statement is used to end a particular case. If
                         we do not use a break statement in programming
                         then there will be a problem that the remaining
                         cases will execute if they match the expression of
                         the switch including the default statement.




          http://eglobiotraining.com
Switch Statement
In programming, the switch
statement is almost the same
as an “if statement”. The
switch statement can have
many conditions. You start the
switch statement with a
condition in programming. If
one of the variable equals the
condition, the instructions are
executed. It is also possible to
add a default in programming.
If none of the variable equals
the condition the default will
be executed. See the
example:


Note: break is used to exit the switch. 

                   http://eglobiotraining.com
Switch Statement




In this example, a break statement in programming follows each statement of
the switch body. The break statement forces an exit from the statement body
after one statement is executed. The final break statement is not strictly
necessary, since control passes out of the body at the end of the compound
statement, but it is included for consistency.

                        http://eglobiotraining.com
Switch Statement




        http://eglobiotraining.com
Switch Case
                              In programming, the break
                              statement interrupts the flow
                              of control. We have seen in
                              switch statement that when a
                              true case is found, the flow of
                              control goes through every
                              statement downward. We
                              want that only statements of
                              true case should be executed
                              and the remaining should be
                              skipped. For this purpose, we
                              use the break statement. The
                              break statement is necessary
                              in switch structure, without it
                              in programming the switch
                              structure becomes illogic. As
                              without it all the statement
                              will execute after first match
                              case is found.


         http://eglobiotraining.com
Explanation:
Expression shall be an expression, convertible to an integer value.

All constant_expressions in programming shall be constant
expressions, convertible to an integer value, which is unique within
this switch statement

If the expression evaluates to a value, equal to the value of one of
the defined constant_expressioni, the statement (if present) and all
subsequent statements (except default_statement, if present) are
executed. If the value of the expression does not match any of
the constant_expressions, the default_statement is executed if
present.

It is useful to note, that if the execution in programming of
subsequent statements is undesirable, the break statement can be
used. In that case the execution of the switch statement terminates.


                   http://eglobiotraining.com
Loop Statements :)
different types of Looping statements
such as For, While, and Do




             http://eglobiotraining.com
While Loop                      The for loop is a very flexible C/C++ construct.
                                In programming, we also can use the count
                                down, decrementing the counter variable
                                instead of incrementing.

                                We can use counter other than 1, The
                                initialization expression can be omitted if the
                                test variable has been initialized previously in
                                the program.  However the semicolon must
                                still be used in the statement. The
                                initialization expression need not be an actual
                                initialization, it can be any valid C/C++
                                expression, the expression is executed once
                                when the for statement is first reached. he
                                incremented expression can be omitted as long
                                as the counter variable is updated within the
                                body of the for statement. 

                                The semicolon still must be included. The test
                                expression that terminates the loop can be any
                                C/C++ expression in programming.  As long as
                                it evaluates as true (non zero),
                                the for statement continues to execute.
                                Logical operators can be used to construct
                                complex test expressions.
                                An expression can be created by separating
                                two sub expressions with the comma
                                operator, and are evaluated (in left-to-right
                                order), and the entire expression evaluates to
                                the value of the right sub expression. Each
                                part of the for statement can be made to
                                perform multiple duties.

             http://eglobiotraining.com
While Loop                                        Loops in programming have as objective
                                                  to repeat a statement a certain number
                                                  of times or while a condition is fulfilled.
                                                  The while loop.
                                                  Its format is:
                                                  while (expression) statement and its
                                                  function in programming is simply to
                                                  repeat statement while expression is
                                                  true. For example, we are going to make a
                                                  program to count down using a while loop:

                                                  When the program starts the user is
                                                  prompted to insert a starting number for
                                                  the countdown. Then the while loop
                                                  begins, if the value entered by the user
                                                  fulfills the condition n>0 (that n be
                                                  greater than 0 ),the block of instructions
                                                  that follows will execute indefinite times
                                                  while the condition (n>0) remains being
                                                  true. 


All the process in the program above can be interpreted according to the following
script: beginning in main: (1). User assigns a value to n. (2). The while instruction
checks if (n>0). At this point there are two possibilities:
1st. true: execute statement (step 3,) 2nd. false: jump statement. The program
follows in step 5.. (3). Execute statement: cout << n << ", "; --n; (prints out n on screen
and decreases n by 1). (4). End of block. Return Automatically to step 2. (5). Continue
the program after the block: print out FIRE! and end of program.
                         http://eglobiotraining.com
Format:
Do-While Loop                         do statement while condition; Its
                                      functionality in programming is exactly
                                      the same as the while loop except
                                      that condition in the do-while is
                                      evaluated after the execution
                                      of statement instead of before,
                                      granting at least one execution
                                      of statement even if condition is
                                      never fulfilled. For example, the
                                      following program echoes any number
                                      you enter until you enter 0.

                                      The do-while loop in programming is
                                      usually used when the condition that has
 the relative output
                                      to determine its end is determined
                                      within the loop statement, like in the
 Enter number (0 to end): 12345
                                      previous case, where the user input
 You entered:12345
                                      within the block of instructions is what
 Enter number (0 to end): 160277
                                      determines the end of the loop. If you
 You entered: 160277                  never enter the 0 value in the previous
 Enter number (0 to end): 0           example the loop will never end.
 You entered: 0

                       http://eglobiotraining.com
Its format is:
 For Loop                                    for (initialization; condition; increase) state
                                             ment; and its main function is to
                                             repeat statement while condition remains
                                             true, like the while loop. But in
                                             addition, for provides places to specify
                                             an initialization instruction and
                                             an increase instruction. So this loop in
                                             programming is specially designed to perform
                                             a repetitive action with a counter. It works
                                             the following way:

                                             1, initialization is executed. Generally in
                                             programming it is a initial value setting for a
                                             counter variable. This is executed only once.
                                             2, condition is checked, if it is true the loop
                                             continues, otherwise the loop finishes
                                             and statement is skipped. 3, statement is
                                             executed. As usual, it can be either a single
                                             instruction or a block of instructions enclosed
                                             within curly brackets { }. 4, finally, whatever
                                             is specified in the increase field is executed
                                             and the loop gets back to step 2. > Here is an
                                             example of countdown using a for loop.

The initialization and increase fields are optional. They can be avoided but not the
semicolon signs among them. Thus, for example we could write: for (;n<10;) if we
want to specify no initialization neither increase; or for (;n<10;n++) if we want to
include an increase field but not an initialization. Optionally, using the comma
operator (,) we can specify more than one instruction in any of the fields included in
a for loop, like in initialization, for example. The comma operator (,) in
programming is an instruction separator, it serves to separate more than one
instruction where only one instruction is generally expected.
                         http://eglobiotraining.com
For Loop
FOR - for loops are the most useful type. The syntax for a for loop is 




The variable initialization allows you to either declare a variable and give it a
value or give a value to an already existing variable. Second, the condition
tells the program that while the conditional expression is true the loop should
continue to repeat itself. The variable update section is the easiest way for a
for loop to handle changing of the variable. It is possible to do things like x++,
x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could
call other functions that do nothing to the variable but still have a useful
effect on the code. Notice that in programming a semicolon separates each
of these sections, that is important. Also note that every single one of the
sections may be empty, though the semicolons still have to be there. In
programming, if the condition is empty, it is evaluated as true and the loop
will repeat until something else stops it.

                        http://eglobiotraining.com
For Loop




This program is a very simple example in programming of a for
loop. x is set to zero, while x is less than 10 it calls cout<< x
<<endl; and it adds 1 to x until the condition is met. Keep in mind
also that the variable is incremented after the code in the loop is
run for the first time. 
                   http://eglobiotraining.com
http://www.slideshare.net/fayesalosagcol




              http://eglobiotraining.com
Submitted to: Prof. Erwin M. Globio
 http://eglobiotraining.com




             http://eglobiotraining.com

Mais conteúdo relacionado

Mais procurados

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case StatementsDipesh Pandey
 
Qtp classes-in-mumbai
Qtp classes-in-mumbaiQtp classes-in-mumbai
Qtp classes-in-mumbaivibrantuser
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in javaAtul Sehdev
 
The Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionThe Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionPhilip Schwarz
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETUjwala Junghare
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java StatementsFernando Gil
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsMarkus Voelter
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in javaTalha Saleem
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_iiNico Ludwig
 

Mais procurados (20)

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Switch case in C++
Switch case in C++Switch case in C++
Switch case in C++
 
Qtp classes-in-mumbai
Qtp classes-in-mumbaiQtp classes-in-mumbai
Qtp classes-in-mumbai
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
The Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionThe Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary Version
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java Statements
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
 
Javascript
JavascriptJavascript
Javascript
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii
 

Destaque

Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 
Final requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary ClemenceFinal requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary Clemenceclemencebonifacio
 
Understanding micro elements in film openings
Understanding micro elements in film openingsUnderstanding micro elements in film openings
Understanding micro elements in film openingsteelendorblack
 
Photo Inventory Mendez, Jennifer
Photo Inventory Mendez, JenniferPhoto Inventory Mendez, Jennifer
Photo Inventory Mendez, Jenniferjenn_oh
 
Express Yourself - Question D
Express Yourself - Question DExpress Yourself - Question D
Express Yourself - Question DPri_Jain
 
Reacciones sigmatropicas
Reacciones sigmatropicasReacciones sigmatropicas
Reacciones sigmatropicasJhonatan Charry
 
El color de las piedras precisosa jhonathan charry
El color de las piedras precisosa jhonathan charryEl color de las piedras precisosa jhonathan charry
El color de las piedras precisosa jhonathan charryJhonatan Charry
 
sciPADS presentation @ JURE conference 2014 in Nicosia, Cyprus
sciPADS presentation @ JURE conference 2014 in Nicosia, CyprussciPADS presentation @ JURE conference 2014 in Nicosia, Cyprus
sciPADS presentation @ JURE conference 2014 in Nicosia, CyprusDica Lab
 
Presentación1.pptx mmmmmm
Presentación1.pptx mmmmmmPresentación1.pptx mmmmmm
Presentación1.pptx mmmmmmalex rey
 
Caso chernobil jhonathan charry
Caso chernobil jhonathan charryCaso chernobil jhonathan charry
Caso chernobil jhonathan charryJhonatan Charry
 
What kind of media institution might distribute your
What kind of media institution might distribute yourWhat kind of media institution might distribute your
What kind of media institution might distribute yourteelendorblack
 
Apb presentation eng leetanwong
Apb presentation eng leetanwongApb presentation eng leetanwong
Apb presentation eng leetanwongHong Wei Wong
 
Blakeleigh doucet 4th hour exam
Blakeleigh doucet 4th hour exam Blakeleigh doucet 4th hour exam
Blakeleigh doucet 4th hour exam danielleclamotte
 
Canavis d9 jhonathan charry
Canavis d9 jhonathan charryCanavis d9 jhonathan charry
Canavis d9 jhonathan charryJhonatan Charry
 

Destaque (20)

Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Final requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary ClemenceFinal requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary Clemence
 
180
180180
180
 
Hardware
HardwareHardware
Hardware
 
Understanding micro elements in film openings
Understanding micro elements in film openingsUnderstanding micro elements in film openings
Understanding micro elements in film openings
 
Photo Inventory Mendez, Jennifer
Photo Inventory Mendez, JenniferPhoto Inventory Mendez, Jennifer
Photo Inventory Mendez, Jennifer
 
Express Yourself - Question D
Express Yourself - Question DExpress Yourself - Question D
Express Yourself - Question D
 
Reacciones sigmatropicas
Reacciones sigmatropicasReacciones sigmatropicas
Reacciones sigmatropicas
 
El color de las piedras precisosa jhonathan charry
El color de las piedras precisosa jhonathan charryEl color de las piedras precisosa jhonathan charry
El color de las piedras precisosa jhonathan charry
 
sciPADS presentation @ JURE conference 2014 in Nicosia, Cyprus
sciPADS presentation @ JURE conference 2014 in Nicosia, CyprussciPADS presentation @ JURE conference 2014 in Nicosia, Cyprus
sciPADS presentation @ JURE conference 2014 in Nicosia, Cyprus
 
Present simple tutorial
Present simple tutorialPresent simple tutorial
Present simple tutorial
 
Presentación1.pptx mmmmmm
Presentación1.pptx mmmmmmPresentación1.pptx mmmmmm
Presentación1.pptx mmmmmm
 
Caso chernobil jhonathan charry
Caso chernobil jhonathan charryCaso chernobil jhonathan charry
Caso chernobil jhonathan charry
 
Reunió de 6è.
Reunió de 6è.Reunió de 6è.
Reunió de 6è.
 
What kind of media institution might distribute your
What kind of media institution might distribute yourWhat kind of media institution might distribute your
What kind of media institution might distribute your
 
Mia darville
Mia darvilleMia darville
Mia darville
 
Apb presentation eng leetanwong
Apb presentation eng leetanwongApb presentation eng leetanwong
Apb presentation eng leetanwong
 
Blakeleigh doucet 4th hour exam
Blakeleigh doucet 4th hour exam Blakeleigh doucet 4th hour exam
Blakeleigh doucet 4th hour exam
 
Papaverinas
PapaverinasPapaverinas
Papaverinas
 
Canavis d9 jhonathan charry
Canavis d9 jhonathan charryCanavis d9 jhonathan charry
Canavis d9 jhonathan charry
 

Semelhante a Final requirement

Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statementFarshidKhan
 
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
 
Lecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfLecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfSalmanKhurshid25
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Techglyphs
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptxAdrianVANTOPINA
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition StructureShahzu2
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)jakejakejake2
 

Semelhante a Final requirement (20)

Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Lecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfLecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdf
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
What is Switch Case?
What is Switch Case?What is Switch Case?
What is Switch Case?
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
C language 2
C language 2C language 2
C language 2
 

Final requirement

  • 1. Switch Case and Looping Statements Fae Loise Salosagcol FM10205 – Fundamentals of Programming :) Far Eastern University http://eglobiotraining.com
  • 2. Switch Statements :) http://eglobiotraining.com
  • 3. Switch Case The switch/case statement in programming usually is used to replace multiple if/else statements. The general syntax is: http://eglobiotraining.com
  • 4. Now, let's see it working: Let's say that we want to write a program that will ask for a number between 1 and 5, and it will write out the number in text format (1 = one, 2 = two, 3 = three ....). Using the if/else if statements in programming it will look like in the below example: http://eglobiotraining.com
  • 5. And using the switch/case break; statement: In programming, The break is used to break out from the switch/case statement. Without break the program will execute all the code after the selected one. For example, if we don't use breaks in the above code, and the user inputs 3, the program will show:threefourfive3 is not between 1 and 5! instead of three default: The default case in programming is optional, this is executed when none of the previous cases are executed or when you forget the break;. Note: The cases (including the default case) are followed by a colon, not semicolon! Note: This is very important in Programming! A variable can't be used as possible value of a case statement! Example of how NOT to do: int myvariable = 1; switch (x){ case myvariable: //this is NOT valid //code to execute } http://eglobiotraining.com
  • 6. Switch Statement In programming, Switch Statement type of conditional statement. It allows users when there are many options available and only one option should be executed. Instead of using a series of if...else if statements in programming we can use switch statement. The switch statement controls a program by executing statements. These statements depend on the value of the expression. In programming, the value of the expression should be integer or character data type. It uses single expression for multiple options. It cannot replace the nested if…else completely but it can be helpful for users. The value of the switch expression is compared with each of the given case. When a match found then it execute the statements. Otherwise it does not execute the statements and then the default statements are executed. In programming, break statement is used to end a particular case. If we do not use a break statement in programming then there will be a problem that the remaining cases will execute if they match the expression of the switch including the default statement. http://eglobiotraining.com
  • 7. Switch Statement In programming, the switch statement is almost the same as an “if statement”. The switch statement can have many conditions. You start the switch statement with a condition in programming. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default in programming. If none of the variable equals the condition the default will be executed. See the example: Note: break is used to exit the switch.  http://eglobiotraining.com
  • 8. Switch Statement In this example, a break statement in programming follows each statement of the switch body. The break statement forces an exit from the statement body after one statement is executed. The final break statement is not strictly necessary, since control passes out of the body at the end of the compound statement, but it is included for consistency. http://eglobiotraining.com
  • 9. Switch Statement http://eglobiotraining.com
  • 10. Switch Case In programming, the break statement interrupts the flow of control. We have seen in switch statement that when a true case is found, the flow of control goes through every statement downward. We want that only statements of true case should be executed and the remaining should be skipped. For this purpose, we use the break statement. The break statement is necessary in switch structure, without it in programming the switch structure becomes illogic. As without it all the statement will execute after first match case is found. http://eglobiotraining.com
  • 11. Explanation: Expression shall be an expression, convertible to an integer value. All constant_expressions in programming shall be constant expressions, convertible to an integer value, which is unique within this switch statement If the expression evaluates to a value, equal to the value of one of the defined constant_expressioni, the statement (if present) and all subsequent statements (except default_statement, if present) are executed. If the value of the expression does not match any of the constant_expressions, the default_statement is executed if present. It is useful to note, that if the execution in programming of subsequent statements is undesirable, the break statement can be used. In that case the execution of the switch statement terminates. http://eglobiotraining.com
  • 12. Loop Statements :) different types of Looping statements such as For, While, and Do http://eglobiotraining.com
  • 13. While Loop The for loop is a very flexible C/C++ construct. In programming, we also can use the count down, decrementing the counter variable instead of incrementing. We can use counter other than 1, The initialization expression can be omitted if the test variable has been initialized previously in the program.  However the semicolon must still be used in the statement. The initialization expression need not be an actual initialization, it can be any valid C/C++ expression, the expression is executed once when the for statement is first reached. he incremented expression can be omitted as long as the counter variable is updated within the body of the for statement.  The semicolon still must be included. The test expression that terminates the loop can be any C/C++ expression in programming.  As long as it evaluates as true (non zero), the for statement continues to execute. Logical operators can be used to construct complex test expressions. An expression can be created by separating two sub expressions with the comma operator, and are evaluated (in left-to-right order), and the entire expression evaluates to the value of the right sub expression. Each part of the for statement can be made to perform multiple duties. http://eglobiotraining.com
  • 14. While Loop Loops in programming have as objective to repeat a statement a certain number of times or while a condition is fulfilled. The while loop. Its format is: while (expression) statement and its function in programming is simply to repeat statement while expression is true. For example, we are going to make a program to count down using a while loop: When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n be greater than 0 ),the block of instructions that follows will execute indefinite times while the condition (n>0) remains being true.  All the process in the program above can be interpreted according to the following script: beginning in main: (1). User assigns a value to n. (2). The while instruction checks if (n>0). At this point there are two possibilities: 1st. true: execute statement (step 3,) 2nd. false: jump statement. The program follows in step 5.. (3). Execute statement: cout << n << ", "; --n; (prints out n on screen and decreases n by 1). (4). End of block. Return Automatically to step 2. (5). Continue the program after the block: print out FIRE! and end of program. http://eglobiotraining.com
  • 15. Format: Do-While Loop do statement while condition; Its functionality in programming is exactly the same as the while loop except that condition in the do-while is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following program echoes any number you enter until you enter 0. The do-while loop in programming is usually used when the condition that has the relative output to determine its end is determined within the loop statement, like in the Enter number (0 to end): 12345 previous case, where the user input You entered:12345 within the block of instructions is what Enter number (0 to end): 160277 determines the end of the loop. If you You entered: 160277 never enter the 0 value in the previous Enter number (0 to end): 0 example the loop will never end. You entered: 0 http://eglobiotraining.com
  • 16. Its format is: For Loop for (initialization; condition; increase) state ment; and its main function is to repeat statement while condition remains true, like the while loop. But in addition, for provides places to specify an initialization instruction and an increase instruction. So this loop in programming is specially designed to perform a repetitive action with a counter. It works the following way: 1, initialization is executed. Generally in programming it is a initial value setting for a counter variable. This is executed only once. 2, condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped. 3, statement is executed. As usual, it can be either a single instruction or a block of instructions enclosed within curly brackets { }. 4, finally, whatever is specified in the increase field is executed and the loop gets back to step 2. > Here is an example of countdown using a for loop. The initialization and increase fields are optional. They can be avoided but not the semicolon signs among them. Thus, for example we could write: for (;n<10;) if we want to specify no initialization neither increase; or for (;n<10;n++) if we want to include an increase field but not an initialization. Optionally, using the comma operator (,) we can specify more than one instruction in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) in programming is an instruction separator, it serves to separate more than one instruction where only one instruction is generally expected. http://eglobiotraining.com
  • 17. For Loop FOR - for loops are the most useful type. The syntax for a for loop is  The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that in programming a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. In programming, if the condition is empty, it is evaluated as true and the loop will repeat until something else stops it. http://eglobiotraining.com
  • 18. For Loop This program is a very simple example in programming of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.  http://eglobiotraining.com
  • 19. http://www.slideshare.net/fayesalosagcol http://eglobiotraining.com
  • 20. Submitted to: Prof. Erwin M. Globio  http://eglobiotraining.com http://eglobiotraining.com