Your SlideShare is downloading. ×
Fundamentals of programming)
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Fundamentals of programming)


Published on

Sir di po ma upload yung mga program na ginawa ko. Kaya yung pdf nlng po ang pinasa ko

Sir di po ma upload yung mga program na ginawa ko. Kaya yung pdf nlng po ang pinasa ko

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1.
  • 2. Fundamentals of Programming
  • 3. Fundamentals of ProgrammingSwitch case statements are a substitutefor long if statements that compare avariable to several "integral" values("integral" values are simply values thatcan be expressed as an integer, such asthe value of a char). The value of thevariable given into switch is compared tothe value following each of the cases,and when one value matches the valueof the variable, the computer continuesexecuting the program from that point.
  • 4. Fundamentals of ProgrammingThe condition of a switch statement is avalue. The case says that if it has the valueof whatever is after that case then dowhatever follows the colon. The break isused to break out of the case statements.Break is a keyword that breaks out of thecode block, usually surrounded by braces,which it is in. In this case, break prevents theprogram from falling through and executingthe code in all the other case statements.An important thing to note about the switchstatement is that the case values may onlybe constant integral expressions. Sadly, itisnt legal to use case like this.
  • 5. Fundamentals of Programming
  • 6. Fundamentals of Programming 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.
  • 7. Fundamentals of Programming
  • 8. Fundamentals of ProgrammingThis program will compile, but cannot berun until the undefined functions are givenbodies, but it serves as a model (albeitsimple) for processing input. If you do notunderstand this then try mentally putting inif statements for the case statements.Default simply skips out of the switch caseconstruction and allows the program toterminate naturally. If you do not like that,then you can make a loop around thewhole thing to have it wait for valid input.You could easily make a few small functionsif you wish to test the code.
  • 9. Fundamentals of ProgrammingThe switch-case statement is a multi-way decision statement.Unlike the multiple decision statement that can be createdusing if-else, the switch statement evaluates theconditional expression and tests it against numerousconstant values. The branch corresponding to the value thatthe expression matches is taken during execution.The value of the expressions in a switch-case statement mustbe an ordinal type i.e. integer, char, short, long etc. Float anddouble are not allowed.The syntax is :
  • 10. Fundamentals of ProgrammingThe case statements andthe default statement can occur in anyorder in the switch statement.The default clause is an optional clausethat is matched if none of the constantsin the case statements can be matched.Consider the example shown below:
  • 11. Fundamentals of ProgrammingHere, if the Grade is A then the output will be:
  • 12. Fundamentals of ProgrammingThis is because, in the C switch statement,execution continues on into the next caseclause if it is not explicitly specified that theexecution should exit the switch statement.The correct statement would be:
  • 13. Fundamentals of ProgrammingAlthough the break inthe default clause (or in general,after the last clause) is notnecessary, it is goodprogramming practice to put it inanyway.An example where it is better toallow the execution to continueinto the next case statement:
  • 14. Fundamentals of Programming
  • 15. Fundamentals of Programming
  • 16. Fundamentals of ProgrammingLooping Statementswhile ( expression ) statementIn a while loop, the expression is evaluated. If nonzero, the statement executes, and theexpression is evaluated again. This happens over and over until the expressions value iszero. If the expression is zero the first time it is evaluated, statement is not executed statementwhile ( expression);A do while loop is just like a plain while loop, except the statement executes before theexpression is evaluated. Thus, the statement will always be evaluated at least once.for ( expression1; expression2; expression3 ) statementIn a for loop, first expression1 is evaluated. Then expression2 is evaluated, and if it is zeroEEL leaves the loop and begins executing instructions after statement. Otherwise thestatement is executed, expression3 is evaluated, and expression2 is evaluated again,continuing until expression2 is zero.You can omit any of the expressions. If you omit expression2, it is like expression2 isnonzero. while (expression) is the same as for (; expression; ). The syntax for (;;) createsan endless loop that must be exited using the break statement (or one of the otherstatements described below).
  • 17. Fundamentals of Programming The for loop construct is used is used to repeat a statement or block of statements a specific number of times. The while loop construct only contains condition. The do while, the difference between do while loop and other loops is that in the do while loop the condition comes after the statement
  • 18. Fundamentals of ProgrammingThe break keyword is used to terminate aloop, intermediately bypassing anyconditions. The control will be transferredto the first statement following the loopblock. The break statement can be usedto terminate an infinite loop or to force aloop to end before its normaltermination.
  • 19. Fundamentals of Programming
  • 20. Fundamentals of Programming#include<stdio.h>#include<conio.h>int main(void){ int n; printf("input year level: "); scanf("%d",&n); switch(n) { case 1: printf("freshmen"); break; case 2: printf("sophomores"); break; case 3: printf("juniors"); break; Output case 4: printf("seniors"); break; default: printf("invalid"); break; } getch(); return 0; }
  • 21. Fundamentals of ProgrammingIf the user will input a number until 1 – 4the program will answer, but if the userinput higher number it will be invalid.Example:If the user will input “1” the variable n willhave its value 1 and the variable will beused in the switch statement and it willprovide its equivalent value in case. Theanswer will be “freshmen”.
  • 22. Fundamentals of Programmingfil#include<stdio.h>#include<conio.h>int main(void){ int n1,n2,r; char o; printf("select an operation na)addition nb)subtraction nc)multiplication nd)division "); scanf("%c",&o); switch(o) { case a: printf("input 1st number:"); scanf("%d",&n1); printf("input 2nd number:"); scanf("%d",&n2); r=n1+n2; printf("%d",r); break; case b: printf("input 1st number:"); scanf("%d",&n1); printf("input 2nd number:"); scanf("%d",&n2); r=n1-n2; printf("%d",r); break; case c: printf("input 1st number:"); scanf("%d",&n1); Output printf("input 2nd number:"); scanf("%d",&n2); r=n1*n2; printf("%d",r); break; case d: printf("input 1st number:"); scanf("%d",&n1); printf("input 2nd number:"); scanf("%d",&n2); r=n1/n2; printf("%d",r); break; default: printf("invalid"); break; } getch(); return 0; }
  • 23. Fundamentals of ProgrammingThe user will choose one operation if theuser choose “d” then it will have thevalue for case d that is division. Then theprogram will ask you to provide the firstnumber and second number, then if youhave already given the two values it willquickly give you the answer.
  • 24. Fundamentals of Programming#include<stdio.h>#include<conio.h>int main(void){ int n,x; printf("input a number: "); scanf("%d",&n); if(n>=0) { x=1; } else { x=2; } switch(x) Output { case 1:printf("positive");break; case 2:printf("negative");break; default:printf("invalid");break; } getch(); return 0; }
  • 25. Fundamentals of ProgrammingInput a number, then the value of n willbe the input number. There is a conditionstatement that if n is greater than orequal to 0 x will be equal to one, thevalue of x will switch and become thevalue of case.
  • 26. Fundamentals of Programming#include<stdio.h>#include<conio.h>int main(void){ int n1,n2,x; printf("input 1st number: "); scanf("%d",&n1); printf("input 2nd number: "); scanf("%d",&n2); if(n1>n2) { x=1; } else if(n2>n1) { x=2; } else Output { x=3; } switch(x) { case 1:printf("Higher: %d Lower: %d",n1,n2);;break; case 2:printf("Higher: %d Lower: %d",n2,n1);;break; default:printf("equal");break; } getch(); return 0; }
  • 27. Fundamentals of ProgrammingThe user must input two numbers. The firstnumber will serve as the n1 and thesecond number is the n2. If you alreadyput the two numbers the values willswitch and the program will determinethe higher and the lower value.
  • 28. Fundamentals of Programming#include<stdio.h>#include<conio.h>int main(void){ int n,x,y; printf("input a number: "); scanf("%d",&n); x=n%2; if(x==0) { y=1; } else { y=2; } switch(y) { case 1:printf("even");break; Outpu case 2:printf("odd");break; default:printf("invalid");break; t } getch(); return 0; }
  • 29. Fundamentals of ProgrammingThe user must input number and thatnumber that has been encoded will bedivided into two, then the answer thatwill come out will be the value of x. Andlastly the program will determine if it is anodd, even or invalid.
  • 30. Fundamentals of Programming #include<stdio.h>#include<conio.h>int main(void){ int ctr; for(ctr=1;ctr<=5;ctr++) { printf("%d",ctr); } Output getch(); return 0; }
  • 31. Fundamentals of ProgrammingThe variable ctr is equal to one, if ctr isless than or equal to 5, if it is true it willprint value of ctr that is 1 then to the nextctr value the first value will be added soit will become 2 and so on and so forth.
  • 32. Fundamentals of Programming#include<stdio.h>#include<conio.h>int main(void){ int ctr; for(ctr=10;ctr>=1;ctr--) { printf(" %d",ctr); } getch(); return 0; Output }
  • 33. Fundamentals of ProgrammingThe value of ctr is equal to 10 if the ctr isgreater that or equal to 1 and if it is truethe value 10 will be deducted by 1 andso on and so forth.
  • 34. Fundamentals of Programming #include<stdio.h>#include<conio.h>int main(void){ int ctr=2; while(ctr<=20) { printf(" %d",ctr); ctr=ctr+2; } getch(); return 0;} Output
  • 35. Fundamentals of ProgrammingThe value of ctr is 2 if the ctr is less than orequal to 20 then the value of ctr that is 2will become 4 because the program willadd another 2 and again and againand again.
  • 36. Fundamentals of Programming #include<stdio.h>#include<conio.h>int main(void){ int x=1,y=5; while(x<=5) { printf(" %d %d",x,y); x++; y--; } getch(); return 0; } Output
  • 37. Fundamentals of ProgrammingThe value of x is 1 and y is equal to 5. Ifthe x is less than or equal 5 then thevalue of 1 will be added by 1 and thevalue of y will be subtracted by 1.
  • 38. Fundamentals of Programming #include<stdio.h>#include<conio.h>int main(void){ int ctr; ctr=1; do { printf(" jake %d",ctr); ctr++; }while(ctr<=10); getch(); return 0; } Output
  • 39. Fundamentals of Programming The value of ctr is 1 if the value of ctr is less than or equal to 10 then the value will be added by 1 and so on and so forth.This file is to be submitted by:Prof. Erwin M. Globio