• Like
  • Save
Programming ppt files (final)
Upcoming SlideShare
Loading in...5
×
 

Programming ppt files (final)

on

  • 217 views

final requirement FNDPRG

final requirement FNDPRG

Statistics

Views

Total Views
217
Views on SlideShare
217
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Programming ppt files (final) Programming ppt files (final) Presentation Transcript

    • 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/
    • http://eglobiotraining.com/
    • How to Construct a Basic FOR Loop in the C LanguageThe core of most modern programming, including those inthe C language, is the loop. A loop gives a program theability to repeat a group of statements, sometimes for agiven count or duration, or, often, until a certain conditionis met. The C language gives you many ways to createloops in your code, but the most common is the for loop. http://eglobiotraining.com/
    • 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 LoopFor Loop:In for looping statement allows a number oflines represent until the condition issatisfied WHILE LOOP: In While looping statement allows a number of lines represent until the condition is satisfiedDO WHILE LOOP:In DO WHILE LOOP first execute thestatements then it checks the condition. http://eglobiotraining.com/
    • CONCLUSION:In FOR LOOP:No need to initialize variable before the LOOPIn WHILE LOOP:To initialize the variable before the LOOPIncrement/decrement the variable within the LOOPIn DO WHILE LOOP:Once it execute If the condition is TRUE/FALSE. http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • In C Language programming, the for keyword is followed by a set of parentheses.Inside the parentheses are three separate items that configure theloop.The c variable is already defined as an int (integer). Its used by the for loop tocontrol 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 theloop 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, heres the "do this" part of the loop (c=c+1) :Each time the loop is repeated, the for statement executes this statement. It mustbe a real C language statement, one that you hope somehow manipulates thevariable thats set up in the first step. Here, the value of variable c is increased,or incremented, by one. http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • 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/
    • void box(int width, int height);* this is a method that does anythingint x, y;*x is the width, y is the heightbox(x,y);*function that will do the things belowprintf(― ");*this is the condition to leave empty space inside the boxprintf("0");*this will fill "0"s in the whole rowprintf("0n");*if the width reached the end it will go to the next line with symbols of "n" http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • void countdown(int time);*this is a method that does anythingint time;*the max timecountdown(time);*function that will do the things belowif(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/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • int exponent ( int number, int power ); /*this is a function that returns int valueprintf( "The result is %dn", exponent( number, power ) );*exponent(number, power) will automatically perform tasks and return intvalueint last = 1;*serve as the temporary numberif (power > 0)*if the power is greater than zero, it will perform for loop until (x < power) isfalselast *= number;*same as last = last * number* it means the left "last" will be the output of last * number http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • void ladder(int top);*this is a method that does anythingladder(top);*function that will do the things belowfor (int y = 1; y < top + 1; y++)*the width of the ladder will graduallyincrease http://eglobiotraining.com/
    • http://eglobiotraining.com/
    • Switch case statements . . .In programming, these are substitutes for long ifstatements that compare a variable to several"integral" values ("integral" values are simply valuesthat can be expressed as an integer, such as thevalue of a char). The value of the variable given intoswitch in C Language programming is compared tothe value following each of the cases, and when onevalue matches the value of the variable, the computercontinues executing the program from that point. http://eglobiotraining.com/
    • 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/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • 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/
    • 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/
    • 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/
    • 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 valuesint 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/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • 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/
    • int charToint(char letter);*this is a function that returns int valuechar 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 andperform task*switch will end after it reach "break;“switch (charToint(letter)%10)*"charToint(letter)%10" returns a remainder of charToint(letter)/10switch (letter)*alphabet ranking of letters a to z http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #include <stdio.h>int plus (int one, int two); // these are functions that return int valuesint 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/
    • 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/
    • 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 andperform 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 andperform task*switch will end after it reach "break;" http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • *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 similarcase and perform task*switch will end after it reach "break;" http://eglobiotraining.com/
    • Output screenshot : http://eglobiotraining.com/
    • #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/
    • int number; printf( "Please input number ofcharacters: " ); scanf( "%d", &number ); printabc(number);*input: length of the word*output: word with a, b and c http://eglobiotraining.com/
    • http://www.slideshare.net/upload?from_source=loggedin_homepage_navbar http://eglobiotraining.com/
    • This presentation is to be submitted for the finalrequirements in Fundamentals of Programming. http://eglobiotraining.com/