Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Requirement in fundamentals of programming
1. Switch Case and Looping Statements
http://eglobiotraining.com
2. In a the world of programming, switch case statement
is a multi-way decision statement.
It 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.
Unlike the multiple decision statement that can be
created using if-else, the switch statement evaluates
the conditional expression and tests it against
numerous constant values.
The value of the expressions in a switch-case
statement must be an ordinal type i.e.
integer, char, short, long, etc. in programming.
Float and double are not allowed in this type of
programming.
http://eglobiotraining.com
3. #include <iostream>
#include <stdlib.h> // to determine which set of tasks will be performed.
using namespace std; void displayResponse(int choice)
void welcome(); {
int getInteger(); int again;
void displayResponse(int choice); // switch statement based on the choice variable
int main(int argc, char *argv[]) switch (choice) // notice no semicolon
{ {
int choice; // declares the choice variable case 1: // choice was the number 1
welcome(); // This calls the welcome function cout << "One is a lonely number and very useful in math.nn";
choice = getInteger(); // calls getInteger and receives the value for choice break; // this ends the statements for case 1
displayResponse(choice); // passes choice to displayResponse function case 2: // choice was the number 2
system("PAUSE"); cout << "Two is the only even prime number.nn";
return 0; break; // this ends the statements for case 2
} // end main case 3: // choice was the number 3
// welcome function displays an opening message to cout << "Three is a crowd and also a prime number.nn";
// explain the program to the user break; // this ends the statements for case 3
void welcome() case 4: // choice was the number 4
{ cout << "Four square is a fun game to play, but four squared is ";
cout << "This program displays different messages dependingn"; cout << 4 * 4 << ".nn";
cout << "on which number is entered by the user.n"; break; // this ends the statements for case 4
cout << "Pick a number between 1 and 6 to see whatn"; case 5: // choice was the number 5
cout << "the program will say.nn"; cout << "Counting by fives is fun. Five, Ten, Fifteen, Twenty...nn";
} // end of welcome function break; // this ends the statements for case 5
// getInteger asks the user for a number between 1 and 6. case 6: // choice was the number 6
// The integer is returned to where the function was called. cout << "Six is divisible by two and three.nn";
int getInteger() break; // this ends the statements for case 6
{ default: // used when choice falls out of the cases covered above
int response; // declares variable called response cout << "You didn't pick a number between 1 and 6.nn";
cout << "Please type a number between 1 and 6: "; // prompt for number again = getInteger(); // gives the user another try
cin >> response; // gets input from user and assigns it to response displayResponse(again); // recalls displayResponse with new number
return response; // sends back the response value break;
} // end getInteger function } // end of switch statement
// displayResponse function takes the int variable and uses it } // end displayResponse function
http://eglobiotraining.com
5. This program displays different messages
depending on which number is entered by the
user. Pick a number between 1 to 6 to see
what the program will say. This is executed in
the C++ programming software.
http://eglobiotraining.com/
6. #include <iostream> return response; // sends back the response value
#include <stdlib.h> } // end getChar function
using namespace std; // displayResponse function takes the char variable and uses it
void welcome(); // to determine which set of tasks will be performed.
char getChar(); void displayResponse(char choice)
void displayResponse(char choice); {
int main(int argc, char *argv[]) char again;
{ // switch statement based on the choice variable
char choice; // declares the choice variable switch (choice) // notice no semicolon
welcome(); // This calls the welcome function {
choice = getChar(); // calls getChar and returns the value for choice case 'A': // choice was the letter A
displayResponse(choice); // passes choice to displayResponse function case 'a': // choice was the letter a
system("PAUSE"); cout << "A is for apple.nn";
return 0; break; // this ends the statements for case A/a
} // end main case 'B': // choice was the letter b
// welcome function displays an opening message to case 'b': // choice was the letter b
// explain the program to the user cout << "B is for baseball.nn";
void welcome() break; // this ends the statements for case B/b
{ case 'C': // choice was the letter C
cout << "This program displays different messages dependingn"; case 'c': // choice was the letter c
cout << "on which letter is entered by the user.n"; cout << "C is for cat.nn";
cout << "Pick a letter a, b or c to see whatn"; break; // this ends the statements for case C/c
cout << "the program will say.nn"; default: // used when choice falls out of the cases covered above
} // end of welcome function cout << "You didn't pick a letter a, b or c.nn";
// getChar asks the user for a letter a, b or c. again = getChar(); // gives the user another try
// The character is returned to where the function was called. displayResponse(again); // recalls displayResponse with new character
char getChar() break;
{ } // end of switch statement
} // end displayResponse function
char response; // declares variable called response
cout << "Please type a letter a, b or c: "; // prompt for letter
cin >> response; // gets input from user and assigns it to response
http://eglobiotraining.com
8. This program displays different messages
depending on which letter is entered by the
user. Pick a letter: a, b or c to see what the
program will say. This is executed in the C++
programming software.
http://eglobiotraining.com/
9. #include <iostream> cout<<"4. Exitn";
cout<<"Selection: ";
using namespace std; cin>> input;
switch ( input ) {
void playgame() case 1: // Note the colon, not a semicolon
{ playgame();
cout << "Play game called"; break;
} case 2: // Note the colon, not a semicolon
void loadgame() loadgame();
{ break;
cout << "Load game called"; case 3: // Note the colon, not a semicolon
} playmultiplayer();
void playmultiplayer() break;
{ case 4: // Note the colon, not a semicolon
cout << "Play multiplayer game called"; cout<<"Thank you for playing!n";
} break;
default: // Note the colon, not a semicolon
int main() cout<<"Error, bad input, quittingn";
{ break;
int input; }
cin.get();
cout<<"1. Play gamen"; }
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
http://eglobiotraining.com
11. This program will compile, but cannot be run
until the undefined functions are given
bodies, but it serves as a model in computer
programming (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 programming construction and
allows the program to terminate naturally. If you
do not like that kind of programming
scheme, 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/
12. #include <stdio.h> case 'a':
#include <iostream> cout << "You typed in an A!n";
using namespace std; break;
case 'b':
int main() cout << "You typed in an B!n";
{ break;
int nr = 0;
char ch = 0; default:
cout << "You didn't type in a or b!n";
break;
//This uses numbers }
cout << "Type in number 1 or 2: ";
cin >> nr;
//This uses lowercase an uppercase characters
cout << "nnType in lowercase or uppercase a or b: ";
switch(nr) cin >> ch;
{
case 1: switch(ch)
cout << "The number typed was 1!n"; {
break; case 'a': case 'A':
cout << "You typed in an A!n";
case 2: break;
cout << "The number typed was 2!n";
break; case 'b': case 'B':
cout << "You typed in an B!n";
default: break;
cout << "You didn't type in 1 or 2!n";
break; default:
cout << "You didn't type in a or b!n";
}
break;
}
//This uses lowercase characters only
getchar(); // wait for a key to be pressed.
cout << "nn Type in character a or b: ";
cin >> ch;
return 0;
}
switch(ch)
{ http://eglobiotraining.com
14. In this program, the user will be asked to type
number 1 or 2. When the user types in 1 or 2,
the program will display either of the two
numbers. This is executed in the C++
programming software.
http://eglobiotraining.com/
15. #include <iostream.h>
int main()
{
unsigned short int number;
cout << "Enter a number between 1 and 5: ";
cin >> number;
switch (number)
{
case 0: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!n"; // fall through
case 4: cout << "Nice Pick!n"; // fall through
case 3: cout << "Excellent!n"; // fall through
case 2: cout << "Masterful!n"; // fall through
case 1: cout << "Incredible!n";
break;
default: cout << "Too large!n";
break;
}
cout << "nn";
return 0;
}
http://eglobiotraining.com
17. In programming, the user is prompted for a number.
That number is given to the switch statement. If the
number is 0, the case statement on line 13
matches, the message Too small, sorry! is
printed, and the break statement ends the switch. If
the value is 5, execution switches to line 15 where a
message is printed, and then falls through to line
16, another message is printed, and so forth until
hitting the break on line 20.
The net effect of these statements in this
programming is that for a number between 1 and
5, that many messages are printed. If the value of
number is not 0-5, it is assumed to be too large, and
the default statement in programming is invoked on
line 21.
http://eglobiotraining.com
18. while ( expression )
statement
In a while loop of computer programming, the expression is evaluated. If
nonzero, the statement executes, and the expression is evaluated again. This
happens over and over until the expression's value is zero. If the expression
is zero the first time it is evaluated, statement is not executed at all.
do
statement
while ( expression);
A do while loop is just like a plain while loop, except the statement executes
before the expression is evaluated. Thus, the statement will always be
evaluated at least once in programming.
http://eglobiotraining.com
19. for ( expression1; expression2; expression3 )
statement
In a for loop in computer programming, first expression1 is
evaluated. Then expression2 is evaluated, and if it is zero EEL
leaves the loop and begins executing instructions after
statement. Otherwise the statement is executed, expression3
is evaluated, and expression2 is evaluated again, continuing
until expression2 is zero.
You can omit any of the expressions in programming. If you
omit expression2, it is like expression2 is nonzero. while
(expression) is the same as for (; expression; ). The syntax for
(;;) creates an endless loop that must be exited using the
break statement
http://eglobiotraining.com
20. #include <iostream>
using namespace std;
int main(){
int a;
cout << "How many times do you want the loop to run? ";
cin >> a;
while (a){
cout << a << "n";
--a;
}
return 0;
}
http://eglobiotraining.com
22. In computer programming language, his code
takes a value from the user and runs a while
loop that many times. The conditions used
for the while loop are the same as the if-
then-else statements, same goes for every
loop. In this programming language, since I
only put "a" the program will read "While a is
true execute this block" and as long as a is a
positive integer it is considered to be 'true'.
http://eglobiotraining.com
23. #include <iostream.h>
int main()
{
int counter;
cout << "How many hellos?: ";
cin >> counter;
while (counter > 0)
{
cout << "Hello!n";
counter--;
}
cout << "Counter is OutPut: " << counter;
return 0;
}
http://eglobiotraining.com
25. In programming, the user of is prompted for
a starting value on line 10. This starting value
is stored in the integer variable counter. The
value of counter is tested on line 12, and
decremented in the body of the while loop.
The first time through counter was set to
2, and so the body of the while loop ran
twice. The second time through, however, the
user typed in 0. The value of counter was
tested on line 12 and the condition was false;
counter was not greater than 0. The entire
body of the while loop in programming was
skipped, and Hello was never printed.
http://eglobiotraining.com
26. #include <iostream>
for (int t = 0; t <=9; ++t){
using namespace std;
cout << myArray[i][t];
int main(){
}
int myArray[10][10];
for (int i = 0; i <= 9; ++i){ }
for (int t = 0; t <=9; ++t){ system("pause");
myArray[i][t] = i+t; //This }
will give each element a value
}
}
for (int i = 0; i <= 9; ++i){
http://eglobiotraining.com
28. Here the first for loop of programming defines i
as an integer with a value of 0. Since the array is
10x10 and 0 counts when counting the elements
of an array, we will run the loop of computer
programming until i is equal or greater than 9.
"++i" means "add 1 to i", it can be used with any
numeric data type. Since the array is two
dimensional we will need a second for the loop to
get the second index number. This is setup the
exact same way, except I used a t instead of an i.
So now every time the first for loop runs in the
C++ programming, the second will run 10
times, and then return to the first until the first
has been run 10 times thus covering every
element in the array.
http://eglobiotraining.com
29. #include <iostream.h>
int main()
{
int counter;
cout << "How many hellos? ";
cin >> counter;
do
{
cout << "Hellon";
counter--;
} while (counter >0 );
cout << "Counter is: " << counter << endl;
return 0;
}
http://eglobiotraining.com
31. The user is prompted for a starting value on line 9,
which is stored in the integer variable counter. In the
do...while loop of computer programming, the body
of the loop is entered before the condition is tested,
and therefore the body of the loop is guaranteed to
run at least once. On line 13 the message is printed,
on line 14 the counter is decremented, and on line 15
the condition is tested. If the condition of the made
programming language evaluates TRUE, execution
jumps to the top of the loop on line 13; otherwise, it
falls through to line 16.
The continue and break statements in computer
programming work in the do...while loop exactly as
they do in the while loop. The only difference
between a while loop and a do...while loop in
computer programming is when the condition is
tested.
http://eglobiotraining.com
32. #include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
http://eglobiotraining.com
34. The do statement in programming is similar
to the while statement except that its
termination condition is at the end of the
body of the loop only. Thus, you want to use
a do statement in programming, if you want
to perform the body of the loop at least
once, regardless of the condition.
http://eglobiotraining.com/