1. Switch case statements are a substitute for long if statements that
compare to an integral value. The basic format for using switch
case is outlined below.
switch ( value ) {
case this:
Code to execute if value == this
break;
case that:
Code to execute if value == that
break;
...
default:
Code to execute if value != this or that
break;
}
http://eglobiotraining.com.
2. 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. Sadly, it
isn't legal to use case like this:
http://eglobiotraining.com.
3. int a = 10;
int b = 10;
int c = 20;
switch ( a ) {
case b:
// Code
break;
case c:
// Code
break;
default:
// Code
break;
}
http://eglobiotraining.com.
4. The default case is optional, but it is wise to
include it as it handles any unexpected cases.
Switch statements serves as a simple way to
write long if statements when the
requirements are met. Often it can be used
to process input from a user.
Below is a sample program, in which not all
of the proper functions are actually declared,
but which shows how one would use switch
in a program.
http://eglobiotraining.com.
5. #include <iostream>
using namespace std;
void playgame();
void loadgame();
void playmultiplayer();
int main()
{
int input;
cout<<"1. Play gamen";
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
cout<<"4. Exitn";
cout<<"Selection: ";
cin>> input;
http://eglobiotraining.com.
6. switch ( input ) {
case 1: // Note the colon, not a semicolon
playgame();
break;
case 2: // Note the colon, not a semicolon
loadgame();
break;
case 3: // Note the colon, not a semicolon
playmultiplayer();
break;
case 4: // Note the colon, not a semicolon
cout<<"Thank you for playing!n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quittingn";
break;
}
cin.get();
}
http://eglobiotraining.com.
7. This program will compile, but cannot be run until
the undefined functions are given bodies, but it
serves as a model (albeit simple) for processing
input. If you do not understand this then try
mentally putting in if statements for the case
statements. Default simply skips out of the switch
case construction and allows the program to
terminate naturally. If you do not like that, then
you can make a loop around the whole thing to
have it wait for valid input. You could easily make
a few small functions if you wish to test the code.
http://eglobiotraining.com.
8. Here are some program that I use in switch case c programming
1)
#include<stdio.h>
main(){
float up, q, p;
double s, c;
printf("nnnntttt"Ku Ripot Company"");
printf("nnttEnter the Quantity:");
scanf("%f", &q);
printf("nnttEnter the unit price:");
scanf("%f", &up)
s = up*q;
http://eglobiotraining.com.
9. printf("nnntttThe total Sales of the Company is: %.2lf", s);
if(s>=20000){
c= s*(.20);
printf("nnnnttttThe Commission of the salesmen is: %.2lf", c);}
else if(s>15000&&s<=20000){
c= s*.15;
printf("nnnnttttThe Commission of the Salesmen is: %.2lf", c);}
else
{ c= s*.13;
printf("nnnnttttThe Commission of the Salesmen is: %.2lf", c);
}
getch();
}
. http://eglobiotraining.com.
10. In here I use a 'float' variable that may contain decimal values
other than integer, and may also be expressed as scientific
notation like q and p. In C programming and used printf or a word
operator in C programming, lastly I used scanf for breaking the
codes, so in this program I wanted to clarify Ku ripot’s Company
quantity and unit price by using C programming in dev C++ and
thus here is the result of my screen shot.
http://eglobiotraining.com
11. 2) #include<stdio.h>
main()
{
/*
int x,y;
printf("nt"POSSIBLE OUTPUT #1"");
printf("nnntttEnter a number:");
scanf("%d", &x);
x=x;
while (x<=5)
{
if (x<=5){
printf("%d", x);
x++;}
else {
printf("nnntttERROR!!!");}
}
*/
http://eglobiotraining.com
12. int x, sum ;
printf("nt"POSSIBLE OUTPUT #2"");
printf("nnntttEnter the number:");
scanf ("%d", &x);
x=x;
while (x<=5) {
if (x<=5){
printf("%d", x);
x++;}
else {
printf("nnntttERROR!!!");}
}
sum= x+x+x;
printf("nnntttThe total is: %d", sum);
if (sum%2==0) {
printf("nnttThis is an example of an EVEN NUMBER: %d", sum);}
else {
printf("nnttThis is an exaple of an ODD NUMBER: %d", sum);}
getch();
}
http://eglobiotraining.com
13. So for my second switch case program I just wanted to determine
the possible values using variable x and v in c programming and
run it in C programming using dev c++ since only the source codes
of c programming run in dev c++, and here
is the output.
http://eglobiotraining.com.
14. 3) #include<stdio.h>
main(){
float x, ld, id, td;
printf("nnnntttt"COMPUTER VISION"");
printf("nnntttEnter the amount bought to the Company:");
scanf("%f", &x);
if(x>=0 && x<=999 ) {
ld= x*.05;
printf("nnttYour Local Discount is: %.2f", ld);
id= x*.06;
printf("nnttYour Imported Discount is: %.2f", id);
td= ld+id;
printf("nnntttYour Total Discount is: %.2f", td);}
else if (x>=1000 && x<=1999 ) {
ld= x*.06;
http://eglobiotraining.com
15. printf("nnttYour Local Discount is: %.2f", ld);
id= x*.07;
printf("nnttYour Imported Discount is: %.2f", id);
td= ld+id;
printf("nnntttYour Total Discount is: %.2f", td);}
else if (x>=2000 && x<=3999 ){
ld= x*.07;
printf("nnttYour Local Discount is: %.2f", ld);
id= x*.08;
printf("nnttYour Imported Discount is: %.2f", id);
td= ld+id;
printf("nnntttYour Total Discount is: %.2f", td);}
else if (x>=4000 && x<=7999 ){
ld= x*.08;
http://eglobiotraining.com
16. printf("nnttYour Local Discount is: %.2f", ld);
id= x*.09;
printf("nnttYour Imported Discount is: %.2f", id);
td= ld+id;
printf("nnntttYour Total Discount is: %.2f", td);}
else {
ld= x*.10;
printf("nnttYour Local Discount is: %.2f", ld);
id= x*.10;
printf("nnttYour Imported Discount is: %.2f", id);
td= ld+id;
printf("nnntttYour Total Discount is: %.2f", td);}
getch();
http://eglobiotraining.com
17. So here by using C programming I found this program which is all
about computer vision and use to know the amount bought by
the company. Thus here is my output in C programming source
codes in dev C++.
http://eglobiotraining.com
18. 4) #include <iostream>
using namespace std;
void playgame()
{
cout << "Play game called";
}
void loadgame()
{
cout << "Load game called";
}
void playmultiplayer()
{
cout << "Play multiplayer game called";
}
http://eglobiotraining.com
19. int main()
{
int input;
cout<<"1. Play gamen";
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
cout<<"4. Exitn";
cout<<"Selection: ";
cin>> input;
switch ( input ) {
case 1: // Note the colon, not a semicolon
playgame();
break;
case 2: // Note the colon, not a semicolon
loadgame();
break;
case 3: // Note the colon, not a semicolon
playmultiplayer();
break;
http://eglobiotraining.com
20. case 4: // Note the colon, not a semicolon
cout<<"Thank you for playing!n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quittingn";
break;
}
cin.get();
}
http://eglobiotraining.com
21. Thus by using C programming I used this source code so used int
main () as the first function to be executed, and note the colon
not the semicolon and run the program play a game in dev C++
programming. So this is my output using dev C++ C programming
http://eglobiotraining.com
23. printf("n5. Quit");
printf("nEnter your choice [10-15]: ");
scanf("%i", &Choice);
switch(Choice)
{
case 1:
printf("nThe result is: %g", Value10*Value12);
break;
case 2:
printf("nThe result is: %g", Value10/Value12);
break;
case 3:
printf("nThe result is: %g", Value10-Value12);
break;
http://eglobiotraining.com
24. case 4:
printf("nThe result is: %g", Value10+Value12);
break;
case 5:
printf("nThank you");
break;
default:
printf("nInvalid choice!");
}
}while (Choice != 15);
} http://eglobiotraining.com
25. Thus by using C programming I used this source code so used
main () as the first function to be executed, and enter the values
to be substract,multiply, add or divide ,and run the program play a
game in dev C++ programming. So this is my output using dev C++
C programming.
http://eglobiotraining.com
27. Every loop will always contain three
main elements:
Priming: initialize your variables.
Testing: test against some
known condition.
Updating: update the variable
that is tested.
http://eglobiotraining.com
28. Type of Loops
Indefinite Loop:
You do not know ahead of time how many times your
loop will execute.
For example, you do not know how many books a person
might order.
Definite Loop:
You know exactly how many times you want the loop to
execute.
not at compile time necessarily
http://eglobiotraining.com
29. For loops
• Another type of loop in Java is the for
loop.
• It is very good for definite loops
• All the parts (priming, testing and updating)
are in one place.
• format:
for (prime expression; test expression; update expression)
• Since the expressions are all in one place,
many people prefer for to while when
the number of iterations is known.
• http://eglobiotraining.com
30. Basic For Loop Syntax
• for loops are good for creating definite
loops.
int counter;
1. Priming: Set 2. Test Condition: 3. Update: Update the
the start value. Set the stop value. value.
for (counter =1;counter <= 10;counter++)
System.out.println (counter);
http://eglobiotraining.com Note that each section is
separated by a semicolon.
31. for Loop Flowchart
1. Priming
Set counter=1
2. Test TRUE
counter 3a. print counter
<= 10 3b. Update counter++;
FALSE
http://eglobiotraining.com
32. Infinite Loop
• You can still end up with an infinite loop when
using for loops
for (counter = 0; counter <= 10; counter--)
http://eglobiotraining.com
33. For Loop Variations
• The limit can be a variable:
for ( i = 1; i <= limit; i++)
– This counts from 1 to limit
• Update may be negative:
for (i = 100; i >= 1; i--)
– This counts from 100 to 1.
• Update may be greater than 1:
for (i = 100; i >= 5; i -= 5)
– This counts from 100 to 5 in steps of 5
http://eglobiotraining.com
34. The for Structure: Notes and Observations
• Arithmetic expressions
– Initialization, loop-continuation, and increment can contain
arithmetic expressions. If x equals 2 and y equals 10
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
• Notes about the for structure:
– If the loop continuation condition is initially false
• The body of the for structure is not performed
• Control proceeds with the next statement after the for
structure
– Control variable
• Often printed or used inside for body, but not necessary
http://eglobiotraining.com
35. The Comma Operator
• Commas known here as comma operators
• by using commas, you can put more than one
statement in priming or updating expression
for (i = 100, y = 0; i >= 1; i--)
or
for (i = 1; j + i <= 10; i++, j++)
{
code;
}
• Commas known here as comma operators
http://eglobiotraining.com
36. Warnings
• Do not use a float or double for the counter
– May result in imprecise counter values and faulty
evaluation for loop termination purposes
• Don’t use commas instead of semicolons to
separate the components of the for loop
– (very common error)
• As in the if and while, do not put a semicolon ; right
after the parentheses – will be an empty loop!
http://eglobiotraining.com
37. Off-by-one error
• In the first example, shown here, if had
written
counter < 10
then loop would execute 9 times, not the
desired 10 times
for (counter = 1; counter <= 10; counter++)
{
System.out.println (counter);
}/* end for counter */
http://eglobiotraining.com
38. Help avoid off-by-one errors
• Try to make your conditions in the form <= not
<
– Avoid code like counter < 11 or counter < 10
http://eglobiotraining.com
39. Good Programming Practices
• Do not change the value of the counter variable in your loop.
– Do not attempt to manually adjust it to force an exit
– Can cause subtle errors that are difficult to catch
– Instead change your logic
• Do not put other expressions in the for control structure
– Manipulations of other variables should appear before or within the body
of the loop depending on whether or not you want to repeat them
• Put a blank line before and after each major control structure
• Try to limit nesting to three levels, if possible
– More than three levels of indentation can get confusing
• Limit the for control header to one line if possible
http://eglobiotraining.com
40. 1) #include<stdio.h>
main()
{
int x,y;
for(x=1;x<=3;x++){
for (y=1;y<=3;y++){
printf("*");
}
printf("n");
}
getch();
}
http://eglobiotraining.com
41. For looping I just used variable x, and y and printf as the variable operator in C
programming and thus here is the output for dev c++ C programming
http://eglobiotraining.com
42. 2) #include <iostream>
using namespace std;
int main()
{
int number = 0; // Variable for user to enter a number.
int sum = 0; // To hold the running sum during all loop iterations.
cout << "Enter a number: ";
cin >> number;
// Loop keeps adding until it reaches the number entered.
for (int index = 0; index <= number; index++)
{
sum += index; // Each iteration Adds to the variable sum.
}
// cout statement is put after the loop.
cout << "nThe sum of all numbers from 0 to " << number
<< " equals: " << sum << "nn";
return 0;
}
http://eglobiotraining.com
43. In my second attempt using C programming source code I used int main () and enter a
number and run the program in dev C++ programming. So this is my output using dev C++
C programming.
http://eglobiotraining.com
44. 3) #include <stdio.h> // including standard input output library
#include <conio.h> // including the conio.h file
#define MAX 10000 // defining a constant, MAX = 10000
int main() // main program
{
unsigned long long i, j, s ; // declaring variables
printf(" Perfect numbers till %d: n ", MAX) ; // outputting message
for(i = 2; i <= MAX; i++) // loop for the prime numbers
{
s=0; // s (sum) is declared and initialized with a value of zero
for(j = 1; j <= i/2; j++)
if(i % j == 0) // if the carry-over of i divided by j equals 0
s += j ; // j = s+j
if(i == s)
printf(" %5u ", i) ; // outputting the perfect numbers
}
getch();
} // end of program
http://eglobiotraining.com
45. Furthermore this is the result of my third attempt of using C programming I put the
question s like what is the perfect number till 10000 and run the program in dev C++
programming. So this is my output using dev C++ C programming.
http://eglobiotraining.com
46. 4) #include <iostream>
using namespace std;
int main(){
int myArray[10][10];
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
myArray[i][t] = i+t; //This will give each element a value
}
}
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
cout << myArray[i][t];
}
}
system("pause");
} http://eglobiotraining.com
47. Thus in my fourth program using C programming I used some source codes and include
array to give each element a value and after that run the program in dev C++
programming. So this ismy output using dev C++ C programming.
http://eglobiotraining.com
48. 5) #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
49. Lastly in my 5th looping program using C programming source codes and
ask a question on how many times do want the loop to run, and started
to run the program in dev C++ programming. So this is my output using
dev C++ C programming.
http://eglobiotraining.com