SlideShare ist ein Scribd-Unternehmen logo
1 von 46
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
                  Language



The core of most modern programming, including those in
the C language, is the loop. A loop gives a program the
ability to repeat a group of statements, sometimes for a
given count or duration, or, often, until a certain condition
is met. The C language gives you many ways to create
loops 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 Loop



For Loop:
In for looping statement allows a number of
lines represent until the condition is
satisfied                                WHILE LOOP:
                                         In While looping statement allows a
                                         number of lines represent until the
                                         condition is satisfied
DO WHILE LOOP:
In DO WHILE LOOP first execute the
statements then it checks the condition.


                              http://eglobiotraining.com/
CONCLUSION:



In FOR LOOP:
No need to initialize variable before the LOOP


In WHILE LOOP:
To initialize the variable before the LOOP
Increment/decrement the variable within the LOOP


In 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 the
loop.The c variable is already defined as an int (integer). It's used by the for loop to
control 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 the
loop 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, here's the "do this" part of the loop (c=c+1) :
Each time the loop is repeated, the for statement executes this statement. It must
be a real C language statement, one that you hope somehow manipulates the
variable that's 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 anything

int x, y;
*x is the width, y is the height

box(x,y);
*function that will do the things below

printf(― ");
*this is the condition to leave empty space inside the box


printf("0");
*this will fill "0"s in the whole row

printf("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 anything

int time;
*the max time

countdown(time);
*function that will do the things below

if(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 value

printf( "The result is %dn", exponent( number, power ) );
*exponent(number, power) will automatically perform tasks and return int
value

int last = 1;
*serve as the temporary number

if (power > 0)
*if the power is greater than zero, it will perform for loop until (x < power) is
false

last *= 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 anything

ladder(top);
*function that will do the things below

for (int y = 1; y < top + 1; y++)
*the width of the ladder will gradually
increase

                http://eglobiotraining.com/
http://eglobiotraining.com/
Switch case statements . . .
In programming, these are substitutes 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 in C Language programming 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/
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 values

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 );
* 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 value


char 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 and
perform task
*switch will end after it reach "break;“


switch (charToint(letter)%10)
*"charToint(letter)%10" returns a remainder of charToint(letter)/10


switch (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 values
int 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 and
perform 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 and
perform 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 similar
case 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 of
characters: " );
     scanf( "%d", &number );
     printabc(number);



*input: length of the word
*output: word with a, b and c
               http://eglobiotraining.com/
http://www.slideshare.net/uploa
d?from_source=loggedin_home
page_navbar




           http://eglobiotraining.com/
This presentation is to be submitted for the final
requirements in Fundamentals of Programming.




               http://eglobiotraining.com/

Weitere ähnliche Inhalte

Was ist angesagt?

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 

Was ist angesagt? (20)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
String
StringString
String
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C programming
C programmingC programming
C programming
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Tu1
Tu1Tu1
Tu1
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
C Programming
C ProgrammingC Programming
C Programming
 
C operators
C operatorsC operators
C operators
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 

Ähnlich wie Programming ppt files (final)

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1Mohamed Ahmed
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
Loop control structure
Loop control structureLoop control structure
Loop control structureKaran Pandey
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about CYi-Hsiu Hsu
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 

Ähnlich wie Programming ppt files (final) (20)

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
2 data and c
2 data and c2 data and c
2 data and c
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Presentation1
Presentation1Presentation1
Presentation1
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Looping
LoopingLooping
Looping
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 

Programming ppt files (final)

  • 1. 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/
  • 3. How to Construct a Basic FOR Loop in the C Language The core of most modern programming, including those in the C language, is the loop. A loop gives a program the ability to repeat a group of statements, sometimes for a given count or duration, or, often, until a certain condition is met. The C language gives you many ways to create loops in your code, but the most common is the for loop. http://eglobiotraining.com/
  • 4. 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 Loop For Loop: In for looping statement allows a number of lines represent until the condition is satisfied WHILE LOOP: In While looping statement allows a number of lines represent until the condition is satisfied DO WHILE LOOP: In DO WHILE LOOP first execute the statements then it checks the condition. http://eglobiotraining.com/
  • 5. CONCLUSION: In FOR LOOP: No need to initialize variable before the LOOP In WHILE LOOP: To initialize the variable before the LOOP Increment/decrement the variable within the LOOP In DO WHILE LOOP: Once it execute If the condition is TRUE/FALSE. http://eglobiotraining.com/
  • 6. Output screenshot : http://eglobiotraining.com/
  • 7. #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/
  • 8. In C Language programming, the for keyword is followed by a set of parentheses. Inside the parentheses are three separate items that configure the loop.The c variable is already defined as an int (integer). It's used by the for loop to control 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 the loop 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, here's the "do this" part of the loop (c=c+1) : Each time the loop is repeated, the for statement executes this statement. It must be a real C language statement, one that you hope somehow manipulates the variable that's set up in the first step. Here, the value of variable c is increased, or incremented, by one. http://eglobiotraining.com/
  • 9. Output screenshot : http://eglobiotraining.com/
  • 10. #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/
  • 11. 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/
  • 12. void box(int width, int height); * this is a method that does anything int x, y; *x is the width, y is the height box(x,y); *function that will do the things below printf(― "); *this is the condition to leave empty space inside the box printf("0"); *this will fill "0"s in the whole row printf("0n"); *if the width reached the end it will go to the next line with symbols of "n" http://eglobiotraining.com/
  • 13. Output screenshot : http://eglobiotraining.com/
  • 14. #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/
  • 15. void countdown(int time); *this is a method that does anything int time; *the max time countdown(time); *function that will do the things below if(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/
  • 16. Output screenshot : http://eglobiotraining.com/
  • 17. #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/
  • 18. int exponent ( int number, int power ); / *this is a function that returns int value printf( "The result is %dn", exponent( number, power ) ); *exponent(number, power) will automatically perform tasks and return int value int last = 1; *serve as the temporary number if (power > 0) *if the power is greater than zero, it will perform for loop until (x < power) is false last *= number; *same as last = last * number * it means the left "last" will be the output of last * number http://eglobiotraining.com/
  • 19. Output screenshot : http://eglobiotraining.com/
  • 20. #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/
  • 21. void ladder(int top); *this is a method that does anything ladder(top); *function that will do the things below for (int y = 1; y < top + 1; y++) *the width of the ladder will gradually increase http://eglobiotraining.com/
  • 23. Switch case statements . . . In programming, these are substitutes 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 in C Language programming 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/
  • 24. 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/
  • 25. Output screenshot : http://eglobiotraining.com/
  • 26. #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/
  • 27. 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/
  • 28. 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/
  • 29. 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/
  • 30. 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 values 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 ); * 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/
  • 31. Output screenshot : http://eglobiotraining.com/
  • 32. #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/
  • 33. 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/
  • 34. int charToint(char letter); *this is a function that returns int value char 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 and perform task *switch will end after it reach "break;“ switch (charToint(letter)%10) *"charToint(letter)%10" returns a remainder of charToint(letter)/10 switch (letter) *alphabet ranking of letters a to z http://eglobiotraining.com/
  • 35. Output screenshot : http://eglobiotraining.com/
  • 36. #include <stdio.h> int plus (int one, int two); // these are functions that return int values int 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/
  • 37. 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/
  • 38. 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 and perform 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 and perform task *switch will end after it reach "break;" http://eglobiotraining.com/
  • 39. Output screenshot : http://eglobiotraining.com/
  • 40. #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/
  • 41. *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 similar case and perform task *switch will end after it reach "break;" http://eglobiotraining.com/
  • 42. Output screenshot : http://eglobiotraining.com/
  • 43. #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/
  • 44. int number; printf( "Please input number of characters: " ); scanf( "%d", &number ); printabc(number); *input: length of the word *output: word with a, b and c http://eglobiotraining.com/
  • 46. This presentation is to be submitted for the final requirements in Fundamentals of Programming. http://eglobiotraining.com/