2. What is Programming?
Programming is instructing a computer to do something for
http://eglobiotraining.com
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.
is a creative process done by programmers to instruct a
computer on how to do a task. Hollywood has helped instill
an image of programmers as uber techies who can sit down
at a computer and break any password in seconds or make
highly tuned warp engines improve performance by 500%
with just one tweak. Sadly the reality is far less
interesting!
3. http://eglobiotraining.com
Programming skills are regarded as crucial to
develop a thriving economy (Silicon Valley being the prime
proponent of said argument), but on a more fundamental
level it teaches us skills that underline the contemporary
condition.
A programming language should both provide means
to describe primitive data and procedures and means to
combine and abstract those into more complex ones.
The distinction between data and procedures is not
that clear cut. In many programming languages, procedures
can be passed as data (to be applied to ``real'' data) and
sometimes processed like ``ordinary'' data. Conversely
``ordinary'' data can be turned into procedures by an
evaluation mechanism.
4. http://eglobiotraining.com
In the beginning , programming is quite confusing
because you have so much to learn and understand about
codes that will operate to run a program. Computer
programming (often shortened to programming or coding) is
the process of designing writing, testing, debugging and
maintaining the source code of computer programs
• Fundamentally programs manipulate numbers and
text. These are the building blocks of all programs.
Programming languages let you use them in different
ways, eg adding numbers, etc, or storing data on disk for
later retrieval.
• These numbers and text are called variables and can be
handled singly or in structured collections. In C++, a
variable can be used to count numbers, or a struct)
variable hold payroll details for an employee such as
5. http://eglobiotraining.com
A programming language is a notation for
writing programs, which are specifications of a computation
or algorithm . Some, but not all, authors restrict the term
"programming language" to those languages that can
express all possible algorithms
C++pronounced "see plus plus") is a statistically
typed, free form, multi-paradigm, compiled, general-
purpose programming language
C++ is one of the most popular programming languages and is
implemented on a wide variety of hardware and operating
system platforms.
6. Switch Case
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.
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.
7. Basic Formation of Switch Case:
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
http://eglobiotraining.com
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 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.
8. Typical Syntax
In most languages, a switch statement is defined across
http://eglobiotraining.com
many individual lines using one or two keywords. A typical
syntax is:
• The first line contains the basic keyword,
usually switch, case or select, followed by an expression
which is often referred to as the control
expression or control variable of the switch statement.
• Subsequent lines define the actual cases (the values)
with corresponding sequences of statements that should
be executed when a match occurs.
9. Typical Syntax
http://eglobiotraining.com
In many languages, every case must also be preceded by
a keyword such as case or when. An optional default case
is typically also allowed, specified by
a default or else keyword; this is executed when none of
the other cases matches the control expression.
In languages derived from C, a break keyword is used to
go to the end of the switch, thus completing execution
of the switch statement. In such languages, program
execution "falls through" to the statements associated
with the next case in the source text when no break is
present, thereby behaving like a GOTO mechanism.
10. Advantages :
In some languages and programming environments, the
http://eglobiotraining.com
use of a case or switch statement is considered superior
to an equivalent series of if-else statements because it
is:
☺ easier to debug (e.g. setting breakpoints on code
vs. a call table, if the debugger has no conditional
breakpoint capability)
☺ easier to read (subjective)
☺ easier to understand and therefore
☺ easier to maintain
☺ faster execution potential
11. Simple examples that use switch statement the use examples C#
switch (n)
{
case 0:
Console.WriteLine("You typed zero.");
break;
case 1:
case 4:
case 9:
Console.WriteLine("n is a perfect square.");
break;
http://eglobiotraining.com
case 2:
Console.WriteLine("n is an even number.");
goto case 3;
case 3:
case 5:
case 7:
Console.WriteLine("n is a prime number.");
break;
case 6:
case 8:
Console.WriteLine("n is an even number.");
break;
default:
Console.WriteLine("Only single-digit numbers are allowed.");
break;
}
12. Ruby uses case, when, and else
http://eglobiotraining.com
case n
when 0
puts 'You typed zero'
when 1, 9
puts 'n is a perfect square'
when 2
puts 'n is a prime number'
puts 'n is an even number'
when 3, 5, 7
puts 'n is a prime number'
when 4, 6, 8
puts 'n is an even number'
else
puts 'Only single-digit numbers are allowed„
end
13. What is Looping?
http://eglobiotraining.com
In computer programming, a loop is a sequence
of instructions that is continually repeated until a certain
condition is reached.
It is one of the three basic logic structures in computer
programming. The other two logic structures are selection
and sequence.
14. Looping
In computer programming a loop structure,
http://eglobiotraining.com
the program asks a question, and the
answer requires an action, it is performed
and the
original question is asked again until the
answer is
such that the action is no longer required.
Loops constitute one of the most basic and
powerful
programming concepts.
15. Three types of looping:
The For loop
http://eglobiotraining.com
= This is the most common loop type. For loops are executed a fixed
number of times, determined by a count. They terminate when the
count is exhausted.
The Repeat Loop
= is used for loops where we do not know in advance how many
times we will execute. For example, when we keep asking a user for
a value until one is provided, or the user aborts. Here, we are more
concerned with the loop termination condition.
While loops
= are very similar to Repeat loops except that they have the
exit condition at the start. This means that we use them when we
wish to avoid loop execution altogether if the condition for exit is
satisfied at the start.
16. FOR
http://eglobiotraining.com
For ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
17. For Loop
• In computer programming for looping structure is a definite
repetition structure that makes use of a counter.
•
http://eglobiotraining.com
• The three expressions in the for
• loop have the following role:
• –expression 1
• is an arithmetic expression that initializes the counter,
• –expression 2
• is a logical expression that tests the counter against its final
value,
• –expression 3
• is an arithmetic expression that modifies the value of
the counter. Careful: there is no
18. Example:
#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
http://eglobiotraining.com
// 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();
}
This program is a very simple example of a for loop. x is
set to zero, while x is less than 10 it calls cout<< x
<<endl; and it adds 1 to x until the condition is met. Keep
in mind also that the variable is incremented after the
code in the loop is run for the first time.
19. While Loop
In most computer
http://eglobiotraining.com
programming languages,
a while loop is a control
Flow statement that allows
code
to be executed repeatedly
based
on a given boolean condition.
The while loop can be
thought of
as a repeating if statement
20. Example:
#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
http://eglobiotraining.com
cout<< x <<endl;
x++; // Update x so the condition can be met eventually
}
cin.get();
}
In programming 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.
21. DO..WHILE
are useful for things that want to loop at least once.
The Structure:
do {
http://eglobiotraining.com
} while ( condition ) ;
22. DO..WHILE
• In computer programming the do while loop works same
http://eglobiotraining.com
as the while loop and the loop is iterated as long as
condition remains true. The do while loop checks the
condition at the bottom of the loop while for and while
loop checks the condition at the beginning of the loop
and as a result the body of the loop is executed at least
once. The general form of the do while loop is: -
•
• do{
• statement;
• } while(condition);
•
23. Example:
#include <iostream>
using namespace std;
int main()
{
int x;
http://eglobiotraining.com
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();
}
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.
25. LOOPING STATEMENT 1
#include <iostream>
int main()
{
using namespace std;
// nSelection must be declared outside do/while loop
int nSelection;
do
{
http://eglobiotraining.com
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;
}
26. LOOPING STAEMENT 2
#include <iostream>
using namespace std;
int main()
{
int nSelection;
double var1, var2;
do
{
cout << "Please make a selection: " << endl;
http://eglobiotraining.com
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;
}
27. 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 ";
http://eglobiotraining.com
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;
}
28. LOOPING STATEMENT 3
#include <iostream>
using namespace std;
int main()
{
int nSelection;
double var1, var2;
while (1)
{
do
{
http://eglobiotraining.com
cout << "Please make a selection: " << endl;
cout << "1) Addition" << endl;
cout << "2) Subtraction" << endl;
cout << "3) Multiplication" << endl;
cout << "4) Division" << endl;
cout << "5) Exit" << endl;
cin >> nSelection;
} while (nSelection != 1 && nSelection != 2 &&
nSelection != 3 && nSelection != 4 &&
nSelection != 5);
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;
}
29. 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)
{
http://eglobiotraining.com
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;
}
}
}
30. 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.
http://eglobiotraining.com
// consequently, when x equals 10 the loop breaks.
// x is updated before the condition is checked.
cout<< x <<endl;
}
cin.get();
}
31. LOOPING STATEMENT 6
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
http://eglobiotraining.com
do {
// "Hello, world!" is printed at least one time
// even though the condition is false
cout<<"Hello, world!n";
} while ( x != 0 );
cin.get();
}
32. LOOPING STATEMENT 7
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
http://eglobiotraining.com
cout << n << ", ";
--n;
}
cout << "FIRE!n";
return 0;
}
33. SWITCH CASE 1
SWITCH CASE
#include <iostream>
using namespace std;
int main ()
http://eglobiotraining.com
{
int score;
cout << "What was your score?";
cin >> score;
if (score <= 25)
{
cout << "nOuch, less than 25...!";
}
34. SWITCH CASE 2
else if (score <= 50)
{
cout << "nYou score aint great mate..";
}
else if (score <= 75)
http://eglobiotraining.com
{
cout << "nYour pretty good, wel done man!";
}
else if (score <= 100)
{
cout << "nYou got to the top!!!";
}
36. #include <iostream>
using namespace std;
int main(){
cout << "Enter a number between 1 and 5!" << endl;
int number;
cin >> number;
if(number == 1){
http://eglobiotraining.com
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");
}
37. SWITCH CASE 5
#include <iostream>
using namespace std;
int main()
{
int price_before_discount, RM, dozen, total_price;
cout<< "How much is the price before discount for 1 dozen boxes of tissue?n";
cout<<"RM ";
cin>>price_before_discount;
cout<<"nn";
cout<< "How many dozen boxes of tissue you buy?n";
cin>>dozen;
cout<<"nn";
http://eglobiotraining.com
switch (dozen)
{
total_price = ((price_before_discount*dozen) * (95/100));
case '1': cout<< "Total price is RM ";
cout<<RM;
cout<<"nn";
break;
total_price = ((price_before_discount*dozen) * (88/100));
case '2': cout<< "Total price is RM ";
cout<<RM;
cout<<"nn";
break;
total_price = ((price_before_discount*dozen) * (75/100));
case '3': cout<< "Total price is RM ";
cout<<RM;
cout<<"nn";
break;
total_price = ((price_before_discount*dozen) * (60/100));
case '4' : cout<< "Total price is RM ";
cout<<RM;
cout<<"nn";
break;
total_price = ((price_before_discount*dozen) * (40/100));
default : cout<< "Total price is RM ";
cout<<RM;
cout<<"nn";
}
return 0;
}
38. SWITCH CASE 6
#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");
http://eglobiotraining.com
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;
}
39. SWITCH CASE 7
#include <iostream>
using namespace std;
int main(void)
{
char grade;
cout << "Enter your grade: ";
cin >> grade;
switch (grade)
{
case 'A':
cout << "Your average must be between 90 - 100"
http://eglobiotraining.com
<< endl;
break;
case 'B':
cout << "Your average must be between 80 - 89"
<< endl;
break;
case 'C':
cout << "Your average must be between 70 - 79"
<< endl;
break;
case 'D':
cout << "Your average must be between 60 - 69"
<< endl;
break;
default:
cout << "Your average must be below 60" << endl;
}
return 0;
}
40. AN OUTPUT
http://eglobiotraining.com
PROGRAM USING DEV C++
41. http://eglobiotraining.com
In this looping statement, I used “while” looping, and I
choose to show MDAS just as an example for the program to
run. If logical Expression evaluates to true, the
statement executes. The logical Expression is
reevaluated. The body of the loop continues to execute until
the logical Expression is false
42. http://eglobiotraining.com
I have came up with this by just starting to write this
code: #include <iostream> and then enter the
succeeding codes, compiled and run.
43. http://eglobiotraining.com
I noticed that sometimes if the program does not run, it
is because some braces are not included and I
accidentally put braces on the same line and it causes
the program not to read its contents. Programming is
sensitive, when there is missing variable or braces or
some words it does not run.
44. http://eglobiotraining.com
When I learned that programming is very sensitive
and at the same time very detailed when it comes to
entering codes, I make sure that it is clear means
that I put everything important codes in it so that
the program would run.
45. http://eglobiotraining.com
So much codes that should be entered that even the
spaces are needed programming is very specific that
whatever you have entered in to it you should specify
because when the statement is false it wouldn‟t let you
run the program, I have experienced it before I arrived
at this result.
46. http://eglobiotraining.com
Because of so many experiences I had before this
program run, I found programming is also interesting
for the more you are practicing to make a program run,
the more questions that came up in my mind and try
something that will fit to this or entering new codes to
make matrix etc… that I know is possible.
47. http://eglobiotraining.com
In this switch case missing out a break statement causes
control to fall through to the next case label. Switches
can always be replaced by nested if-else statements, but
in some cases this may be more clumsy.
Each break statement terminates the
enclosing switch statement. Control flow continues with
the first statement
48. http://eglobiotraining.com
Unlike if-then and if-then-else statements,
the switch statement can have a number of possible
execution paths, A switch works with the byte, short, char,
and in primitive data types.
50. http://eglobiotraining.com
The switch statement can include any number
of case instances, but no two case constants within the
same switch statement can have the same value.
Execution of the statement body begins at the selected
statement and proceeds until the jump-
statement transfers control out of the case body.