switch case and looping

46
Switch case and Looping http://eglobiotraining.com

Upload: chaastillas

Post on 22-May-2015

283 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Switch case and looping

http://eglobiotraining.com

Switch case and Looping

Page 2: Switch case and looping

http://eglobiotraining.com

What is Programming Language?

A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer.

Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely.

Page 3: Switch case and looping

http://eglobiotraining.com

A programming language is a notation for writing programs, which are specifications of a computation or algorithm.

Page 4: Switch case and looping

http://eglobiotraining.com

Function and target

A computer programming language is a language used to write computer programs, which involve a computer performing some kind of computation or algorithm and possibly control external devices such as printers, disk drives, robots, and so on.

Page 5: Switch case and looping

http://eglobiotraining.com

Abstractions

Programming languages usually contain abstractions for defining and manipulating data structures or controlling theflow of execution.

Page 6: Switch case and looping

http://eglobiotraining.com

As a student, I have learned that programming is difficult to understand because it has so many scripts and applications that can be used to run a program.

Page 7: Switch case and looping

http://eglobiotraining.com

First, I find programming very hard and confusing because it has lots of codes that you need to understand for you to run a program.

Page 8: Switch case and looping

http://eglobiotraining.com

Programming is a creative process done by programmers to instruct a computer on how to do a task. Programming languages let you use them in different ways, e.g. adding numbers, etc… or storing data on disk for later retrieval.

Page 9: Switch case and looping

http://eglobiotraining.com

You need to consider languages to run your own program. DEV C++ is the most language that we use in programming.

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

Page 11: Switch case and looping

http://eglobiotraining.com

switch ( <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 condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.

Page 12: Switch case and looping

http://eglobiotraining.com

The default case is optional, but it is wise to include it as it handles any unexpected cases. Switch statements serves as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user. 

Above is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.

Page 13: Switch case and looping

http://eglobiotraining.com

This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code. 

Page 14: Switch case and looping

http://eglobiotraining.com

Looping

LOOP is a pedagogical programming language designed by Uwe Schöning, along with GOTO and WHILE. The only operations supported in the language are assignment, addition and looping.

Page 15: Switch case and looping

http://eglobiotraining.com

A loop lets you write a very simple statement to produce a significantly greater result simply by repetition.

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

Page 16: Switch case and looping

http://eglobiotraining.com

Types of Looping

For While and Do

Page 17: Switch case and looping

http://eglobiotraining.com

FOR

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

Page 18: Switch case and looping

http://eglobiotraining.com

WHILE

Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Page 19: Switch case and looping

http://eglobiotraining.com

The basic structure:

While ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.

Page 20: Switch case and looping

http://eglobiotraining.com

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: Switch case and looping

http://eglobiotraining.com

Do While loop

The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. 

Having the test condition at the end, guarantees that the body of the loop always executes at least one time. 

Page 22: Switch case and looping

http://eglobiotraining.com

Format

do{     block of code;}while (test condition);

Page 23: Switch case and looping

http://eglobiotraining.com

Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true".

Page 24: Switch case and looping

http://eglobiotraining.com

Page 25: Switch case and looping

http://eglobiotraining.com

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 26: Switch case and looping

http://eglobiotraining.com

Codes and Explanations

Page 27: Switch case and looping

http://eglobiotraining.com

#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;}

Page 28: Switch case and looping

http://eglobiotraining.com

Looping statement 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 29: Switch case and looping

http://eglobiotraining.com

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 30: Switch case and looping

http://eglobiotraining.com

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 31: Switch case and looping

http://eglobiotraining.com

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 32: Switch case and looping

http://eglobiotraining.com

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

}

Page 33: Switch case and looping

http://eglobiotraining.com

Switch case 2

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

Page 34: Switch case and looping

http://eglobiotraining.com

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

Page 35: Switch case and looping

http://eglobiotraining.com

Switch case 4#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 36: Switch case and looping

http://eglobiotraining.com

Switch case 5#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 37: Switch case and looping

http://eglobiotraining.com

Output

Page 38: Switch case and looping

http://eglobiotraining.com

In this, I used the four basic mathematical expressions. MDAS ( Multiplication, Division, Addition and Subtraction.

Page 39: Switch case and looping

http://eglobiotraining.com

I got this when I just started to type this code #include <iostream> and enter the succeeding codes, compiled and run.

Page 40: Switch case and looping

http://eglobiotraining.com

Sometimes the program will not run because of some missing codes.

Page 41: Switch case and looping

http://eglobiotraining.com

I become sensitive and very responsible on what I’m typing to avoid errors.

Page 42: Switch case and looping

http://eglobiotraining.com

Page 43: Switch case and looping

http://eglobiotraining.com

While doing this, I experienced some technical problems or errors which challenges me.

Page 44: Switch case and looping

http://eglobiotraining.com

This shows the switch statement which evaluates all statements.

Page 45: Switch case and looping

http://eglobiotraining.com

Page 46: Switch case and looping

http://eglobiotraining.com

Submitted to:Professor Erwin Globio

Submitted by:Charlaine J. Astillas

BM 10203