Upload
jakejakejake2
View
213
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Sir di po ma upload yung mga program na ginawa ko. Kaya yung pdf nlng po ang pinasa ko
Citation preview
http://eglobiotraining.com.
Fundamentals of Programming
Switch case statements are a substitute
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 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.
Fundamentals of Programming
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.
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.
http://eglobiotraining.com.
Fundamentals of Programming
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.
Fundamentals of Programming
The switch-case statement is a multi-way decision statement.
Unlike the multiple decision statement that can be created
using if-else, the switch statement evaluates the
conditional expression and tests it against numerous
constant values. The branch corresponding to the value that
the expression matches is taken during execution.
The value of the expressions in a switch-case statement must
be an ordinal type i.e. integer, char, short, long etc. Float and
double are not allowed.
The syntax is :
http://eglobiotraining.com.
Fundamentals of Programming
The case statements and
the default statement can occur in any
order in the switch statement.
The default clause is an optional clause
that is matched if none of the constants
in the case statements can be matched.
Consider the example shown below:
http://eglobiotraining.com.
Fundamentals of Programming
Here, if the Grade is 'A' then the output will
be:
http://eglobiotraining.com.
Fundamentals of Programming
This is because, in the 'C' switch statement,
execution continues on into the next case clause if it is not explicitly specified that the execution should exit the switch statement.
The correct statement would be:
http://eglobiotraining.com.
Fundamentals of Programming
Although the break in
the default clause (or in general,
after the last clause) is not
necessary, it is good
programming practice to put it in
anyway.
An example where it is better to
allow the execution to continue
into the next case statement:
http://eglobiotraining.com.
Fundamentals of Programming
http://eglobiotraining.com.
Fundamentals of Programming
Looping Statements
while ( expression ) statement In a while loop, the expression is evaluated. If nonzero, the statement executes, and the expression is evaluated again. This happens over and over until the expression's value is zero. If the expression is zero the first time it is evaluated, statement is not executed at all.do statement while ( expression); A do while loop is just like a plain while loop, except the statement executes before the expression is evaluated. Thus, the statement will always be evaluated at least once.
for ( expression1; expression2; expression3 ) statement In a for loop, first expression1 is evaluated. Then expression2 is evaluated, and if it is zero EEL leaves the loop and begins executing instructions after statement. Otherwise the statement 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 is nonzero. while (expression) is the same as for (; expression; ). The syntax for (;;) creates an endless loop that must be exited using the break statement (or one of the other statements described below).
http://eglobiotraining.com.
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
http://eglobiotraining.com.
Fundamentals of Programming
The break keyword is used to terminate a
loop, intermediately bypassing any
conditions. The control will be transferred
to the first statement following the loop
block. The break statement can be used
to terminate an infinite loop or to force a
loop to end before its normal
termination.
http://eglobiotraining.com.
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;
case 4:
printf("seniors");
break;
default:
printf("invalid");
break;
}
getch();
return 0;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
If the user will input a number until 1 – 4
the program will answer, but if the user
input higher number it will be invalid.
Example:
If the user will input “1” the variable n will
have its value 1 and the variable will be
used in the switch statement and it will
provide its equivalent value in case. The
answer will be “freshmen”.
http://eglobiotraining.com.
Fundamentals of Programming
fil#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);
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;
} http://eglobiotraining.com.
Output
Fundamentals of Programming
The user will choose one operation if the
user choose “d” then it will have the
value for case d that is division. Then the
program will ask you to provide the first
number and second number, then if you
have already given the two values it will
quickly give you the answer.
http://eglobiotraining.com.
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)
{
case 1:printf("positive");break;
case 2:printf("negative");break;
default:printf("invalid");break;
}
getch();
return 0;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
Input a number, then the value of n will
be the input number. There is a condition
statement that if n is greater than or
equal to 0 x will be equal to one, the
value of x will switch and become the
value of case.
http://eglobiotraining.com.
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
{
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;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
The user must input two numbers. The first
number will serve as the n1 and the
second number is the n2. If you already
put the two numbers the values will
switch and the program will determine
the higher and the lower value.
http://eglobiotraining.com.
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;
case 2:printf("odd");break; default:printf("invalid");break; }
getch(); return 0; }
http://eglobiotraining.com.
Outpu
t
Fundamentals of Programming
The user must input number and that
number that has been encoded will be
divided into two, then the answer that
will come out will be the value of x. And
lastly the program will determine if it is an
odd, even or invalid.
http://eglobiotraining.com.
Fundamentals of Programming
#include<stdio.h>
#include<conio.h>
int main(void)
{
int ctr;
for(ctr=1;ctr<=5;ctr++)
{
printf("%d",ctr);
}
getch();
return 0;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
The variable ctr is equal to one, if ctr is
less than or equal to 5, if it is true it will
print value of ctr that is 1 then to the next
ctr value the first value will be added so
it will become 2 and so on and so forth.
http://eglobiotraining.com.
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;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
The value of ctr is equal to 10 if the ctr is
greater that or equal to 1 and if it is true
the value 10 will be deducted by 1 and
so on and so forth.
http://eglobiotraining.com.
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;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
The value of ctr is 2 if the ctr is less than or
equal to 20 then the value of ctr that is 2
will become 4 because the program will
add another 2 and again and again
and again.
http://eglobiotraining.com.
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;
}
http://eglobiotraining.com.
Output
Fundamentals of Programming
The value of x is 1 and y is equal to 5. If
the x is less than or equal 5 then the
value of 1 will be added by 1 and the
value of y will be subtracted by 1.
http://eglobiotraining.com.
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;
}
http://www.slideshare.net/upload?from_source=loggedin_newsfeed
http://eglobiotraining.com.
Output
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
http://eglobiotraining.com/
http://eglobiotraining.com.
Fundamentals of Programming