SlideShare uma empresa Scribd logo
1 de 39
Baixar para ler offline
http://eglobiotraining.com.
Fundamentals of Programming




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




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




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




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




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;                                              Output
              case 4:
              printf("seniors");
              break;
              default:
                   printf("invalid");
                   break;

                  }
                  getch();
                  return 0;
                  }
                                    http://eglobiotraining.com.
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);

                                                                                                    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;
                }                                        http://eglobiotraining.com.
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)                                                        Output
     {
     case 1:printf("positive");break;
     case 2:printf("negative");break;
     default:printf("invalid");break;
     }
     getch();
     return 0;
     }



                                        http://eglobiotraining.com.
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                                                                  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;
        }                                       http://eglobiotraining.com.
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;                                     Outpu
     case 2:printf("odd");break;
     default:printf("invalid");break;                                 t
     }


     getch();
       return 0;
     }




                                        http://eglobiotraining.com.
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);
    }
                                                          Output
    getch();
    return 0;
    }




                            http://eglobiotraining.com.
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;                                              Output
    }




                             http://eglobiotraining.com.
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;
}
                                                                Output




                                  http://eglobiotraining.com.
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;
           }




                                                                 Output




                                   http://eglobiotraining.com.
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;
        }




                                                                Output


http://www.slideshare.net/upload?from_source=loggedin_newsfeed




                                  http://eglobiotraining.com.
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.

Mais conteúdo relacionado

Mais procurados

Problem solving using Computer
Problem solving using ComputerProblem solving using Computer
Problem solving using ComputerDavid Livingston J
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithmshccit
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartSachin Goyani
 
Flowcharts and algorithms
Flowcharts and algorithmsFlowcharts and algorithms
Flowcharts and algorithmsStudent
 
Algorithm and Flowcharts
Algorithm and FlowchartsAlgorithm and Flowcharts
Algorithm and FlowchartsSabik T S
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - IntroductionMadhu Bala
 
flowchart & algorithms
flowchart & algorithmsflowchart & algorithms
flowchart & algorithmsStudent
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problemFrankie Jones
 
Jeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testingJeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testingJeremiah Yancy
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowchartsSamuel Igbanogu
 
Introduction to Algorithms & flow charts
Introduction to Algorithms & flow chartsIntroduction to Algorithms & flow charts
Introduction to Algorithms & flow chartsYash Gupta
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer Ashim Lamichhane
 
Programming fundamentals lecture 1 0f c
Programming fundamentals lecture 1 0f cProgramming fundamentals lecture 1 0f c
Programming fundamentals lecture 1 0f cRaja Hamid
 
Flowchart and algorithem
Flowchart and algorithemFlowchart and algorithem
Flowchart and algorithemehsanullah786
 
Csc 130 class 2 problem analysis and flow charts(2)
Csc 130 class 2   problem analysis and flow charts(2)Csc 130 class 2   problem analysis and flow charts(2)
Csc 130 class 2 problem analysis and flow charts(2)Puneet narula
 

Mais procurados (20)

Problem solving using Computer
Problem solving using ComputerProblem solving using Computer
Problem solving using Computer
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
 
phases of algorithm
phases of algorithmphases of algorithm
phases of algorithm
 
Algorithm & flow chart
Algorithm & flow chartAlgorithm & flow chart
Algorithm & flow chart
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Flowcharts and algorithms
Flowcharts and algorithmsFlowcharts and algorithms
Flowcharts and algorithms
 
Algorithm and Flowcharts
Algorithm and FlowchartsAlgorithm and Flowcharts
Algorithm and Flowcharts
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
flowchart & algorithms
flowchart & algorithmsflowchart & algorithms
flowchart & algorithms
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem
 
Jeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testingJeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testing
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
 
Introduction to Algorithms & flow charts
Introduction to Algorithms & flow chartsIntroduction to Algorithms & flow charts
Introduction to Algorithms & flow charts
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 
Programming fundamentals lecture 1 0f c
Programming fundamentals lecture 1 0f cProgramming fundamentals lecture 1 0f c
Programming fundamentals lecture 1 0f c
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Flowchart and algorithem
Flowchart and algorithemFlowchart and algorithem
Flowchart and algorithem
 
Csc 130 class 2 problem analysis and flow charts(2)
Csc 130 class 2   problem analysis and flow charts(2)Csc 130 class 2   problem analysis and flow charts(2)
Csc 130 class 2 problem analysis and flow charts(2)
 

Destaque

Fundamental Programming Lect 4
Fundamental Programming Lect 4Fundamental Programming Lect 4
Fundamental Programming Lect 4Namrah Erum
 
Fundamental Programming Lect 5
Fundamental Programming Lect 5Fundamental Programming Lect 5
Fundamental Programming Lect 5Namrah Erum
 
Fundamental Programming Lect 3
Fundamental Programming Lect 3Fundamental Programming Lect 3
Fundamental Programming Lect 3Namrah Erum
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2Namrah Erum
 
Fundamental Programming Lect 1
Fundamental Programming Lect 1Fundamental Programming Lect 1
Fundamental Programming Lect 1Namrah Erum
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 

Destaque (7)

Fundamental Programming Lect 4
Fundamental Programming Lect 4Fundamental Programming Lect 4
Fundamental Programming Lect 4
 
Fundamental Programming Lect 5
Fundamental Programming Lect 5Fundamental Programming Lect 5
Fundamental Programming Lect 5
 
Fundamental Programming Lect 3
Fundamental Programming Lect 3Fundamental Programming Lect 3
Fundamental Programming Lect 3
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2
 
Fundamental Programming Lect 1
Fundamental Programming Lect 1Fundamental Programming Lect 1
Fundamental Programming Lect 1
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 

Semelhante a Fundamentals of programming)

Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinarurumedina
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C LanguageMohamed Elsayed
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement_jenica
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 

Semelhante a Fundamentals of programming) (20)

Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Lec 10
Lec 10Lec 10
Lec 10
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Session 3
Session 3Session 3
Session 3
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 

Fundamentals of programming)

  • 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.
  • 11. Fundamentals of Programming Here, if the Grade is 'A' then the output will be: 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.
  • 22. 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); 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; } 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.
  • 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; } 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.
  • 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 } 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.