Castro

18
Submitted by Jenine Rose V. Castro Programming http://eglobiotraining.com

Transcript of Castro

Page 1: Castro

http://eglobiotraining.com

Submitted by Jenine Rose V. Castro

Programming

Page 2: Castro

http://eglobiotraining.com

SWITCH CASELOOPING

TOPICS IN PROGRAMMING:

Page 3: Castro

http://eglobiotraining.com

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages.

LOOPING

Page 4: Castro

http://eglobiotraining.com

FOR WHILEDO WHILE

looping programming has three types:

Page 5: Castro

http://eglobiotraining.com

The statements in the for loop repeat continuously for aspecific number of times.  The while and do-while loops repeat until a certain condition is met.  The for loop repeats until a specific count is met.  Use a for loop when the number of repetition is know, or can be supplied by the user

FOR

Page 6: Castro

http://eglobiotraining.com

The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true.

WHILE

Page 7: Castro

http://eglobiotraining.com

In most computer programming languages, a do while loop, sometimes just called a while loop, is a control flow statement that allows code to be executed once based on a given Boolean condition

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.

Do while

Page 8: Castro

http://eglobiotraining.com

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using the switch case in the programming is outlined below. 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.

SWITCH CASE

Page 9: Castro

http://eglobiotraining.com

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. It is also included in several other types of Programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "go to", one of several labels). 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.

SWITCH CASE

Page 10: Castro

http://eglobiotraining.com

#include <iostream> #include <stdlib.h> using namespace std; void welcome(); char getChar(); void displayResponse(char choice); int main(int argc, char *argv[]) { char choice; // declares the choice variable welcome(); // This calls the welcome function choice = getChar(); // calls getChar and returns the value for choice displayResponse(choice); // passes choice to displayResponse function system("PAUSE"); return 0; } // end main // welcome function displays an opening message to // explain the program to the user void welcome() { cout << "This program displays different messages depending\n"; cout << "on which letter is entered by the user.\n"; cout << "Pick a letter a, b, c or d to see what\n"; cout << "the program will say.\n\n"; } // end of welcome function // getChar asks the user for a letter a, b or c. // The character is returned to where the function was called. char getChar() { char response; // declares variable called response cout << "Please type a letter a, b, c and d: "; // prompt for letter cin >> response; // gets input from user and assigns it to response return response; // sends back the response value } // end getChar function // displayResponse function takes the char variable and uses it // to determine which set of tasks will be performed. void displayResponse(char choice) { char again;

EXAMPLES OF SOURCE CODE IN C++ PROGRAMMING

// switch statement based on the choice variable switch (choice) // notice no semicolon { case 'A': // choice was the letter A case 'a': // choice was the letter a cout << "your awesome dude.\n\\n"; break; // this ends the statements for case A/a case 'B': // choice was the letter b case 'b': // choice was the letter b cout << "you will find your lovelife.\n\n"; break; // this ends the statements for case B/b case 'C': // choice was the letter C case 'c': // choice was the letter c cout << "your will won the lottery.\n\n"; break; // this ends the statements for case C/c case 'D': // choice was the letter D case 'd': // choice was the letter d cout << "your so ugly!!.\n\n"; break; // this ends the statements for case D/d default: // used when choice falls out of the cases covered above cout << "You didn't pick a letter a, b or c.\n\n"; again = getChar(); // gives the user another try displayResponse(again); // recalls displayResponse with new character break; } // end of switch statement } // end displayResponse function

Page 11: Castro

http://eglobiotraining.com

EXAMPLES OF SOURCE CODE IN C++ PROGRAMMING

#include <iostream.h> int main(void) { int x = 0; int y = 0; bool validNumber = false; while (validNumber == false) { cout << "Please enter an integer between 1 and 10: "; cin >> x; cout << "You entered: " << x << endl << endl; if ((x < 1) || (x > 10)) { cout << "Your value for x is not between 1 and 10!" << endl; cout << "Please re-enter the number!" << endl << endl; } else validNumber = true; } cout << "Thank you for entering a valid number!" << endl; return 0; }

Page 12: Castro

http://eglobiotraining.com

EXAMPLES OF SOURCE CODE IN C++ PROGRAMMING

#include <iostream> using namespace std; main() { int num1, num2; char again = 'y';

while (again == 'y' || again == 'Y') { cout << "Enter a number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; cout << "Their sum is " << (num1 + num2) << endl; cout << "Do you want to do this again? "; cin >> again; } return 0; }

Page 13: Castro

http://eglobiotraining.com

EXAMPLES OF SOURCE CODE IN C++ PROGRAMMING

#include <iostream>#include <cmath>using namespace std;

//prototypeint fallingdistance();

//main functionint main(){

int count = 1 ;int time;double distance ;cout << "Please enter time in 1

through 10 seconds.\n\n";

time = fallingdistance();

while ( time < 1 || time > 10){ cout << "Must enter

between 1 and 10 seconds, please re-enter.\n";

time = fallingdistance();

}

cout <<"\nSecondsfalling distance\n";cout <<"---------------------------------------\

n";

for ( count = 1; count <= time; count++)

{distance = .5 * 9.8 *

pow(time, 2.0);cout << count << "

" << distance <<" meters"<< endl;

} system ("pause");

return 0;}// falling distance function for a return value in seconds transfer to timeint fallingdistance (){

int seconds;cin >> seconds;return seconds;

}

Page 14: Castro

http://eglobiotraining.com

RUNNING SOURCE FILE IN C++ PROGRAMMING

Page 15: Castro

http://eglobiotraining.com

RUNNING SOURCE FILE IN C++ PROGRAMMING

Page 16: Castro

http://eglobiotraining.com

RUNNING SOURCE FILE IN C++ PROGRAMMING

Page 17: Castro

http://eglobiotraining.com

RUNNING SOURCE FILE IN C++ PROGRAMMING

Page 18: Castro

http://eglobiotraining.com

http://eglobiotraining.com

SUBMITTED TO: EDWIN GLOBIO