Rd7
Rd7

Reputation: 45

If statement with {} and without {} inside a For loop

I have a loop of queues in a vector and I need to search in the loop of queues to find the minimum index queue as well as the maximum size queue.

I'm using the following code

int min_index = 0;
int max_size = -1;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
    if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is min_index is the minimum size queue
    if(q[i].size() > max_size)
        max_size = q[i].size(); // maximum size queue
} 

I have a small doubt whether to use the {} for each if statement like the below code

int min_index = 0;
    int max_size = -1;
    std::size_t size = q.size();
    for( i=0; i<size; i++){ //accessing loop of queues
        if(q[min_index].size() > q[i].size()){
            min_index = i; // Now q[min_index] is the shortest queue
              }
        if(q[i].size() > max_size){
            max_size = q[i].size(); // longest queue        
            } 
}

Which one is correct and what will be exactly difference with and without {}. Sorry if it is a silly question. I am new to programming.

Upvotes: 1

Views: 696

Answers (5)

Jean Logeart
Jean Logeart

Reputation: 53829

Usually, the best rule to apply is:

If any doubt, use the {}

Same goes for ()

Yet in your case, both are equivalent.

Upvotes: 3

Neppinger
Neppinger

Reputation: 148

They will run the same but it is some people prefer to use the {} that way if you need to go back and have more than one line of code after your if statement you don't have to add {}.

Upvotes: 1

Maroun
Maroun

Reputation: 95968

In your case, there is no difference between the two codes.

if(SOMETHING)
   DO_SOMETHING;

is the same as

if(SOMETHING) {
   DO_SOMETHING;
}

But

 if(SOMETHING)
   DO_SOMETHING_1;
   DO_SOMETHING_2;   //DO_SOMETHING_2 will be performed whether the `if` condition
                     //is satisfied or not 

is not the same as

 if(SOMETHING) {
   DO_SOMETHING_1;
   DO_SOMETHING_2;   //DO_SOMETHING_2 will be performed only if the `if` condition
 }                   //is satisfied, since it is inside the curly parentheses

In your case, since the if block contains only one statement, there is no need for curly parentheses (although, if it confuses you, it's recommended to use them for clarifying things)..

Upvotes: 4

Smartfox
Smartfox

Reputation: 83

No difference:

int min_index = 0;
int max_size = -1;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
    if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is min_index is the minimum size queue
    if(q[i].size() > max_size)
        max_size = q[i].size(); // maximum size queue
}

The only difference will be if you want to write more then one line in the if, for example

if(q[min_index].size() > q[i].size())
        min_index = i;
        ++i;

In this case only min_index = i; line will be called if your if(q[min_index].size() > q[i].size()) is true.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

The brackets when used that way is called a block-statement, and is used to encapsulate one or more statements. One such block is equal to one statement, which satisfies the parser that expects one statements as the body of an if statement.

So no, there is really no difference between the two. In one you have a single statement, in the other another kind of single statement, that just happens to contain other statements.

Upvotes: 0

Related Questions