SEONGWOO CHAE
SEONGWOO CHAE

Reputation: 65

Using comma and break have an error in for loop

i have a question about break in for loop.

I know 'comma' can make code using no semicolon.

Most of codes are going well, but 'break' doesn't meet this rule.

#include<stdio.h>

int main(void){

    for(int i=0;i<n;i++) 
        puts("hello"),break;

return 0;
}

In this code, break have an error "Expression expected" and i don't know why..

Is there any problem? :(

Upvotes: 2

Views: 116

Answers (1)

taskinoor
taskinoor

Reputation: 46037

The comma operator needs the operands to be expressions, i.e. something that evaluates to a value. break, continue etc. are not expressions, instead they are statements which do not evaluate to a value. That's why they can't be used with comma operator.

The error is perfectly clear in the message "Expression expected".

Upvotes: 4

Related Questions