3. 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.
4. 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.
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.
http://eglobiotraining.com.
8. 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.
9. 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.
10. 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.
12. 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.
13. 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.
16. 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.
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
http://eglobiotraining.com.
18. 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.
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;
}
http://eglobiotraining.com.
21. 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.
23. 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.
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;
}
http://eglobiotraining.com.
25. 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.
27. 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.
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;
}
http://eglobiotraining.com.
29. 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.
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;
}
http://eglobiotraining.com.
31. 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.
33. 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.
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
http://eglobiotraining.com.
35. 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.
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
http://eglobiotraining.com.
37. 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.
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
http://www.slideshare.net/upload?from_source=loggedin_newsfeed
http://eglobiotraining.com.
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
http://eglobiotraining.com/
http://eglobiotraining.com.