У нас вы можете посмотреть бесплатно PPS Experiment 3: Write a c program to demonstrate conditional / ternary expressions / operator или скачать в максимальном доступном качестве, видео которое было загружено на ютуб. Для загрузки выберите вариант из формы ниже:
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием видео, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса ClipSaver.ru
Subject: Programming for Problem-solving Lab (24AF1000ES107L):
Experiment 3: Write a c program to demonstrate conditional / ternary expressions / operator and explain order of Evaluation:
======================================================================================
In C, conditional expressions are used to evaluate conditions and return values based on the outcome of those conditions. The most common form of conditional expression in C is the ternary operator, which is a shorthand for an if-else statement. It has the following syntax:
condition ? expression_if_true : expression_if_false;
The conditional operator in C is called by two more names:
Ternary Operator
? : operator
Ex:
max = (a b) ? a : b;
How It Works:
Condition: This is an expression that evaluates to either true (non-zero) or false (zero).
Expression if true: This expression is executed if the condition is true.
Expression if false: This expression is executed if the condition is false.
//C program to check entered number is positive or negative using if else
#include stdio.h
#include conio.h
void main(){
int i;
i=5;
if(I 0)
printf(“i is a positive number”);
else
printf(“i is negative number”);
getch();
}
Now if we wish to write the above program using conditional/ternary/ ? : operator,
we can write it as follows:
//C program to check entered number is positive or negative using ternary/conditional operator
#include stdio.h
#include conio.h
void main(){
int i;
i=5;
(I 0) ? printf(“number is positive”) : printf(“number is negative”) ;
getch();
}
here, expression 1 is : (I 0)
expression 2 is : printf(“number is positive”)
expression 3 is : printf(“number is negative”)
So first we check the left side expression of question mark “ ? “ i.e (I 0)
since i is 5 and it satisfies the condition, therefore condition is true and left side of “ : “ expression will be executed i.e. printf(“number is positive”)
therefore the output will be number is positive.
Example 2:
Here's a simple example to illustrate the use of a conditional expression:
#include stdio.h
int main() {
int a = 5, b = 10;
int max;
// Using conditional expression to find the maximum
max = (a b) ? a : b;
printf("The maximum value is: %d
", max);
return 0;
}
Explanation of the Example
In this code, we have two integers, a and b.
The conditional expression (a b) ? a : b checks if a is greater than b.
If true, max is assigned the value of a.
If false, max is assigned the value of b.
Finally, it prints the maximum value.
Advantages of Conditional Expressions:
Conciseness: They make the code shorter and often clearer for simple conditions.
Inline Usage: Conditional expressions can be used within larger expressions and function calls, which can enhance readability.
Limitations:
For complex conditions or multiple statements, traditional if-else statements are often clearer and more maintainable.
Overusing conditional expressions can lead to less readable code, especially when nested.
Hope you understood it.
If you have any questions, please feel free to ask.
Good Luck :)