C Programming Tutorial – 31 – switch

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

37 responses to “C Programming Tutorial – 31 – switch”

  1. Alex Shepler Avatar

    I can C that you don't have any kids

  2. Danny_Programs ! Avatar

    I have a question, what is the difference between (“”) and (‘’) it seems to make a big difference in C.

  3. The Maudorator Avatar

    not a father but the dad jokes are on point

  4. shiny sonal Avatar

    what if we type continue instead of break ?

  5. Christian Khoueiri Avatar

    You'd yell "Sweaty!" at people who got an "A" because they're a bunch of try-hards.

  6. Nidhish Sharma Avatar

    what did you say in the beginning, like the first two seconds

  7. Caio Costa Avatar

    pretty cool function! 😀

  8. Krishna Arora Avatar

    for those thinking why switch instead of if else if:
    it is far more cleaner to use this instead of if else if there are many cases.

  9. Abhinav Shukla Avatar

    My Program: (Made it a bit more complex)

    Syntax:

    #include <stdio.h>
    #include <stdlib.h>

    int main ()
    {
    int scoreTest;
    int grade = 0;
    int average;
    int testTaken = 0;

    printf("Welcome to the FRCS Averager 101! This Program Decides if the student should get honor roll or not by inputting their grades! n");
    printf("Please press -0 when you're done. n");

    do{

    printf("Number of Tests:%d t Average:%d n" ,testTaken,average);

    printf("Please enter their grade on there test: n");
    scanf(" %d" ,&scoreTest);

    if(scoreTest == -0) {
    break;
    }
    grade+=scoreTest;
    testTaken++;
    average = grade/testTaken;

    }while(scoreTest != -0);

    switch(average) {
    case 90 … 100 : printf("Congratulations! The Student has an A and should be given Distinguished Honors!");
    break;
    case 80 … 89 : printf("Great Job! The Student has an B and should be given High Honors!");
    break;
    case 70 … 79 : printf("Good Job! The Student has an C and should be given Honor Roll!");
    break;
    case 69 … 60 : printf("Almost! The Student has an D and is less then 10 points off of getting Honors!");
    break;
    case 59 … 50 : printf("The Student needs to improve academically!");
    break;
    default : printf("The Student is an idiot!");
    break;
    }
    return 0;
    }

    Test it 🙂

  10. Rome Legeai LLC Avatar

    I felt like you kind fell off topic. I still kinda don't know what "switch" is.

  11. Tiarnán Avatar

    I have a final in 20 minutes and I'm watching these 🙁

  12. Kepler Avatar

    Why do you have to type the char in a ' ' and not in a " "?

  13. Paramvir Singh Avatar

    how is this different form if statements

  14. Lasandra Fernandes Avatar

    What if the break statement wasn't used?would it work in the same way?

  15. Smart James Avatar

    I cannot understand why there is no E in american grading system

  16. yerrapotu kiran Avatar

    Guys execute this ,Iam sure I got this right!
    #include <stdio.h>
    #include <stdlb.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    Int main()
    {
    Int video = 31;

    Switch(video)
    {

    Number 1-10 : printf("welcome and don't panic ur first steps n");
    Number 11-20: printf("u know some basics ,get ready to Learn important topics");
    Number 21-30 : printf("Now u know the basics ,practice or gtfo");
    Number 31-40 : printf("do basic projects on what u have learned ");
    Default : printf("Get some life ") ;

    } return 0;
    }

  17. Pedro Menegat Avatar

    Can I use switch for strings? I tried to use it but the program always prints the default message. Here's the code:
    int main()
    {

    char state;

    printf("enter your state's initials: n");
    scanf("%s", state);

    switch(state){
    case 'MG' : printf("mineiro n");
    break;
    case 'RJ' : printf("carioca n");
    break;
    case 'SP' : printf("paulista n");
    break;
    default : printf("other states");
    }

    return 0;
    }

  18. Ruthvik _Rajesh Avatar

    Can i use it for strings

  19. shizyninjarocks Avatar

    Incredible useful and incredible funny.

  20. Shaikh Rezwan Rafid Ahmad Avatar

    So what I was asked to code is 'Ask the user for a number, a mathematical operator(+,,* and / ) then ask another number. Then print the product. For ex: if a user inputs 5 and used '' and then 3, it should print 2. Use switch-case method.

    My code is:
    Somehow it doesn't work. 😐 Any solutions?
    Thanks!

    int main()
    {
    char opp;

    float num1, num2 ;

    printf("Enter a numer: n");
    scanf(" %f", &num1 );

    printf("Enter which mathematical operator you want to use(+/-/* and / for divison): ");
    scnaf(" %c", &opp );

    printf("Enter a numer: n");
    scanf(" %f", &num2 );

    float addition = num1 + num2;
    float multiplication = num1*num2;
    float division = (float)num1/(float)num2;
    float subtraction = num1 – num2;

    switch(opp)
    {

    case '+' : printf("Result: %.2f" , addition);
    break;

    case '-' : printf("Result: %.2f" , subtraction);
    break;

    case '*' : printf("Result: %.2f" , multiplication);
    break;

    case '/' : printf("Result: %.2f" , division);
    break;

    default : printf("Invalid operator used");
    break;

    }

    return 0;
    }

  21. Ruthvik _Rajesh Avatar

    Thanks a lot Mr Albert but I have found my solution. The keyword is STRCMP. You use it like this

    if(strcmp(variable , "What it can be for eg: banana")==0){
    your code
    }

  22. Ruthvik _Rajesh Avatar

    what if we enter a word, as a case for e.g.:

    switch(variable){
    case "calci" : printf("bla bla");
    break;

  23. Wishal Avatar

    I need help i tested my program and tried to input a lowercase alphabet and it seems to not accept it plz help.

    Here is the code
    int main()
    {

    char grade[1];

    printf("Enter your grade: ");
    scanf(" %s", &grade);

    if (!grade[1]){
    switch (grade[0])
    {
    case 'A' : printf("Well done!n");
    break;
    case 'B': printf("Do better!n");
    break;
    case 'C' : printf("Bad!n");
    break;
    case 'D' : printf("Horrible!n");
    break;
    case 'F': printf("Failed wow..n");
    break;

    default : printf("That doesn't exist");
    }
    }else printf("???");
    return 0;
    }

  24. Deniz Doğru Avatar

    I guess he plays Candy Crush.

  25. Anthony Symanski Avatar

    ur videos save my life for college

  26. Jones Trabelsi Avatar

    Hey Bucky!
    i tried to modify the program by letting someone else type in their grade and then continue the switch-statement but if i would type in "Donuts" (for example) then the program would execute case D rather than default. probably because the word starts with D. Any ideas how to fix this?

    PS
    Big fan 🙂

  27. Audai Al-Fandi Avatar

    Hello Bucky ..
    Thanks for the amazing tutorials…
    I just want to point out that when the switch statement finds a valid case it executes the case and all the statements below it including the default case… and that's why we use break statement to skip the rest of the switch block and avoid executing other cases….
    that's what makes Switch Statement better than if in various problems…

    maybe my English is bad ..but in the video you Implied that we use break; to avoid testing the rest cases… although switch statement executes the rest cases no matter what the test result is …

  28. Γκα Ζερβας Avatar

    switch aint gonna work for strings right?

  29. ZURATAMA1324 Avatar

    Question here!
    Why would I bother to use the if statement if there is a switch statement I could use?
    Switch seems objectively more convenient and versatile to me.

  30. MrTomro Avatar

    You love the D haha

  31. TheMasterNasty Avatar

    OOOOOO snap ,you love the D

Leave a Reply

Your email address will not be published. Required fields are marked *