1. In this presentation, the switch case and looping in C
language will be discussed.
• Sample codes wil be given
• Screenshots of output will be shown
• Steps will be explained
http://eglobiotraining.com/
3. How to Construct a Basic FOR Loop in the C
Language
The core of most modern programming, including those in
the C language, is the loop. A loop gives a program the
ability to repeat a group of statements, sometimes for a
given count or duration, or, often, until a certain condition
is met. The C language gives you many ways to create
loops in your code, but the most common is the for loop.
http://eglobiotraining.com/
4. LOOPS:
To execute a set of instructions repeatedly until a particular
condition is being satisfied.
Three types of looping statements in programming are :
1) For Loop
2) While Loop
3) Do while Loop
For Loop:
In for looping statement allows a number of
lines represent until the condition is
satisfied WHILE LOOP:
In While looping statement allows a
number of lines represent until the
condition is satisfied
DO WHILE LOOP:
In DO WHILE LOOP first execute the
statements then it checks the condition.
http://eglobiotraining.com/
5. CONCLUSION:
In FOR LOOP:
No need to initialize variable before the LOOP
In WHILE LOOP:
To initialize the variable before the LOOP
Increment/decrement the variable within the LOOP
In DO WHILE LOOP:
Once it execute If the condition is TRUE/FALSE.
http://eglobiotraining.com/
7. #include <stdio.h>
int main()
{
int c;
for(c=0;c<3;c=c+1)
{
puts("I shall refrain from calling my friends names.");
}
getchar();
return(0);
}
http://eglobiotraining.com/
8. In C Language programming, the for keyword is followed by a set of parentheses.
Inside the parentheses are three separate items that configure the
loop.The c variable is already defined as an int (integer). It's used by the for loop to
control how many times the loop — the statements belonging to for — is repeated.
First comes the setup (c=0) :
The variable c is assigned the value 0. The for statement does this first, before the
loop is ever repeated, and then only once.
Note that starting at 0 rather than 1 is a traditional C language thing. Zero is the
"first" number. Get used to that.
Next comes the exit condition (c<3) :
The loop repeats itself as long as the value of variable c is less than 3.
Finally, here's the "do this" part of the loop (c=c+1) :
Each time the loop is repeated, the for statement executes this statement. It must
be a real C language statement, one that you hope somehow manipulates the
variable that's set up in the first step. Here, the value of variable c is increased,
or incremented, by one.
http://eglobiotraining.com/
10. #include <stdio.h>
void box(int width, int height);
int main()
{
int x, y;
printf( "Please input the width of the box: " );
scanf( "%d", &x );
printf( "Please input the height of the box: " );
scanf( "%d", &y );
printf( "Draw...n" );
box(x,y);
getchar();
}
void box(int width, int height)
{
for (int y = 0; y < height; y++)
{
http://eglobiotraining.com/
11. if (y > 0 && y < height - 1)
{
for (int x = 0; x < width; x++)
{
if(x == 0)
printf("0");
else if( x == width - 1)
printf("0n");
else
printf(" ");
}
}
else
{
for (int x = 0; x < width; x++)
{
if( x == width - 1)
printf("0n");
else
printf("0");
}
}
}
printf("n");
}
http://eglobiotraining.com/
12. void box(int width, int height);
* this is a method that does anything
int x, y;
*x is the width, y is the height
box(x,y);
*function that will do the things below
printf(― ");
*this is the condition to leave empty space inside the box
printf("0");
*this will fill "0"s in the whole row
printf("0n");
*if the width reached the end it will go to the next line with symbols of "n"
http://eglobiotraining.com/
14. #include <stdio.h>
void countdown(int time);
int main()
{
int time;
printf( "Please input the time to perform countdown: " );
scanf( "%d", &time );
countdown(time);
getchar();
}
void countdown(int time)
{
if(time < 0)
time = 0;
do{
printf( "%dn", time );
if(time == 0) // if the time reached 0, it will print Happy New Year then end
printf( "Happy New Year!!n");
time--;
}
while(time > -1);
getchar();
}
http://eglobiotraining.com/
15. void countdown(int time);
*this is a method that does anything
int time;
*the max time
countdown(time);
*function that will do the things below
if(time < 0)
*if the time is less than 0, it will automatically be "0“
if(time == 0)
printf( "Happy New Year!!n");
*if the time reached 0, it will print “Happy New Year” then end
http://eglobiotraining.com/
17. #include <stdio.h>
int exponent ( int number, int power );
int main()
{
int number, power;
printf( "Please input a number: " );
scanf( "%d", &number );
printf( "Please input the power of exponentn (greater than zero): " );
scanf( "%d", &power );
printf( "The result is %dn", exponent( number, power ) );
getchar();
}
int exponent (int number, int power)
{
int last = 1;
if (power > 0)
for(int x = 0; x < power; x++)
last *= number;
return last;
}
http://eglobiotraining.com/
18. int exponent ( int number, int power ); /
*this is a function that returns int value
printf( "The result is %dn", exponent( number, power ) );
*exponent(number, power) will automatically perform tasks and return int
value
int last = 1;
*serve as the temporary number
if (power > 0)
*if the power is greater than zero, it will perform for loop until (x < power) is
false
last *= number;
*same as last = last * number
* it means the left "last" will be the output of last * number
http://eglobiotraining.com/
20. #include <stdio.h>
void ladder(int top);
int main()
{
int top;
printf( "Please input the height of the ladder: " );
scanf( "%d", &top );
printf( "Draw...n" );
ladder(top);
getchar();
}
void ladder(int top)
{
for (int y = 1; y < top + 1; y++)
{
for (int x = y; x > 0; x--)
printf("%d",x);
printf("n");
}
}
http://eglobiotraining.com/
21. void ladder(int top);
*this is a method that does anything
ladder(top);
*function that will do the things below
for (int y = 1; y < top + 1; y++)
*the width of the ladder will gradually
increase
http://eglobiotraining.com/
23. Switch case statements . . .
In programming, these are substitutes 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 value of the variable given into
switch in C Language programming 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.
http://eglobiotraining.com/
24. LOOP FLOW CONTROLS :
BREAK AND CONTINUE
C uses two different orders in programming to control
loop’s flow :
•break – escapes from the nearest outer loop
•continue – inside ―while‖ and ―do‖ loop: switches
program execution to test condition, inside ―for‖ loop:
switches program execution to ―for‖ loop step and
then to condition test (also applies for nearest outer
loop)
http://eglobiotraining.com/
26. #include <stdio.h>
int plus (int one, int two);
int minus (int one, int two);
int multi (int one, int two);
int divide (int one, int two);
int exponent (int number, int power);
int factorial ( int number );
int main()
{
int choice, x, y;
printf( "(1) additionn" );
printf( "(2) subtractionn" );
printf( "(3) multiplicationn" );
printf( "(4) divisionn" );
printf( "(5) exponentn" );
printf( "(6) factorialn" );
printf( "Please input the number of the method you want to perform(1 - 6): " );
scanf( "%d", &choice );
switch (choice)
{
http://eglobiotraining.com/
27. case 1: case 2: case 3: case 4:
printf( "Please input the 1st number: ");
scanf( "%d", &x );
printf( "Please input the 2nd number: ");
scanf( "%d", &y );
break;
case 5:
printf( "Please input the number: ");
scanf( "%d", &x );
printf( "Please input the power: ");
scanf( "%d", &y );
break;
case 6:
printf( "Please input a number: ");
scanf( "%d", &x );
break;
}
switch (choice)
{
case 1: printf( "result of addition: %dn" ,plus(x, y));
break;
case 2: printf( "result of subtraction: %dn" ,minus(x, y));
break;
case 3: printf( "result of multiplication: %dn" ,multi(x, y));
break;
case 4: printf( "result of division: %dn" ,divide(x, y));
break;
case 5: printf( "result of exponent: %dn" ,exponent(x, y));
break;
case 6: printf( "result of factorial: %dn" ,factorial(x));
break;
}
getchar();
}
http://eglobiotraining.com/
28. int factorial ( int number )
{
int last = 1;
while (number > 0)
{
last *= number;
number--;
}
return last;
}
int exponent (int number, int power)
{
int last = 1;
if (power > 0)
for(int x = 0; x < power; x++)
last *= number;
return last;
}
int plus (int one, int two)
{
return one + two;
}
http://eglobiotraining.com/
29. int minus (int one, int two)
{
return one - two;
}
int multi (int one, int two)
{
return one * two;
}
int divide (int one, int two)
{
return one / two;
}
http://eglobiotraining.com/
30. int plus (int one, int two);
int minus (int one, int two);
int multi (int one, int two);
int divide (int one, int two);
int exponent (int number, int power);
int factorial ( int number );
*these are functions that return int values
int choice, x, y;
printf( "(1) additionn" );
printf( "(2) subtractionn" );
printf( "(3) multiplicationn" );
printf( "(4) divisionn" );
printf( "(5) exponentn" );
printf( "(6) factorialn" );
printf( "Please input the number of the method you want to perform(1 - 6): " );
scanf( "%d", &choice );
* a "switch" if quite similar to "if statement"
*after you input the choice, the switch will find a similar case and perform task
*switch will end after it reach "break;"
http://eglobiotraining.com/
32. #include <stdio.h>
int charToint(char letter);
int main()
{
char letter;
printf( "Please input a character (a - z): " );
scanf( "%c", &letter );
switch (charToint(letter)%10)
{
case 1:printf("It is the %dst character.n",charToint(letter));
break;
case 2:printf("It is the %dnd character.n",charToint(letter));
break;
case 3:printf("It is the %drd character.n",charToint(letter));
break;
default:printf("It is the %dth character.n",charToint(letter));
break;
}
getchar();
}
int charToint(char letter)
{
http://eglobiotraining.com/
33. switch (letter)
{
case 'a': case 'A': return 1;
case 'b': case 'B': return 2;
case 'c': case 'C': return 3;
case 'd': case 'D': return 4;
case 'e': case 'E': return 5;
case 'f': case 'F': return 6;
case 'g': case 'G': return 7;
case 'h': case 'H': return 8;
case 'i': case 'I': return 9;
case 'j': case 'J': return 10;
case 'k': case 'K': return 11;
case 'l': case 'L': return 12;
case 'm': case 'M': return 13;
case 'n': case 'N': return 14;
case 'o': case 'O': return 15;
case 'p': case 'P': return 16;
case 'q': case 'Q': return 17;
case 'r': case 'R': return 18;
case 's': case 'S': return 19;
case 't': case 'T': return 20;
case 'u': case 'U': return 21;
case 'v': case 'V': return 22;
case 'w': case 'W': return 23;
case 'x': case 'X': return 24;
case 'y': case 'Y': return 25;
case 'z': case 'Z': return 26;
getchar();
}
}
http://eglobiotraining.com/
34. int charToint(char letter);
*this is a function that returns int value
char letter;
printf( "Please input a character (a - z): " );
scanf( "%c", &letter );
* a "switch" if quite similar to "if statement"
* after you input the letter, the switch will find a similar case and
perform task
*switch will end after it reach "break;“
switch (charToint(letter)%10)
*"charToint(letter)%10" returns a remainder of charToint(letter)/10
switch (letter)
*alphabet ranking of letters a to z
http://eglobiotraining.com/
36. #include <stdio.h>
int plus (int one, int two); // these are functions that return int values
int minus (int one, int two);
int multi (int one, int two);
int divide (int one, int two);
int main()
{
int choice, x, y;
printf( "Please input the 1st number: ");
scanf( "%d", &x );
printf( "Please input the 2nd number: ");
scanf( "%d", &y );
printf( "(1) additionn" );
printf( "(2) subtractionn" );
printf( "(3) multiplicationn" );
printf( "(4) divisionn" );
printf( "Please input the number of the method you want to perform(1 - 4): " );
scanf( "%d", &choice );
// a "switch" if quite similar to "if statement"
// after you input the choice, the switch will find a similar case and perform task
// switch will end after it reach "break;"
switch (choice)
{
http://eglobiotraining.com/
37. case 1: printf( "result of addition: %dn" ,plus(x, y));
break;
case 2: printf( "result of subtraction: %dn" ,minus(x, y));
break;
case 3: printf( "result of multiplication: %dn" ,multi(x, y));
break;
case 4: printf( "result of division: %dn" ,divide(x, y));
break;
}
getchar();
}
int plus (int one, int two)
{
return one + two;
}
int minus (int one, int two)
{
return one - two;
}
int multi (int one, int two)
{
return one * two;
}
int divide (int one, int two)
{
return one / two;
}
http://eglobiotraining.com/
38. int plus (int one, int two);
int minus (int one, int two);
int multi (int one, int two);
int divide (int one, int two);
* these are functions that return int values
*a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar case and
perform task
*switch will end after it reach "break;“
•a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar case and
perform task
*switch will end after it reach "break;"
http://eglobiotraining.com/
40. #include <stdio.h>
int main()
{
int choice;
printf( "(1) facebookn" );
printf( "(2) Yahoon" );
printf( "(3) twittern" );
printf( "(4) Taggedn" );
printf( "(5) LinkedInn" );
printf( "Please input the number of the Network you like (1 - 5): " );
scanf( "%d", &choice );
switch (choice)
{
case 1: printf( "I LOVE facebookn" );
break;
case 2: printf( "I like Yahoon" );
break;
case 3: printf( "I like twittern" );
break;
case 4: printf( "I like Taggedn" );
break;
case 5: printf( "I like LinkedInn" );
break;
}
getchar();
}
http://eglobiotraining.com/
41. *simply get the a number of choice then print
*a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar
case and perform task
*switch will end after it reach "break;"
http://eglobiotraining.com/
43. #include <stdio.h>
void printabc(int number);
int main()
{
int number;
printf( "Please input number of characters: " );
scanf( "%d", &number );
printabc(number);
getchar();
}
void printabc(int number)
{
while (number > 0)
{
switch(number%3)
{
case 0:
printf("a");
break;
case 1:
printf("b");
break;
case 2:
printf("c");
break;
}
number--;
}
printf("n");
}
http://eglobiotraining.com/
44. int number;
printf( "Please input number of
characters: " );
scanf( "%d", &number );
printabc(number);
*input: length of the word
*output: word with a, b and c
http://eglobiotraining.com/