is ternary operator usable for the following? [duplicate] - c

This question already has answers here:
Why can't I use a "break" statement inside a ternary conditional statement in C++?
(2 answers)
Closed 7 years ago.
just wondering if, for the following code lines, it is possible to replace using a ternary operator:
if( current->chbits[i] != '\0')
printf("%c\n",current->chbits[i]);
else
break;
If so, how would I parse it correctly?

You can't have break in a ternary operator.
See this for more information.

The ternary operator is used to evaluate an expresson conditionally:
result = condition ? first : second;
In your example is no conditional expression but an conditional statement.

Yes, you can replace the code with ternary operator by making some modifications to eliminate the break statement.
for(int i=0, int flag=1 ;flag!=0; i++){
flag=(current->chbits[i]!='\0') ? printf("%c\n",current->chbits[i]) : 0;
}
Here flag won't be zero since printf() returns the number of characters successfully written on the output. When current->chbits[i] != '\0' becomes false flag is set to 0 and for terminates as per the condition flag!=0.

Related

I do not understand this C notation [duplicate]

This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
Closed 10 months ago.
Can someone explain this kind of statement(s)? I didn't get it.
data[i] = ((data[i] == div[i]) ? '0' : '1');
It's an if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
So , your expression can be visualized into if-else statement as:
#include <stdio.h>
int main(){
// Code ...
if(data[i]==div[i])
data[i]='0';
else
data[i]='1';
}
** ‘?:’ takes three operands to work, hence they are also called ternary operators.

Assignment operator works fine as a condition in if Statement in C programs [duplicate]

This question already has answers here:
How the assignment statement in an if statement serves as a condition?
(4 answers)
Closed 2 years ago.
I am aware that when a non zero value is provided as a condition in an if statement, the condition is evaluated to be true.
Yet, I'm wondering how an assignment(=) is evaluated to be true here, instead of a comparison(==), and runs without any errors in the below C program.
int main(){
int a, b =5;
if(a=10) {
("This works");
}
if(b=1){
("This works too");
} }
The assignment operator always returns the value that was assigned. So in your case a=10 has the value 10 and the if statement is essentially if(10). Which is true.
a = 10 is an expression that assigns 10 to a and whose value is the result of the assignment (that is 10).
any value different from 0 is considered to be true
Try this:
if (b = 0) {
("This is never displayed");
}

Using an Int in the if condition [duplicate]

This question already has answers here:
why negative number is taken as true in condition statement? [duplicate]
(5 answers)
Closed 2 years ago.
In my current class I am seeing a lot of times where my teacher puts a number as the condition of an if statement then asks for the output. I'm confused as to how these statements are being evaluated.
Example:
if(-1) {
x = 35;
}
or
if(0) {
x = 35;
}
Why does it go inside the if statement instead of just ignoring it? What is making if(-1) evaluate true? And why does if(0) evaluate false?
This is an online class so I can't get help directly from the teacher usually. I have searched for a few hours on various websites about C for loops, but I haven't found anything relevant yet to my question.
C doesn't have a "real" boolean type. When evaluating integers, 0 is a "false", and anything else is a "true" value.

What a " , " does in a while loop? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
I accidentally typed while(x,0) instead of while(x<0). The code of course didn't work as planned nor got compiler errors so i took a damn hour to find the mistake.
Why it didn't get a compiler error? And what does the , do in the while loop?
while(x,0)
comma is treated as binary operator and it will return 0, so your loop condition will go false.
In C, a comma is "a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)."
Since 0 is false then your code was exiting the loop.
https://en.m.wikipedia.org/wiki/Comma_operator

C: Ternary operator giving error when leaving one expression empty [duplicate]

This question already has answers here:
Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)
(7 answers)
Closed 10 years ago.
I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:
expression result unused
And the code in that particular section does not run.
The code in this case is:
i != a ?: printf("|%*s\\\n", i, "");
Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.
Your code is equivalent to
(i != a) ? (i != a) : printf(...);
Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:
if(i==a) printf(...);

Resources