• Like
Final requirement
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Final requirement


final requirement in programming. :)

final requirement in programming. :)

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Switch Case and Looping Statements Fae Loise SalosagcolFM10205 – Fundamentals of Programming :) Far Eastern University http://eglobiotraining.com
  • 2. Switch Statements :) http://eglobiotraining.com
  • 3. Switch CaseThe switch/case statement in programming usually isused to replace multiple if/else statements.The general syntax is: http://eglobiotraining.com
  • 4. Now, lets see it working: Lets say that we want to write a programthat will ask for a number between 1 and 5, and it will write outthe number in text format (1 = one, 2 = two, 3 = three ....).Using the if/else if statements in programming it will look like inthe 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 dont 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 cant 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 StatementIn programming, the switchstatement is almost the sameas an “if statement”. Theswitch statement can havemany conditions. You start theswitch statement with acondition in programming. Ifone of the variable equals thecondition, the instructions areexecuted. It is also possible toadd a default in programming.If none of the variable equalsthe condition the default willbe executed. See theexample:Note: break is used to exit the switch.  http://eglobiotraining.com
  • 8. Switch StatementIn this example, a break statement in programming follows each statement ofthe switch body. The break statement forces an exit from the statement bodyafter one statement is executed. The final break statement is not strictlynecessary, since control passes out of the body at the end of the compoundstatement, 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 constantexpressions, convertible to an integer value, which is unique withinthis switch statementIf the expression evaluates to a value, equal to the value of one ofthe defined constant_expressioni, the statement (if present) and allsubsequent statements (except default_statement, if present) areexecuted. If the value of the expression does not match any ofthe constant_expressions, the default_statement is executed ifpresent.It is useful to note, that if the execution in programming ofsubsequent statements is undesirable, the break statement can beused. In that case the execution of the switch statement terminates. http://eglobiotraining.com
  • 12. Loop Statements :)different types of Looping statementssuch 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 followingscript: beginning in main: (1). User assigns a value to n. (2). The while instructionchecks if (n>0). At this point there are two possibilities:1st. true: execute statement (step 3,) 2nd. false: jump statement. The programfollows in step 5.. (3). Execute statement: cout << n << ", "; --n; (prints out n on screenand decreases n by 1). (4). End of block. Return Automatically to step 2. (5). Continuethe 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 thesemicolon signs among them. Thus, for example we could write: for (;n<10;) if wewant to specify no initialization neither increase; or for (;n<10;n++) if we want toinclude an increase field but not an initialization. Optionally, using the commaoperator (,) we can specify more than one instruction in any of the fields included ina for loop, like in initialization, for example. The comma operator (,) inprogramming is an instruction separator, it serves to separate more than oneinstruction where only one instruction is generally expected. http://eglobiotraining.com
  • 17. For LoopFOR - 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 avalue or give a value to an already existing variable. Second, the conditiontells the program that while the conditional expression is true the loop shouldcontinue to repeat itself. The variable update section is the easiest way for afor 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 couldcall other functions that do nothing to the variable but still have a usefuleffect on the code. Notice that in programming a semicolon separates eachof these sections, that is important. Also note that every single one of thesections may be empty, though the semicolons still have to be there. Inprogramming, if the condition is empty, it is evaluated as true and the loopwill repeat until something else stops it. http://eglobiotraining.com
  • 18. For LoopThis program is a very simple example in programming of a forloop. 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 mindalso that the variable is incremented after the code in the loop isrun 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