Thread: Simple if Statement

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14

    Simple if Statement

    I created two programs (Number 1 and Number 2) using simple if statements. As you know there are two way to create program using simple if statements, syntax are:

    Code:
    A. if (condition)
           Statement;
    
    
    B. if(condition)
        {
          Block of statements;
        }

    My both programs are same except i used set brackets in number 2 and I didn't use set brackets in number 1 but both codes are working fine. Is it not necessary to use set brackets when we write "block of statements" ?
    I also want to know a live example of "Block of Statements" I know that when we use more than one statement within set brackets that is called Block of Statements.
    Code:
    if(gross_salary>=10000)
            {
                    tax = gross_salary*0.15;
                net_salary = gross_salary - tax;
            }
    is above one has example of "Block of statements" ?

    Below are my both programs

    Code:
    //Number 1
    /* Program to calculate the net Salary of an employee */
    #include<stdio.h>
    int main()
    {
        float tax, gross_salary, net_salary;
        printf(" Enter Gross Salary of Employee: ");
        scanf("%f", &gross_salary);
        printf(" Employee Gross Salary %9.2f\n", gross_salary);
        if(gross_salary<10000)
            net_salary=gross_salary;
        if(gross_salary>=10000)
            tax = gross_salary*0.15;
            net_salary = gross_salary - tax;
        printf(" Tax: %26.2f", tax);
        printf("\n Net Salary Rs. %16.2f\n", net_salary);
    }
    Code:
    // Number 2
    /* Program to calculate the net Salary of an employee */
    #include<stdio.h>
    int main()
    {
        float tax, gross_salary, net_salary;
        printf(" Enter Gross Salary of Employee: ");
        scanf("%f", &gross_salary);
        printf(" Employee Gross Salary %9.2f\n", gross_salary);
        if(gross_salary<10000)
            net_salary=gross_salary;
        if(gross_salary>=10000)
            {
                    tax = gross_salary*0.15;
                net_salary = gross_salary - tax;
            }
        printf(" Tax: %26.2f", tax);
        printf("\n Net Salary Rs. %16.2f\n", net_salary);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raj21
    Is it not necessary to use set brackets when we write "block of statements" ?
    If you omit the braces then the body of the if statement would be just one statement. As an example, consider:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = 0;
    
        if (n != 0)
            printf("#1\n");
            printf("#2\n");
    
        if (n != 0)
        {
            printf("#3\n");
            printf("#4\n");
        }
    
        return 0;
    }
    If you compile and run the above program, you will find that "#2" is printed even though the statement to print "#2" was indented as if it were part of the body of the first if statement. So, it is usually better to have the braces even when they are not necessary.

    Quote Originally Posted by raj21
    is above one has example of "Block of statements" ?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    Code:
    #include<stdio.h>
    main()
    {
        puts("My doubts");
        system("cls");
        puts("Thanks");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple If statement not working
    By amitj93 in forum C Programming
    Replies: 6
    Last Post: 01-22-2013, 10:35 PM
  2. error with simple printf statement
    By jollykiddo in forum C Programming
    Replies: 2
    Last Post: 12-16-2011, 02:11 AM
  3. Having troubles with a simple print statement.
    By Tails in forum C Programming
    Replies: 4
    Last Post: 09-22-2011, 06:14 PM
  4. Simple if/else statement problem
    By benrogers in forum C Programming
    Replies: 13
    Last Post: 02-06-2011, 04:11 PM
  5. Why won't this simple statement work?
    By nick753 in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2010, 11:20 PM

Tags for this Thread