Tuesday, September 29, 2020

thumbnail

Switch Statement in C

 Hello Guys, 

    Switch statements are alternative for if statements. It is usually used in specific situations.

    The conditional operator and the if statements make it easy to write a program that chooses between two alternatives.

    However many times a program needs to choose one of several alternatives.
         You can do this by using else if; else if ... else.
            It is tedious and prone to error, but switch statements are cleaner and efficient.

    When the value of the variable is successively compared against different values, use the switch statements, it is more convenient and efficient.

Syntax: 

switch (expression)
{
    case value1:
        statement1;
        ...
        break;
    case value2:
        statement2;
        ...
        break;
    .
    .
    .
    default:
        statement;
        ...
        break;
}
    The expression enclosed within the parameters is successively compared against the values: value1, value2, ..., valueN.
        The case must be a simple constant or constant expression.

    If the case is found whose value is equal to the value of the expression then the statements that follow the case are executed.
           When more than one statement is included, they do not have to be enclosed within the braces. (as        shown in the syntax above)

    The break statement signals the end of a particular case and causes execution of the switch statement to be terminated.
        We must include the switch statement after every case.
        Forgetting to do so for a particular case causes the program execution into the next case.

    The spacial optional case called default is executed if the value of expression does not match any of the case values. It is same as "fall through" else.

Example: 

#include<stdio.h>
enum weekDay {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
enum weekDay today = Monday;
int maint()
{
    switch (today)
    {
        case Sunday:
            printf("Today is Sunday");
            break;
        case Monday:
            printf("Today is Monday");
            break;
        case Tuesday:
            printf("Today is Tuesday");
            break;
        default:
            printf("whatever");
            break;
    }
    return 0;
}

You can check that out by removing the break statement. and then by changing the value of today.

Output:
 Today is Monday
Click here if you are unsure about enums

Another Example:

// Program to use the switch statement for calculation of two numbers...
#include<stdio.h>
int main(void)
{
    float value1, value2;
    char operator;
    printf("Type in your expression.\n");
    scanf("%f %c %f", &value1, &operator &value2);
    switch(operator)
    {
        case '+':
            pritnf("%.2f\n", value1 + value2);
            break;
        case '-':
            pritnf("%.2f\n", value1 - value2);
            break;
        case '*':
            pritnf("%.2f\n", value1 * value2);
            break;
        case '/':
            if (value2 == 0)
                printf("Division not possible \n");
            else:
                pritnf("%.2f\n", value1 / value2);
            break;
        default:
            print("UNKNOWNOPERATORError"")
            break;
    }
    return 0;
}

Comment down the output...

You can also get the video to understand the topic on my YouTube channel: eat_sleep_ code_repeat (Subscribe) 



Thanks, allow me to complete.

Comments what you feel about the article and suggest me the topic. 

This is Harsh Jaiswal Signing off.
Take Care.











Saturday, September 26, 2020

thumbnail

enums and char | Data Types | C

 Hello Guys, 


In this blog, we will be discussing enums and char in C. 

Enums

    A datatype that allows a programmer to define a variable and specify the valid values that can be stored into the variables.

    For example, let's create a variable named "optCompany" and it can only contain one of the primary colors as "GOOGLE", "MICROSOFT", or "NETFLIX", and no other values.

HOW TO DEFINE THE ENUM TYPE

    You have to define the enum type and give it a name as:

        1) initiated by keyword enum
        2) then the name of the enumerated datatype 
        3) then the list of identifiers (enclosed within a set of curly braces) that define the permissible values that can be assigned to the type.

enum optCompany {GOOGLE, MICROSOFT, NETFLIX};
    Variables declared to be of this datatype can be assigned GOOGLE, MICROSOFT, and NETFLIX inside the program, and no other value.

DECLARING THE VARIABLE 

    To declare the variable to be of type enum optCompany 
        1) use the keyword enum
        2) followed by the enumerated datatype
        3) followed by the variable list so the statement.

enum optCompany myCompany, jakesCompany;

The above sentence defines the variable myCompany and jakesCompany to be of optCompany datatype.

The only permissible values that can be assigned to these variables are the names GOOGLE, MICROSOFT, and NETFLIX.

As 
myCompany = GOOGLE;
Another Example,
enum month {January, February, March, April, May, June, July, August, September, October, November, December};

ENUM AS INT

    The compiler actually treats enumeration identifiers as an integer constant.
        The first name on the list is 0.

example;
enum month thisMonth;
....
thisMonth = February;
    Here, the value 1 is assigned to this month (not the name February) because it is the second identifier listed inside the enumeration list.

    If you want to have the special integer value associated with an enumeration identifier the integer can be assigned to the identifier directly when the data type is defined.

eg.
enum direction {up, down, left = 10, right};
        1) an enumerated datatype direction is defined with the values up, down, left, right.
        2) up = 0 because it appears first in the list.
        3) 1 to down because it appears next
        4) 10 to left because it is explicitly assigned this value
        5) 11 to the right because it appears immediately after left on the list.

Char

    char represents a single character such as the letter 'a' and a digital number '7', or a semicolon ';'.

    Character literal uses single quotes such as 'A' or '8'. 

    You can also declare the char variable to be unsigned.
        It can also be used to explicitly tell the compiler that a particular variable is a signed quantity.

DECLARING THE CHAR

char hello;            /* declare a char variable */
hello = 'T'            /* OK */
hello = "T"          /* No! Thinks T is a string --- ERROR!!! */
hello = T            /* Searches for the variable T in the program --ERROR!!!*/
char grade = 65;    /* OK for ASCII, but poor style */

Escape Characters

    C contains a special character that represents an action as:
  •         backspacing
  •         going to the next line
  •         making the terminal bell ring
    We can represent these actions by using a special; symbol a sequence called an escape sequence.

    These are also encoded in single quotes when assigned to a char variable

    Below are the escape characters:::

                        \n                                    ---        next line
                        \a                                    ---        Alert (ANSI C)
                        \b                                    ---        Blankspace
                        \f                                    ---        Form Feed
                        \r                                    ---        carriage return
                        \t                                    ---        horizontal tab
                        \v                                    ---        vertical tabs
                        \\                                     ---        backslash (\)
                        \'                                     ---        single quotes (')
                        \"                                    ---        double quotes (")
                        \?                                    ---        Question mark (?)
                        \Ooo                               ---        Octal Values
                        \xhh                                ---         Hexadecimal values

Example:

/* program to demonstrate enum datatype */
#include<stdio.h>
int main() 
{    
    enum gender {male, female};
    enum gender myGender; 
    myGender = male return
}

/* program to demonstrate char datatype and escape character */
#include<stdio.h>
int main()
{
    char myCharacter = '\n';
    print("%c", myCharacter);
    return 0;
}


You can get the video on my YouTube channel eat_sleep_ code repeat or play the video below.


Thanks, for reading. Do subscribe to getting notified for the next blog. 

Then it is Harsh Jaiswal.
Signing off.



Sieve Of Eratosthenes | Mathematics | Data Structures and Algorithm | Python

Problem Statement: The  Sieve of Eratosthenes  is a method for finding all primes up to (and possibly including) a given natural n . n .  Th...