When to use a brace, when using quotation

In the process of learning to program, there is a lot you can not distinguish and do not know when to use braces, when to use parentheses. This article we will clarify a bit more about them.

1. When to use a brace {} ?

In a statement if-else we mentioned command and the command block, You may recall a little.

Order a task, expression, jaw, control structure ... any single.

Example:

x = x + 2; // đây là một lệnh đơn 
printf("Day la mot lenh\n");  // đây cũng là một lệnh đơn.

block command: is a series of statements surrounded by braces { }.

Example:

{ //dau khoi
    a = 78;
    b = 26;
    printf("Tong %d + %d = %d", a, b, a+b);
} //cuoi khoi

When you want to perform a series of consecutive commands that we use the command block, ie put them into the braces {} and write indented 1 tab for easy look.

So grab kilt is braces whose mission is to create the command block, to set multiple commands into 1 block command.

When to create the command block? That's when you want to execute multiple statements in a case, in a certain situation.

Examples of the quadratic equation 2.

If delta> 0 then we must do the work is:

  • Count x1 test
  • Calculated x2 test
  • Announcement equation 2 experience.

When we use the braces to group 3 command on a command block.

If delta = 0 then we need to do 2 job:

  • Count dual experience x.
  • Announcement equation dual experience

When we use the braces to group 2 command on a 1 block command.

If delta < 0 it shall notify the equation has no solution. So only 1 ordered to do, Unnecessary braces. However, despite your recommendations 1 or multiple commands to do is also gathering it in braces.

// e.g about string - code by nguyenvanquan7826
#include <stdio.h>
#include <math.h>

int main() 
{
    float a, b, c, delta;
    printf("Nhap cac he so a, b, c:\n");
    scanf("%f%f%f", &a, &b, &c);

    delta = b * b - 4 * a * c;

    if( delta > 0 )
    {
        float x1 = ( -b + sqrt(delta) ) / (2 * a);
        float x2 = ( -b - sqrt(delta) ) / (2 * a);
        printf("Phuong trinh co 2 nghiem:\n x1 = %.2f \n x2 = %.2f \n", x1, x2);
    } 

    if( delta == 0 )
    {
        float x = -b  / (2 * a);
        printf("Phuong trinh co nghiem kep x = %.2f\n", x);
    } 

    if(delta < 0 )
    {
        printf("Phuong trinh vo nghiem\n");
    }

    return 0;
}

In the example above, if you do not use braces to transplant the same commands as a block in which so write, we will be dragged as follows:

if( delta > 0 )
    float x1 = ( -b + sqrt(delta) ) / (2 * a);  //(lenh 1)
    float x2 = ( -b - sqrt(delta) ) / (2 * a);  // (lenh 2)
    printf("Phuong trinh co 2 nghiem:\n x1 = %.2f \n x2 = %.2f \n", x1, x2);

if( delta == 0 )
    float x = -b  / (2 * a);
    printf("Phuong trinh co nghiem kep x = %.2f\n", x);

if(delta < 0 )
    printf("Phuong trinh vo nghiem\n");

Like this, Whether you indented poked whatever type the above commands are considered equal. TH VD in delta < 0 or delta = 0, Meanwhile only counted command x1 (lenh 1) is located in the if statement, command also calculated x2 (lenh 2), and command notification 2 experience (lenh 3) outside if, should 2 This command (lenh 2 and Order 3) always run.

2. When to use parentheses () ?

You do plenty of homework and, commands printf, scanf or even function main also quotation (), So when the call or simply build the function, we used this round brackets. In this round brackets can have parameters if.

Example command printf("hello"); then printf command (but essentially it is a function built system) brackets used to round and round brackets in a string “hello”, that is the argument to pass in.

Also in the syntax of the for loop, while, do…while hoặc if-else, switch-case contains parentheses, then this syntax is not a function that just command syntax has predefined severance.

Apprehend that we use parentheses when we call, when building function or used in a certain syntax has been defined. So you need to learn the syntax of the command to make, structures.