Biggest of Two Numbers Using Ternary Operator: C

Lets find biggest of 2 numbers using ternary operator / conditional operator.

Related Read:
Ternary Operator / Conditional Operator In C

To find biggest of Two numbers using if-else control structure:
Biggest of Two Numbers: C

Source Code: Biggest of Two Numbers using ternary operator: C

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    (a > b) ? (big = a) : (big = b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
5
6
Biggest of 5 and 6 is 6

Output 2:
Enter 2 numbers
40
15
Biggest of 40 and 15 is 40

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    big = (a > b) ? (a) : (b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
500
900
Biggest of 500 and 900 is 900

In above source code, if a is bigger than b, then value of a is returned and stored in variable big orelse value of variable b is stored in variable big.

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Biggest of 2 Numbers Using Ternary Operator: C


[youtube https://www.youtube.com/watch?v=TB11OWMyiR8]

YouTube Link: https://www.youtube.com/watch?v=TB11OWMyiR8 [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Ternary Operator / Conditional Operator In C

In this video tutorial we will show you how to use Conditional Operator. They are also called Ternary Operators as they take 3 arguments.

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

C Program: Ternary Operator / Conditional Operator

 
#include < stdio.h >

int main()
{
    int a = 1, b = 1, c;

    // (expression1) ? (expression2) : (expression3);

     (a+b) ? (c = 10) : (c = 20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 10

a+b is 2 which is non-zero, so it is considered as true. So c = 10 gets executed.

 
#include < stdio.h >

int main()
{
    int a = 1, b = 1, c;

    // (expression1) ? (expression2) : (expression3);

     (a-b) ? (c = 10) : (c = 20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 20

a-b is 0 which is zero, so it is considered as false. So c = 20 gets executed.

 
#include < stdio.h >

int main()
{
    int a = 1, b = 1, c;

    // (expression1) ? (expression2) : (expression3);

    c = (a+b) ? (10) : (20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 10

 
#include < stdio.h >

int main()
{
    int a = 1, b = 1, c;

    // (expression1) ? (expression2) : (expression3);

    c = (a-b) ? (10) : (20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 20

Ternary Operator / Conditional Operator In C


[youtube https://www.youtube.com/watch?v=wubWvdcjMcw]

YouTube Link: https://www.youtube.com/watch?v=wubWvdcjMcw [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

ngIf, index, first, last: Ionic 2

Today lets see how we can track the index number of the loop. i.e., the number of completed iterations in a loop. We shall also learn *ngIf conditional operator and its usage.

Related Read:
Basics of Page Component: Ionic 2

*ngIf Conditional Operator

ngIf is called conditional operator because it operates based on conditions i.e., if the conditional statement it has been assigned is true, then the node it is attached to will be rendered or else it’ll not. *ngIf takes boolean values.

Example: If *ngIf is attached to a div and the condition is false, then the div it has been attached to won’t be added to the DOM. If the condition is true, then div is added to the DOM.

Video Tutorial: ngIf, index, first, last: Ionic 2


[youtube https://www.youtube.com/watch?v=Cb4a-oh_yXM]

YouTube Link: https://www.youtube.com/watch?v=Cb4a-oh_yXM [Watch the Video In Full Screen.]


index, first, last
While we are looping through some array elements inside template file we can know the index of the loop using index variable. We can use ngIf and do something at the first iteration using first variable and similarly we can know the last iteration using last variable.

Source Code: src/pages/home/home.ts

<ion-list no-lines>
  <ion-item *ngFor="let company of companies; 
                     let i = index; 
                     let lst = last; 
                     let fst = first;">
    {{i+1}}. {{company.name}}  
    <span *ngIf="lst"> - Am last!</span>
    <span *ngIf="fst"> - Am first</span>
  </ion-item>
</ion-list> 

inside *ngFor we have initiated and assigned index value to variable i, last index value to variable lst and first index value to variable fst.

Inside ion-item we are using *ngIf to check if the iteration is first iteration, if so add ‘Am first’ message besides the first list item i.e., beside Microsoft. We also check if the iteration is a last iteration, if so we display ‘Am last!’ message beside the last item in the list i.e., beside IBM.

Output:

  1. Microsoft – Am first
  2. Apple
  3. Google
  4. Oracle
  5. IBM – Am last!