Survelaine murillo ppt

51
Switch Case and Looping A final requirement h t t p : / / e g l o b i o t r a i n i n g . c o m

description

a special presentation about Switch case and Looping statement. :)

Transcript of Survelaine murillo ppt

Page 1: Survelaine murillo ppt

Switch Case and Looping

A final requirement

http

://eglo

bio

train

ing.co

m

Page 2: Survelaine murillo ppt

What is Programming?Programming is instructing a computer to do something for you with the help of a programming language. The role of a programming language can be described in two ways:• Technical: It is a means for instructing a Computer to

perform Tasks• Conceptual: It is a framework within which we

organize our ideas about things and processes. is a creative process done by programmers to instruct a computer on how to do a task. Hollywood has helped instill an image of programmers as uber techies who can sit down at a computer and break any password in seconds or make highly tuned warp engines improve performance by 500% with just one tweak. Sadly the reality is far less interesting!

http

://eglo

bio

train

ing.co

m

Page 3: Survelaine murillo ppt

Programming skills are regarded as crucial to develop a

thriving economy (Silicon Valley being the prime proponent of said argument), but on a more fundamental level it teaches us skills that underline the contemporary condition. 

A programming language should both provide means to describe primitive data and procedures and means to combine and abstract those into more complex ones.

The distinction between data and procedures is not that clear cut. In many programming languages, procedures can be passed as data (to be applied to ``real'' data) and sometimes processed like ``ordinary'' data. Conversely ``ordinary'' data can be turned into procedures by an evaluation mechanism.

http

://eglo

bio

train

ing.co

m

Page 4: Survelaine murillo ppt

In the beginning , programming is quite confusing because you have so much to learn and understand about codes that will operate to run a program. Computer programming (often shortened to programming or coding) is the process of designing writing, testing, debugging and maintaining the source code of computer programs• Fundamentally programs manipulate numbers and text.

These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.

• These numbers and text are called variables and can be handled singly or in structured collections. In C++, a variable can be used to count numbers, or a struct) variable hold payroll details for an employee such as

http

://eglo

bio

train

ing.co

m

Page 5: Survelaine murillo ppt

A programming language is a notation for writing programs, which are specifications of a computation or algorithm . Some, but not all, authors restrict the term "programming language" to those languages that can express all possible algorithms

C++pronounced "see plus plus") is a statistically typed, free form, multi-paradigm, compiled, general-purpose programming language

C++ is one of the most popular programming languages and is implemented on a wide variety of hardware and operating system platforms.

http

://eglo

bio

train

ing.co

m

Page 6: Survelaine murillo ppt

Switch Case

In programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as ,Pascal, Ada, C/C+

+, C#, Java, and so on. The main reasons for using a switch include improving

clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

http

://eglo

bio

train

ing.co

m

Page 7: Survelaine murillo ppt

Basic Formation of Switch Case:http

://eglo

bio

train

ing.co

mswitch ( <variable> ) {case this-value: Code to execute if <variable> == this-value break;case that-value: Code to execute if <variable> == that-value\ break;...default: Code to execute if <variable> does not equal the value following any of the cases break;}

The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

Page 8: Survelaine murillo ppt

Typical Syntax In most languages, a switch statement is defined

across many individual lines using one or two keywords. A typical syntax is:

• The first line contains the basic keyword, usually switch, case or select, followed by an expression which is often referred to as the control expression or control variable of the switch statement.

• Subsequent lines define the actual cases (the values) with corresponding sequences of statements that should be executed when a match occurs.

http

://eglo

bio

train

ing.co

m

Page 9: Survelaine murillo ppt

Typical SyntaxIn many languages, every case must also be preceded by a keyword such as case or when. An optional default case is typically also allowed, specified by a default or else keyword; this is executed when none of the other cases matches the control expression.

In languages derived from C, a break keyword is used

to go to the end of the switch, thus completing execution of the switch statement. In such languages, program execution "falls through" to the statements associated with the next case in the source text when no break is present, thereby behaving like a GOTO mechanism.

http

://eglo

bio

train

ing.co

m

Page 10: Survelaine murillo ppt

Advantages : In some languages and programming environments,

the use of a case or switch statement is considered superior to an equivalent series of if-else statements because it is:

☺ easier to debug (e.g. setting breakpoints on code vs. a call table, if the debugger has no conditional breakpoint capability)

☺ easier to read (subjective) ☺ easier to understand and therefore ☺ easier to maintain ☺ faster execution potential

http

://eglo

bio

train

ing.co

m

Page 11: Survelaine murillo ppt

Simple examples that use switch statement the use examples C#

http

://eglo

bio

train

ing.co

m

switch (n) { case 0: Console.WriteLine("You typed zero."); break; case 1: case 4: case 9: Console.WriteLine("n is a perfect square."); break; case 2: Console.WriteLine("n is an even number."); goto case 3; case 3: case 5: case 7: Console.WriteLine("n is a prime number."); break; case 6: case 8: Console.WriteLine("n is an even number."); break; default: Console.WriteLine("Only single-digit numbers are allowed."); break; }

Page 12: Survelaine murillo ppt

