final requirement

20
http://eglobiotraining.com Fae Loise Salosagcol FM10205 – Fundamentals of Programming :) Far Eastern University Switch Case and Looping Statements

Upload: faye-salosagcol

Post on 14-Jul-2015

106 views

Category:

Documents


1 download

TRANSCRIPT

http://eglobiotraining.com

Fae Loise SalosagcolFM10205 – Fundamentals of Programming :)

Far Eastern University

Switch Case and Looping Statements

http://eglobiotraining.com

Switch Statements :)

http://eglobiotraining.com

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

Switch Case

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 statement:

break;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 StatementIn 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 StatementIn 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 theexample:

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 CaseIn 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

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.

While Loop

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

Do-While LoopFormat: 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 relative outputEnter number (0 to end): 12345

You entered:12345 Enter number (0 to end): 160277 You entered: 160277 Enter number (0 to end): 0

You entered: 0

The do-while loop in programming is usually used when the condition that has to determine its end is determined within the loop statement, like in the previous case, where the user input within the block of instructions is what determines the end of the loop. If you never enter the 0 value in the previous example the loop will never end.

http://eglobiotraining.com

For LoopIts format is: for (initialization; condition; increase) statement; 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