SlideShare a Scribd company logo
1 of 49
Switch Case and Looping
Statement
Briñes, Aden Mae S.
BM10203
http://eglobiotraining.com
What is Programming?
“programming”, it is a computer language
programmers use to develop applications, scripts,
or other set of instructions for a computer to
execute.
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.
• http://eglobiotraining.com
As a student, I was a bit curious about my
subject but as time pass by and we had
new lessons I have learned that
programming is very broad because it
composes many applications, codes and
can be used to run a program.
A programming language both provide
means to describe primitive data and
procedures and means to combine and
abstract those into more complex ones.
http://eglobiotraining.com
At first, I had a hard time
understanding what programming is
because you have so much to read
and analyze about codes that will
enable to run a program.
Programming is a creative process
done by programmers to instruct a
computer on how to do a task.
• http://eglobiotraining.com
Switch Case
statements that 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 switch case is outlined below.
• http://eglobiotraining.com
To further understand switch case
here is the format:
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 ( <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;
}
• http://eglobiotraining.com
Condition
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
Break
It 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.
• http://eglobiotraining.com
The break is used to break out
of the 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
Looping
(Loops). They are used to repeat a block
of code. Being able to have your
program repeatedly execute a block of
code is one of the most basic but useful
tasks in programming -- many programs
or websites that produce extremely
complex output (such as a message
board) are really only executing a single
task many times.
• http://eglobiotraining.com
a loop lets you write a very
simple statement to
produce a significantly
greater result simply by
repetition.
• http://eglobiotraining.com
Three types of Loops:
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 loops are very simple.
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
DO WHILE are useful for things
that want to loop at least once.
do {
} while ( condition ) ;
• http://eglobiotraining.com
FOR, WHILE, DO
WHILE examples 
• http://eglobiotraining.com
FOR
#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
WHILE
#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();
}
• http://eglobiotraining.com
DO WHILE
#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();
}
• http://eglobiotraining.com
The examples and
their explanations 
• http://eglobiotraining.com
SWITCH CASE
EXAMPLES:
• http://eglobiotraining.com
Switch Case #1
#include <stdio.h>
#include <conio.h>
void1 playgame() // Creates a function/method called playgame
{
printf( "Play game called" ); // Displays the string found inside the statement
getch(); //getch function prompts the user to press a character and that character is not printed on
screen
}
void loadgame() // Creates a function/method called loadgame
{
printf( "Load game called" ); // Displays the string found inside the statement
getch(); //getch function prompts the user to press a character and that character is not printed on
screen
}
void playmultiplayer() // Creates a function/method called playmultiplayer
{
printf( "Play multiplayer game called" ); // Displays the string found inside the statement
getch(); //getch function prompts the user to press a character and that character is not printed on
screen
}
• http://eglobiotraining.com
Continuation:
int main() // Creates a function/method main to be called first upon running the program
{
int input; // Declares a variable name input to be used
printf( "1: Play gamen" ); //
printf( "2: Load gamen" ); //
printf( "3: Play multiplayern" ); // Displays string in order for the user to select
printf( "4: Exitn" ); //
printf( "Selection: " ); //
scanf( "%d", &input ); // Gets the user number input
switch ( input ) { // Start of the switch case statement based on the input variable
case 1: /* Note the colon, not a semicolon */
playgame(); //Returns playgame function/method if 1 is the value of input variable
break;
case 2:
loadgame(); //Returns playgame function/method if 2 is the value of input variable
break;
case 3:
playmultiplayer(); //Returns playgame function/method if 3 is the value of input variable
break;
case 4:
printf( "Thanks for playing!n" ); //Returns playgame function/method if 4 is the value of input variable
break;
default:
printf( "Bad input, quitting!n" ); //Returns playgame function/method if the value of input variable is not with the range from 1-4
break;
}
getchar();
return 0;
}
• http://eglobiotraining.com
Explanation:
/* The flow of this Dev C++ programming source code is the user is asked
to select a number from the given choices.
When the user has selected a number from the choices an output varies
from one another.
If 1 is selected, it displays a "Play game called".
If 2 is selected, it displays a "Load game called".
If 3 is selected, it displays a "Play multiplayer game called".
If 4 is selected, it displays a "Thanks for playing!"
And if none of the above are inputted by the user then "Bad input, quitting!"
would be displayed.
Using of functions/methods is a good way of programming. Since, you can
have different process/output without crowding a switch case statement
like this for example. programming using of functions/methods is not only
limited to switch case statements and it could also be very useful
depending on how a developer use it. */
• http://eglobiotraining.com
Switch Case #2
#include <stdio.h>
#include <conio.h>
int main() // Creates a function/method main to be called first upon running the program
{
int color = 1; // Creates a variable name called color with a value of 1
printf("Please choose a color(1: red,2: green,3: blue):n");
scanf("%d", &color); // Gets the input number
switch (color) {
case 1:
printf("You chose red colorn");
break;
case 2:
printf("You chose green colorn");
break;
case 3:
printf("You chose blue colorn");
break;
default:
printf("You did not choose any colorn");
}
getche();
return 0;
}
/* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number
inputted by the user.
If 1 is selected, it displays "You chose red color".
If 2 is selected, it displays "You chose green color".
If 3 is selected, it displays "You did not choose any color."
This is just a simple programming example about switch-case statements but a good start when it comes to programming. */
• http://eglobiotraining.com
Switch Case #3
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "A)tHouse MDn"
<< "B)tAmerican Idlen"
<< "C)tFamily Guynn";
cout << "What TV show do you like (Enter A, B, C)?: ";
cin >> letter;
cout << endl;
switch (toupper(letter))
{
case 'A' : cout << "Dr. House is a radical doctor!nn";
break;
case 'B' : cout << "Wannabe singers!nn";
break;
case 'C' : cout << "One of the craziest cartoons Ive ever seen.nn";
break;
default: cout << "Invalid choice.nn";
break;
}
return 0;
}
• http://eglobiotraining.com
Explanation:
As you can see in this C++ programming sample source
code it is a Switch-Case statement where the programs asks
the user to input a letter either
A, B or C and not a number. As you go along the source
code you can see at line 19 which is the case 'A', it will be
called when the user inputted
letter A.
On line 21, it is being called when the user inputted letter B.
On line 23, it is being called when the user inputted letter C.
And the default is only triggered when none of the choices
are inputted by the user. Oftenly, the default is used to
display an error like in this example
stating that "Invalid choice".
• http://eglobiotraining.com
Switch Case #4
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "MENUnn";
cout << "1." << "t" << "Lobstern";
cout << "2." << "t" << "Steakn";
cout << "3." << "t" << "Turkeyn";
cout << "4." << "t" << "Hambergern";
cout << "5." << "t" << "Vegetariannn";
cout << "Choose your dinner entree: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1: cout << "Lobster is my favorite! Dig in!nn";
break;
case 2: cout << "Yummy! Steak is great...n"
<< "but limit yourself to once a week," << endl
<< "or risk the chance of high cholesterol!nn";
break;
case 3: cout << "Turkey is healthier than steak! ...Enjoy!nn";
break;
case 4: cout << "Hamburger is another form of steak. :-)nn";
break;
case 5: cout << "Finally, a vegitarian is in the house!nn";
break;
default: cout << "Invalid number, please enter a number"
<< " from the entrees above.nn";
break;
}
return 0;
}
• http://eglobiotraining.com
Explanation:
As you can see in this C++ programming sample source code is a switch
case statement where the program asks an input from the user with the
choices given.
On case 1, it will output a string "Lobster is my favorite! Dig in!".
On case 2, it will output a string "Yummy! Steak is great... but limit yourself
to once a week, or risk the chance of high cholesterol!"
On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!"
On case 4, it will output a string "Hamburger is another form of steak. :-)"
On case 5, it will output a string "Finally, a vegitarian is in the house!"
And on the default in which none of the choices are inputted by the user it
will output a string "Invalid number, please enter a number from the entrees
above."
Practicing or trying some simple C++ programming source codes like this
could help you get a hold on how to use this more effectively.
• http://eglobiotraining.com
Switch Case #5
#include <stdio.h>
main() {
int grade;
printf ("Input grade [1-5]:");
scanf("%d", & grade);
switch (grade) {
case 1:
printf("Fail (F)n");break;
case 2:
printf("Bad (D)n");break;
case 3:
printf("Good (C)n");break;
case 4:
printf("Very Good (B)n");break;
case 5:
printf("Excellent (A)n");break;
default:
printf("You have inputted false graden");
break; // break isn’t necessary here
}
}
As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input
a number from 1 to 5.
If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)".
If the input number is 2 then the result will be "Bad (B)".
If the input number is 3 then the result will be "Good (C)".
If the input number is 4 then the result will be "Very Good (B)".
If the input number is 5 then the result will be "Excellent (A)".
If the input number is none of the choices then the output will be "You have inputted false grade".
This is one of the simplest example when it comes to C++ programming so it's better to try the code above or start with simple
programming examples
to get a better hold on how the program flows.
http://eglobiotraining.com
LOOPING
EXAMPLES: 
• http://eglobiotraining.com
Looping example #1
#include <iostream>
using namespace std;
int main()
{
for ( int x = 0; x < 10; x++ )
{
cout<< x <<endl;
}
cin.get();
}
This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can
see cout and endl.
The loop goes while x < 10, and x increases by one every loop.
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.
For each loop made, there is an output being displayed which is equal to the value of x on that current loop.
• http://eglobiotraining.com
Looping example #2
#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();
}
In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in
order the cout and endl.
After declaring the function main() a variable x is declared with a value of 0.
On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will
continue to loop
until the variable x meets the condition which is 10.
In order for the x to meet the condition, a statement x++ is used to update the value of x.
x++ statements is equal to (x = x + 1)
• http://eglobiotraining.com
Looping example #3
#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();
}
A do while loop is used in this programming sample source code.
This programming source code displays a string "Hello, world" until it meets the condition
(x != 0). It is likely the same with other looping statement where it loops until it meets its condition.
A good indention for each line of code when programming is a good practice especially when using
looping statements to avoid confusion.
• http://eglobiotraining.com
Looping example #4
#include <iostream>
using namespace std;
int main()
{
int counter, howmuch;
cin >> howmuch;
counter = 0;
do
{
counter++;
cout << counter << 'n';
}
while ( counter < howmuch);
return 0;
}
In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to start
with is a declaration of variables counter and howmuch.
Then on the line 8 is where the program asks the user on how many times does he want to loop the program.
Line 9, the developer sets a value to the counter variable which is 0.
Line 10, is where the Do While loop starts.
Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the
program encounters this line of code.
Line 13, it is where the value of counter is displayed of the current loop.
Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets the
condition, the looping stops.
A good indention for each line of code when programming is a good practice especially when using looping statements to avoid
confusion. Another good practice when programming is one must understand the flow of codes.
• http://eglobiotraining.com
Looping example #5
#include<stdio.h>
#include <conio.h>
int main() // Creates the main function/method or it is where the program starts first
{
int counter, howmuch; // Declares a counter and howmuch variable to be used in the while statement
printf("How many times would you like to loop? "); // Asks the user on how many times does he/she wants the program to loop.
scanf("%d", &howmuch); // Scans user's input
counter = 0; // Set the value of counter variable to 0
while ( counter < howmuch) // Start of while statement. The condition is if counter is lesser than the howmuch variable
then the loop continues.
{
counter++; // for each loop the counter variable adds 1 in its value
printf("%dn", counter); //Prints each result on the screen.
}
getche(); //getch function prompts the user to press a character and that character is not printed on screen
return 0; //returns a value of 0
}
/* In this source code, it is an example of a While looping statement where it retrieves the user's input on how many times does he/she wants
to loop the program. programming a code like this where indentions are really used is a good way or a good practice especially when it
comes to programming a looping statement.
• http://eglobiotraining.com
The Screenshot
output of my
program using
Dev C++ 
• http://eglobiotraining.com
Switch case 1
/* The flow of this Dev C++ programming source code is the user is asked to select a number from the given
choices.
When the user has selected a number from the choices an output varies from one another.
If 1 is selected, it displays a "Play game called".
If 2 is selected, it displays a "Load game called".
If 3 is selected, it displays a "Play multiplayer game called".
If 4 is selected, it displays a "Thanks for playing!"
And if none of the above are inputted by the user then "Bad input, quitting!" would be displayed.
Using of functions/methods is a good way of programming. Since, you can have different process/output without
crowding a switch case statement
like this for example. programming using of functions/methods is not only limited to switch case statements and it
could also be very useful
depending on how a developer use it. */
• http://eglobiotraining.com
Switch case 2
/* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number
inputted by the user.
If 1 is selected, it displays "You chose red color".
If 2 is selected, it displays "You chose green color".
If 3 is selected, it displays "You did not choose any color."
This is just a simple programming example about switch-case statements but a good start when it comes to programming. */
• http://eglobiotraining.com
Switch case 3
As you can see in this C++ programming sample source code it is a Switch-Case statement where the programs asks the user to
input a letter either
A, B or C and not a number. As you go along the source code you can see at line 19 which is the case 'A', it will be called when
the user inputted
letter A.
On line 21, it is being called when the user inputted letter B.
On line 23, it is being called when the user inputted letter C.
And the default is only triggered when none of the choices are inputted by the user. Oftenly, the default is used to display an error
like in this example
stating that "Invalid choice".
• http://eglobiotraining.com
Switch case 4
As you can see in this C++ programming sample source code is a switch case statement where the program asks an input from the user with the
choices given.
On case 1, it will output a string "Lobster is my favorite! Dig in!".
On case 2, it will output a string "Yummy! Steak is great... but limit yourself to once a week, or risk the chance of high cholesterol!"
On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!"
On case 4, it will output a string "Hamburger is another form of steak. :-)"
On case 5, it will output a string "Finally, a vegitarian is in the house!"
And on the default in which none of the choices are inputted by the user it will output a string "Invalid number, please enter a number from the
entrees above."
Practicing or trying some simple C++ programming source codes like this could help you get a hold on how to use this more effectively.
• http://eglobiotraining.com
Switch case 5
As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input a
number from 1 to 5.
If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)".
If the input number is 2 then the result will be "Bad (B)".
If the input number is 3 then the result will be "Good (C)".
If the input number is 4 then the result will be "Very Good (B)".
If the input number is 5 then the result will be "Excellent (A)".
If the input number is none of the choices then the output will be "You have inputted false grade".
This is one of the simplest example when it comes to C++ programming so it's better to try the code above or start with simple programming
examples
to get a better hold on how the program flows.
• http://eglobiotraining.com
Looping 1
This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can see cout and endl.
The loop goes while x < 10, and x increases by one every loop.
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.
For each loop made, there is an output being displayed which is equal to the value of x on that current loop.
• http://eglobiotraining.com
Looping 2
In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in order the cout
and endl.
After declaring the function main() a variable x is declared with a value of 0.
On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will continue to
loop
until the variable x meets the condition which is 10.
In order for the x to meet the condition, a statement x++ is used to update the value of x.
x++ statements is equal to (x = x + 1)
• http://eglobiotraining.com
Looping 3
A do while loop is used in this programming sample source code.
This programming source code displays a string "Hello, world" until it meets the condition
(x != 0). It is likely the same with other looping statement where it loops until it meets its condition.
A good indention for each line of code when programming is a good practice especially when using looping statements to avoid
confusion.
• http://eglobiotraining.com
Looping 4
In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to
start with is a declaration of variables counter and howmuch.
Then on the line 8 is where the program asks the user on how many times does he want to loop the program.
Line 9, the developer sets a value to the counter variable which is 0.
Line 10, is where the Do While loop starts.
Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the
program encounters this line of code.
Line 13, it is where the value of counter is displayed of the current loop.
Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets
the condition, the looping stops.
A good indention for each line of code when programming is a good practice especially when using looping statements to avoid
confusion. Another good practice when programming is one must understand the flow of codes.
• http://eglobiotraining.com
Looping 5
/* In this source code, it is an example of a While looping statement where it retrieves
the user's input on how many times does he/she wants
to loop the program. programming a code like this where indentions are really used is a
good way or a good practice especially when it
comes to programming a looping statement.
• http://eglobiotraining.com
My slideshare URL :
http://www.slideshare.net/aeden_brines
• http://eglobiotraining.com
Submitted to:
Prof. Erwin M. Globio
http://eglobiotraining.com/
Submitted by:
Briñes, Aden Mae S.
BM10203
• http://eglobiotraining.com

More Related Content

What's hot

Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
rurumedina
 

What's hot (18)

Loops in JavaScript
Loops in JavaScriptLoops in JavaScript
Loops in JavaScript
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | Learnbay
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angeli
 
Lesson 4 constant
Lesson 4  constantLesson 4  constant
Lesson 4 constant
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Bioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-filesBioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-files
 
Tutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng ver
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting output
 
PHP slides
PHP slidesPHP slides
PHP slides
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
 

Viewers also liked (10)

Lab # 3
Lab # 3Lab # 3
Lab # 3
 
Lab # 2
Lab # 2Lab # 2
Lab # 2
 
Function C++
Function C++ Function C++
Function C++
 
EE6702 Protection and Switchgear notes R2013
EE6702 Protection and Switchgear notes R2013EE6702 Protection and Switchgear notes R2013
EE6702 Protection and Switchgear notes R2013
 
Switchgear presentation
Switchgear presentationSwitchgear presentation
Switchgear presentation
 
Protection and Switchgear
Protection and SwitchgearProtection and Switchgear
Protection and Switchgear
 
Sumpners Test of Transformers
Sumpners Test of TransformersSumpners Test of Transformers
Sumpners Test of Transformers
 
Switchgear
SwitchgearSwitchgear
Switchgear
 
open circuit and short circuit test on transformer
open circuit and short circuit test on transformeropen circuit and short circuit test on transformer
open circuit and short circuit test on transformer
 
Sc and oc test on transformer
Sc and oc test on transformerSc and oc test on transformer
Sc and oc test on transformer
 

Similar to My programming final proj. (1)

Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
gerrell
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
kimberly_Bm10203
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
aprilyyy
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
mark-asoi
 
Final requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary ClemenceFinal requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary Clemence
clemencebonifacio
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
trish_maxine
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
jewelyngrace
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
Jaricka Angelyd Marquez
 

Similar to My programming final proj. (1) (20)

My final requirement
My final requirementMy final requirement
My final requirement
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
 
Final requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary ClemenceFinal requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary Clemence
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
 
Deguzmanpresentationprogramming
DeguzmanpresentationprogrammingDeguzmanpresentationprogramming
Deguzmanpresentationprogramming
 
Project
ProjectProject
Project
 

My programming final proj. (1)

  • 1. Switch Case and Looping Statement Briñes, Aden Mae S. BM10203 http://eglobiotraining.com
  • 2. What is Programming? “programming”, it is a computer language programmers use to develop applications, scripts, or other set of instructions for a computer to execute. 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. • http://eglobiotraining.com
  • 3. As a student, I was a bit curious about my subject but as time pass by and we had new lessons I have learned that programming is very broad because it composes many applications, codes and can be used to run a program. A programming language both provide means to describe primitive data and procedures and means to combine and abstract those into more complex ones. http://eglobiotraining.com
  • 4. At first, I had a hard time understanding what programming is because you have so much to read and analyze about codes that will enable to run a program. Programming is a creative process done by programmers to instruct a computer on how to do a task. • http://eglobiotraining.com
  • 5. Switch Case statements that 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 switch case is outlined below. • http://eglobiotraining.com
  • 6. To further understand switch case here is the format: 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 ( <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; } • http://eglobiotraining.com
  • 7. Condition 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
  • 8. Break It 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. • http://eglobiotraining.com
  • 9. The break is used to break out of the 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
  • 10. Looping (Loops). They are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. • http://eglobiotraining.com
  • 11. a loop lets you write a very simple statement to produce a significantly greater result simply by repetition. • http://eglobiotraining.com
  • 12. Three types of Loops: for While and do • http://eglobiotraining.com
  • 13. FOR For ( variable initialization; condition; variable update ) { Code to execute while the condition is true } • http://eglobiotraining.com
  • 14. WHILE loops are very simple. 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
  • 15. DO WHILE are useful for things that want to loop at least once. do { } while ( condition ) ; • http://eglobiotraining.com
  • 16. FOR, WHILE, DO WHILE examples  • http://eglobiotraining.com
  • 17. FOR #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
  • 18. WHILE #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(); } • http://eglobiotraining.com
  • 19. DO WHILE #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(); } • http://eglobiotraining.com
  • 20. The examples and their explanations  • http://eglobiotraining.com
  • 22. Switch Case #1 #include <stdio.h> #include <conio.h> void1 playgame() // Creates a function/method called playgame { printf( "Play game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen } void loadgame() // Creates a function/method called loadgame { printf( "Load game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen } void playmultiplayer() // Creates a function/method called playmultiplayer { printf( "Play multiplayer game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen } • http://eglobiotraining.com
  • 23. Continuation: int main() // Creates a function/method main to be called first upon running the program { int input; // Declares a variable name input to be used printf( "1: Play gamen" ); // printf( "2: Load gamen" ); // printf( "3: Play multiplayern" ); // Displays string in order for the user to select printf( "4: Exitn" ); // printf( "Selection: " ); // scanf( "%d", &input ); // Gets the user number input switch ( input ) { // Start of the switch case statement based on the input variable case 1: /* Note the colon, not a semicolon */ playgame(); //Returns playgame function/method if 1 is the value of input variable break; case 2: loadgame(); //Returns playgame function/method if 2 is the value of input variable break; case 3: playmultiplayer(); //Returns playgame function/method if 3 is the value of input variable break; case 4: printf( "Thanks for playing!n" ); //Returns playgame function/method if 4 is the value of input variable break; default: printf( "Bad input, quitting!n" ); //Returns playgame function/method if the value of input variable is not with the range from 1-4 break; } getchar(); return 0; } • http://eglobiotraining.com
  • 24. Explanation: /* The flow of this Dev C++ programming source code is the user is asked to select a number from the given choices. When the user has selected a number from the choices an output varies from one another. If 1 is selected, it displays a "Play game called". If 2 is selected, it displays a "Load game called". If 3 is selected, it displays a "Play multiplayer game called". If 4 is selected, it displays a "Thanks for playing!" And if none of the above are inputted by the user then "Bad input, quitting!" would be displayed. Using of functions/methods is a good way of programming. Since, you can have different process/output without crowding a switch case statement like this for example. programming using of functions/methods is not only limited to switch case statements and it could also be very useful depending on how a developer use it. */ • http://eglobiotraining.com
  • 25. Switch Case #2 #include <stdio.h> #include <conio.h> int main() // Creates a function/method main to be called first upon running the program { int color = 1; // Creates a variable name called color with a value of 1 printf("Please choose a color(1: red,2: green,3: blue):n"); scanf("%d", &color); // Gets the input number switch (color) { case 1: printf("You chose red colorn"); break; case 2: printf("You chose green colorn"); break; case 3: printf("You chose blue colorn"); break; default: printf("You did not choose any colorn"); } getche(); return 0; } /* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number inputted by the user. If 1 is selected, it displays "You chose red color". If 2 is selected, it displays "You chose green color". If 3 is selected, it displays "You did not choose any color." This is just a simple programming example about switch-case statements but a good start when it comes to programming. */ • http://eglobiotraining.com
  • 26. Switch Case #3 #include <iostream> using namespace std; int main() { char letter; cout << "A)tHouse MDn" << "B)tAmerican Idlen" << "C)tFamily Guynn"; cout << "What TV show do you like (Enter A, B, C)?: "; cin >> letter; cout << endl; switch (toupper(letter)) { case 'A' : cout << "Dr. House is a radical doctor!nn"; break; case 'B' : cout << "Wannabe singers!nn"; break; case 'C' : cout << "One of the craziest cartoons Ive ever seen.nn"; break; default: cout << "Invalid choice.nn"; break; } return 0; } • http://eglobiotraining.com
  • 27. Explanation: As you can see in this C++ programming sample source code it is a Switch-Case statement where the programs asks the user to input a letter either A, B or C and not a number. As you go along the source code you can see at line 19 which is the case 'A', it will be called when the user inputted letter A. On line 21, it is being called when the user inputted letter B. On line 23, it is being called when the user inputted letter C. And the default is only triggered when none of the choices are inputted by the user. Oftenly, the default is used to display an error like in this example stating that "Invalid choice". • http://eglobiotraining.com
  • 28. Switch Case #4 #include <stdio.h> #include <iostream> using namespace std; int main() { int choice; cout << "MENUnn"; cout << "1." << "t" << "Lobstern"; cout << "2." << "t" << "Steakn"; cout << "3." << "t" << "Turkeyn"; cout << "4." << "t" << "Hambergern"; cout << "5." << "t" << "Vegetariannn"; cout << "Choose your dinner entree: "; cin >> choice; cout << endl; switch (choice) { case 1: cout << "Lobster is my favorite! Dig in!nn"; break; case 2: cout << "Yummy! Steak is great...n" << "but limit yourself to once a week," << endl << "or risk the chance of high cholesterol!nn"; break; case 3: cout << "Turkey is healthier than steak! ...Enjoy!nn"; break; case 4: cout << "Hamburger is another form of steak. :-)nn"; break; case 5: cout << "Finally, a vegitarian is in the house!nn"; break; default: cout << "Invalid number, please enter a number" << " from the entrees above.nn"; break; } return 0; } • http://eglobiotraining.com
  • 29. Explanation: As you can see in this C++ programming sample source code is a switch case statement where the program asks an input from the user with the choices given. On case 1, it will output a string "Lobster is my favorite! Dig in!". On case 2, it will output a string "Yummy! Steak is great... but limit yourself to once a week, or risk the chance of high cholesterol!" On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!" On case 4, it will output a string "Hamburger is another form of steak. :-)" On case 5, it will output a string "Finally, a vegitarian is in the house!" And on the default in which none of the choices are inputted by the user it will output a string "Invalid number, please enter a number from the entrees above." Practicing or trying some simple C++ programming source codes like this could help you get a hold on how to use this more effectively. • http://eglobiotraining.com
  • 30. Switch Case #5 #include <stdio.h> main() { int grade; printf ("Input grade [1-5]:"); scanf("%d", & grade); switch (grade) { case 1: printf("Fail (F)n");break; case 2: printf("Bad (D)n");break; case 3: printf("Good (C)n");break; case 4: printf("Very Good (B)n");break; case 5: printf("Excellent (A)n");break; default: printf("You have inputted false graden"); break; // break isn’t necessary here } } As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input a number from 1 to 5. If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)". If the input number is 2 then the result will be "Bad (B)". If the input number is 3 then the result will be "Good (C)". If the input number is 4 then the result will be "Very Good (B)". If the input number is 5 then the result will be "Excellent (A)". If the input number is none of the choices then the output will be "You have inputted false grade". This is one of the simplest example when it comes to C++ programming so it's better to try the code above or start with simple programming examples to get a better hold on how the program flows. http://eglobiotraining.com
  • 32. Looping example #1 #include <iostream> using namespace std; int main() { for ( int x = 0; x < 10; x++ ) { cout<< x <<endl; } cin.get(); } This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can see cout and endl. The loop goes while x < 10, and x increases by one every loop. 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. For each loop made, there is an output being displayed which is equal to the value of x on that current loop. • http://eglobiotraining.com
  • 33. Looping example #2 #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(); } In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in order the cout and endl. After declaring the function main() a variable x is declared with a value of 0. On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will continue to loop until the variable x meets the condition which is 10. In order for the x to meet the condition, a statement x++ is used to update the value of x. x++ statements is equal to (x = x + 1) • http://eglobiotraining.com
  • 34. Looping example #3 #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(); } A do while loop is used in this programming sample source code. This programming source code displays a string "Hello, world" until it meets the condition (x != 0). It is likely the same with other looping statement where it loops until it meets its condition. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. • http://eglobiotraining.com
  • 35. Looping example #4 #include <iostream> using namespace std; int main() { int counter, howmuch; cin >> howmuch; counter = 0; do { counter++; cout << counter << 'n'; } while ( counter < howmuch); return 0; } In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to start with is a declaration of variables counter and howmuch. Then on the line 8 is where the program asks the user on how many times does he want to loop the program. Line 9, the developer sets a value to the counter variable which is 0. Line 10, is where the Do While loop starts. Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the program encounters this line of code. Line 13, it is where the value of counter is displayed of the current loop. Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets the condition, the looping stops. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. Another good practice when programming is one must understand the flow of codes. • http://eglobiotraining.com
  • 36. Looping example #5 #include<stdio.h> #include <conio.h> int main() // Creates the main function/method or it is where the program starts first { int counter, howmuch; // Declares a counter and howmuch variable to be used in the while statement printf("How many times would you like to loop? "); // Asks the user on how many times does he/she wants the program to loop. scanf("%d", &howmuch); // Scans user's input counter = 0; // Set the value of counter variable to 0 while ( counter < howmuch) // Start of while statement. The condition is if counter is lesser than the howmuch variable then the loop continues. { counter++; // for each loop the counter variable adds 1 in its value printf("%dn", counter); //Prints each result on the screen. } getche(); //getch function prompts the user to press a character and that character is not printed on screen return 0; //returns a value of 0 } /* In this source code, it is an example of a While looping statement where it retrieves the user's input on how many times does he/she wants to loop the program. programming a code like this where indentions are really used is a good way or a good practice especially when it comes to programming a looping statement. • http://eglobiotraining.com
  • 37. The Screenshot output of my program using Dev C++  • http://eglobiotraining.com
  • 38. Switch case 1 /* The flow of this Dev C++ programming source code is the user is asked to select a number from the given choices. When the user has selected a number from the choices an output varies from one another. If 1 is selected, it displays a "Play game called". If 2 is selected, it displays a "Load game called". If 3 is selected, it displays a "Play multiplayer game called". If 4 is selected, it displays a "Thanks for playing!" And if none of the above are inputted by the user then "Bad input, quitting!" would be displayed. Using of functions/methods is a good way of programming. Since, you can have different process/output without crowding a switch case statement like this for example. programming using of functions/methods is not only limited to switch case statements and it could also be very useful depending on how a developer use it. */ • http://eglobiotraining.com
  • 39. Switch case 2 /* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number inputted by the user. If 1 is selected, it displays "You chose red color". If 2 is selected, it displays "You chose green color". If 3 is selected, it displays "You did not choose any color." This is just a simple programming example about switch-case statements but a good start when it comes to programming. */ • http://eglobiotraining.com
  • 40. Switch case 3 As you can see in this C++ programming sample source code it is a Switch-Case statement where the programs asks the user to input a letter either A, B or C and not a number. As you go along the source code you can see at line 19 which is the case 'A', it will be called when the user inputted letter A. On line 21, it is being called when the user inputted letter B. On line 23, it is being called when the user inputted letter C. And the default is only triggered when none of the choices are inputted by the user. Oftenly, the default is used to display an error like in this example stating that "Invalid choice". • http://eglobiotraining.com
  • 41. Switch case 4 As you can see in this C++ programming sample source code is a switch case statement where the program asks an input from the user with the choices given. On case 1, it will output a string "Lobster is my favorite! Dig in!". On case 2, it will output a string "Yummy! Steak is great... but limit yourself to once a week, or risk the chance of high cholesterol!" On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!" On case 4, it will output a string "Hamburger is another form of steak. :-)" On case 5, it will output a string "Finally, a vegitarian is in the house!" And on the default in which none of the choices are inputted by the user it will output a string "Invalid number, please enter a number from the entrees above." Practicing or trying some simple C++ programming source codes like this could help you get a hold on how to use this more effectively. • http://eglobiotraining.com
  • 42. Switch case 5 As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input a number from 1 to 5. If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)". If the input number is 2 then the result will be "Bad (B)". If the input number is 3 then the result will be "Good (C)". If the input number is 4 then the result will be "Very Good (B)". If the input number is 5 then the result will be "Excellent (A)". If the input number is none of the choices then the output will be "You have inputted false grade". This is one of the simplest example when it comes to C++ programming so it's better to try the code above or start with simple programming examples to get a better hold on how the program flows. • http://eglobiotraining.com
  • 43. Looping 1 This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can see cout and endl. The loop goes while x < 10, and x increases by one every loop. 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. For each loop made, there is an output being displayed which is equal to the value of x on that current loop. • http://eglobiotraining.com
  • 44. Looping 2 In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in order the cout and endl. After declaring the function main() a variable x is declared with a value of 0. On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will continue to loop until the variable x meets the condition which is 10. In order for the x to meet the condition, a statement x++ is used to update the value of x. x++ statements is equal to (x = x + 1) • http://eglobiotraining.com
  • 45. Looping 3 A do while loop is used in this programming sample source code. This programming source code displays a string "Hello, world" until it meets the condition (x != 0). It is likely the same with other looping statement where it loops until it meets its condition. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. • http://eglobiotraining.com
  • 46. Looping 4 In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to start with is a declaration of variables counter and howmuch. Then on the line 8 is where the program asks the user on how many times does he want to loop the program. Line 9, the developer sets a value to the counter variable which is 0. Line 10, is where the Do While loop starts. Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the program encounters this line of code. Line 13, it is where the value of counter is displayed of the current loop. Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets the condition, the looping stops. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. Another good practice when programming is one must understand the flow of codes. • http://eglobiotraining.com
  • 47. Looping 5 /* In this source code, it is an example of a While looping statement where it retrieves the user's input on how many times does he/she wants to loop the program. programming a code like this where indentions are really used is a good way or a good practice especially when it comes to programming a looping statement. • http://eglobiotraining.com
  • 48. My slideshare URL : http://www.slideshare.net/aeden_brines • http://eglobiotraining.com
  • 49. Submitted to: Prof. Erwin M. Globio http://eglobiotraining.com/ Submitted by: Briñes, Aden Mae S. BM10203 • http://eglobiotraining.com