Ruby uses case, when, and else

case n when 0 puts 'You typed zero' when 1, 9 puts 'n is a perfect square' when 2 puts 'n is a prime number' puts 'n is an even number' when 3, 5, 7 puts 'n is a prime number' when 4, 6, 8 puts 'n is an even number' else puts 'Only single-digit numbers are allowed‘ end

http

://eglo

bio

train

ing.co

m

Page 13: Survelaine murillo ppt

What is Looping?

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

certaincondition is reached. 

It is one of the three basic logic structures in computerprogramming. The other two logic structures are

selection and sequence.

http

://eglo

bio

train

ing.co

m

Page 14: Survelaine murillo ppt

Looping In computer programming a loop structure, the

program asks a question, and theanswer requires an action, it is performed and

theoriginal question is asked again until the answer

issuch that the action is no longer required.

Loops constitute one of the most basic and powerful

programming concepts.

http

://eglo

bio

train

ing.co

m

Page 15: Survelaine murillo ppt

Three types of looping:The For loop = This is the most common loop type. For loops are executed a

fixed number of times, determined by a count. They terminate when the count is exhausted.

The Repeat Loop = is used for loops where we do not know in advance how many

times we will execute. For example, when we keep asking a user for a value until one is provided, or the user aborts. Here, we are more concerned with the loop termination condition.

While loops = are very similar to Repeat loops except that they have the

exit condition at the start. This means that we use them when we wish to avoid loop execution altogether if the condition for exit is satisfied at the start.

http

://eglo

bio

train

ing.co

m

Page 16: Survelaine murillo ppt

FOR http

://eglo

bio

train

ing.co

m

For ( variable initialization; condition; variable update ) { Code to execute while the condition is true}

Page 17: Survelaine murillo ppt

For Loop• In computer programming for looping structure is a definite

repetition structure that makes use of a counter.•

• The three expressions in the for• loop have the following role:• –expression 1• is an arithmetic expression that initializes the counter,• –expression 2• is a logical expression that tests the counter against its final

value,• –expression 3• is an arithmetic expression that modifies the value of

the counter. Careful: there is no

http

://eglo

bio

train

ing.co

m

Page 18: Survelaine murillo ppt

Example:

http

://eglo

bio

train

ing.co

m

#include <iostream>

using namespace std; // So the program can see cout and endl

int main(){ // The loop goes while x < 10, and x increases by one every loop for ( 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<< x <<endl; } cin.get();}

This program is a very simple example 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.

Page 19: Survelaine murillo ppt

While LoopIn most computerprogramming languages,a while loop is a controlFlow statement that

allows codeto be executed repeatedly

basedon a

given boolean condition.

The while loop can be thought of

as a repeating if statement

http

://eglo

bio

train

ing.co

m

Page 20: Survelaine murillo ppt

#include <iostream>

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

int main(){ int x = 0; // Don't forget to declare variables

while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get();}

Example:

http

://eglo

bio

train

ing.co

m

In programming the easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block.

Page 21: Survelaine murillo ppt

DO..WHILEhttp

://eglo

bio

train

ing.co

m are useful for things that want to loop at least once.

The Structure:

do {} while ( condition ) ;

Page 22: Survelaine murillo ppt

DO..WHILE• In computer programming the do while loop works

same as the while loop and the loop is iterated as long as condition remains true. The do while loop checks the condition at the bottom of the loop while for and while loop checks the condition at the beginning of the loop and as a result the body of the loop is executed at least once. The general form of the do while loop is: -

•  • do{•    statement;• } while(condition);•  

http

://eglo

bio

train

ing.co

m

Page 23: Survelaine murillo ppt

Example:

http

://eglo

bio

train

ing.co

m

#include <iostream>

using namespace std;

int main(){ int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get();}

Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.

Page 24: Survelaine murillo ppt

CODES AND EXPLANATIONS OF THE PROGRAMS HAVE BEEN TESTED

http

://eglo

bio

train

ing.co

m

Page 25: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

#include <iostream> int main(){ using namespace std;  // nSelection must be declared outside do/while loop int nSelection;  do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4);  // do something with nSelection here // such as a switch statement  return 0;}

LOOPING STATEMENT 1

Page 26: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STAEMENT 2

#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2;  do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cin >> nSelection; }  while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4);  if (nSelection == 1) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1+var2) << endl; }

Page 27: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 3 if (nSelection == 2) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1-var2) << endl; } if (nSelection == 3) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1*var2) << endl; } if (nSelection == 4) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1/var2) << endl; }  return 0;}

Page 28: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 3

#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2; while (1) { do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cout << "5) Exit" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4 && nSelection != 5);  if (nSelection == 1) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1+var2) << endl; }

Page 29: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 4

else if (nSelection == 2) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1-var2) << endl; } else if (nSelection == 3) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1*var2) << endl; } else if (nSelection == 4) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1/var2) << endl; }else { return 0; } }}

Page 30: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 5

#include <iostream> using namespace std; // So the program can see cout and endl int main(){ // The loop goes while x < 10, and x increases by one every loop for ( 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<< x <<endl; } cin.get();}

