switch case and looping

Post on 22-May-2015

283 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

http://eglobiotraining.com

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.

http://eglobiotraining.com

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

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.

http://eglobiotraining.com

Abstractions

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

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.

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.

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.

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". 

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.

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.

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. 

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.

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).

http://eglobiotraining.com

Types of Looping

For While and Do

http://eglobiotraining.com

FOR

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

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.

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.

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.

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. 

http://eglobiotraining.com

Format

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

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".

http://eglobiotraining.com

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.

http://eglobiotraining.com

Codes and Explanations

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

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

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

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

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();}

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...!";

}

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!!!";  }

http://eglobiotraining.com

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

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");}

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

http://eglobiotraining.com

Output

http://eglobiotraining.com

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

http://eglobiotraining.com

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

http://eglobiotraining.com

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

http://eglobiotraining.com

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

http://eglobiotraining.com

http://eglobiotraining.com

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

http://eglobiotraining.com

This shows the switch statement which evaluates all statements.

http://eglobiotraining.com

http://eglobiotraining.com

Submitted to:Professor Erwin Globio

Submitted by:Charlaine J. Astillas

BM 10203

top related