Page 31: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 6

#include <iostream> using namespace std; int main(){ int x;  x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get();}

Page 32: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT 7

#include <iostream>using namespace std; int main (){int n;cout << "Enter the starting number > ";cin >> n; while (n>0) {cout << n << ", ";--n;} cout << "FIRE!\n";return 0;}

Page 33: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 1

 SWITCH CASE#include <iostream>  using namespace std;  int main () {  int score;    cout << "What was your score?";  cin >> score;    if (score <= 25)  {  cout << "\nOuch, less than 25...!";  }

Page 34: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 2

else if (score <= 50)  {  cout << "\nYou score aint great mate..";  }  else if (score <= 75)  {  cout << "\nYour pretty good, wel done man!";  }  else if (score <= 100)  {  cout << "\nYou got to the top!!!";  }

Page 35: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 3

else  {  cout << "\nYou cant score higher than 100!!! Cheater!!!!";  }    cin.ignore();  cin.get();    return 0; }

Page 36: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

#include <iostream> using namespace std; int main(){cout << "Enter a number between 1 and 5!" << endl;int number;cin >> number;if(number == 1){cout << "one";}else if(number == 2){cout << "two";}else if(number == 3){cout << "three";}else if(number == 4){cout << "four";}else if(number == 5){cout << "five";}else{cout << number << " is not between 1 and 5!";}cout << endl;system("pause");}

Page 37: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 5#include <iostream>using namespace std;int main(){int price_before_discount, RM, dozen, total_price; cout<< "How much is the price before discount for 1 dozen boxes of tissue?\n";cout<<"RM ";cin>>price_before_discount;cout<<"\n\n";  cout<< "How many dozen boxes of tissue you buy?\n";cin>>dozen;cout<<"\n\n";  switch (dozen){total_price = ((price_before_discount*dozen) * (95/100));case '1': cout<< "Total price is RM ";cout<<RM;cout<<"\n\n";break; total_price = ((price_before_discount*dozen) * (88/100));case '2': cout<< "Total price is RM ";cout<<RM;cout<<"\n\n";break; total_price = ((price_before_discount*dozen) * (75/100));case '3': cout<< "Total price is RM ";cout<<RM;cout<<"\n\n";break; total_price = ((price_before_discount*dozen) * (60/100));case '4' : cout<< "Total price is RM ";cout<<RM;cout<<"\n\n";break; total_price = ((price_before_discount*dozen) * (40/100));default : cout<< "Total price is RM ";cout<<RM;cout<<"\n\n"; } return 0;}

Page 38: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 6

#include <stdlib.h>#include <stdio.h> 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;}

Page 39: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

SWITCH CASE 7#include <iostream>using namespace std;int main(void){ char grade; cout << "Enter your grade: "; cin >> grade; switch (grade) { case 'A': cout << "Your average must be between 90 - 100" << endl; break; case 'B': cout << "Your average must be between 80 - 89" << endl; break; case 'C': cout << "Your average must be between 70 - 79" << endl; break; case 'D': cout << "Your average must be between 60 - 69" << endl; break; default: cout << "Your average must be below 60" << endl; } return 0;}

Page 40: Survelaine murillo ppt

AN OUTPUT PROGRAM USING DEV C++

http

://eglo

bio

train

ing.co

m

Page 41: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

In this looping statement, I used “while” looping, and I choose to show MDAS just as an example for the program to run.  If logical Expression evaluates to true, the statement executes.  The logical Expression is reevaluated.  The body of the loop continues to execute until the logical Expression is false

Page 42: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

I have came up with this by just starting to write this code: #include <iostream> and then enter the succeeding codes, compiled and run.

Page 43: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

I noticed that sometimes if the program does not run, it is because some braces are not included and I accidentally put braces on the same line and it causes the program not to read its contents. Programming is sensitive, when there is missing variable or braces or some words it does not run.

Page 44: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

When I learned that programming is very sensitive and at the same time very detailed when it comes to entering codes, I make sure that it is clear means that I put everything important codes in it so that the program would run.

Page 45: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

So much codes that should be entered that even the spaces are needed programming is very specific that whatever you have entered in to it you should specify because when the statement is false it wouldn’t let you run the program, I have experienced it before I arrived at this result.

Page 46: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

Because of so many experiences I had before this program run, I found programming is also interesting for the more you are practicing to make a program run, the more questions that came up in my mind and try something that will fit to this or entering new codes to make matrix etc… that I know is possible.

Page 47: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

In this switch case missing out a break statement causes control to fall through to the next case label. Switches can always be replaced by nested if-else statements, but in some cases this may be more clumsy. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement 

Page 48: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths, A switch works with the byte, short, char, and in  primitive data types.

Page 49: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

The switch statement evaluates its expression, then executes all statements that follow the matching case label.

Page 50: Survelaine murillo ppt

http

://eglo

bio

train

ing.co

m

The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the jump-statement transfers control out of the case body.

Page 51: Survelaine murillo ppt

Submitted by :Survelaine S. MurilloBM10203

http

://eglo

bio

train

ing.co

m

Submitted to:Prof. Erwin Globio