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

ngClass Directive: Ionic 2

Today lets learn how to make use of ngClass angular directive in our Ionic 2 application.

Related Read:
ngIf, index, first, last: Ionic 2

ngClass Directive: Ionic 2


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

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



Company Names: src/pages/home/home.ts

import { Component } from '@angular/core';
 
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
items: any = [];
  constructor() {
    this.items = [
      'Microsoft',
      'Apple',
      'Oracle',
      'Google',
      'IBM',
      'Tesla',
      'Technotip'
    ];
  }
}

We have an array of company names which is being assigned to variable items.

ngClass angular directive ionic 2

Output
Microsoft
Apple
Oracle
Google
IBM
Tesla
Technotip

CSS Class: src/pages/home/home.scss

page-home {
    .bold{
        font-weight: bolder;
    }
    .normal{
        font-weight: normal;
    }
}

We define two CSS classes which we use to apply for alternate list items – using ngClass directive. Alternatively the list items appear in bold and in normal font weight.

ngClass Directive – using ternary operator: src/pages/home/home.html

< ion-header>
  < ion-navbar>
    < ion-title>
      Company Names
    < /ion-title>
  < /ion-navbar>
< /ion-header>
 
< ion-content padding>
< ion-list no-lines>
  < ion-item *ngFor="let item of items; let i = index;"
             [ngClass]="(i % 2 == 0) ? 'bold' : 'normal'">
    {{i+1}}. {{item}}
  < /ion-item>
< /ion-list>
< /ion-content>

Here we use ternary operator and whenever i % 2 is zero we apply bold class to that item, if not we apply normal class to that list item.

ngClass Directive: src/pages/home/home.html

< ion-header>
  < ion-navbar>
    < ion-title>
      Company Names
    < /ion-title>
  < /ion-navbar>
< /ion-header>
 
< ion-content padding>
< ion-list no-lines>
  < ion-item *ngFor="let item of items; let i = index;"
             [ngClass]="{'bold': (i % 2 == 0)}">
    {{i+1}}. {{item}}
  < /ion-item>
< /ion-list>
< /ion-content>

This is yet another way of doing the same thing which we did using ternary operator above. Here, if i % 2 is equal to zero, then bold class will be applied, if not it won’t be applied.

changing colors: src/pages/home/home.scss

page-home {
    .bold{
        font-weight: bolder;
        color: blue;
    }
    .normal{
        font-weight: normal;
        color: green;
    }
}

Alternatively the list items appear in bold and in normal font weight, along with blue and green colors respectively.

Biggest of 2 Numbers Using Function: C++

Find the biggest of two numbers using function and ternary operator.

Basics of functions:

A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.

Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.

Function Prototype:

Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:

< return_type > < function_name >( arguments_type );

Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);

Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:

< return_type >< function_name >(parameters)
{
 
}

Example:

int findsum(int a, int b)
{
 int sum;
 
 sum = a + b;
 return(sum);
}

Function Call:
It is a technique used to invoke a function.
Syntax:

[variable] < function_name >([arguments]);

Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();

Full Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include< iostream.h >
#include< conio.h >
 
void main()
{
  int Big(int x,int y);  // Prototype
 
  int a, b;
  clrscr();
 
  cout< <"Enter 2 numbers\n";
  cin>>a>>b;
 
  int res = Big(a, b);   // Function call
 
  cout< <"Biggest = "<<res;
  getch();
}
 
int Big(int x, int y)   // Function Definition
{
  return( x>y?x:y );
}

You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++

Video Tutorial: Biggest of 2 Numbers Using Function


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

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



Output:
Enter 2 numbers
420
305
Biggest = 420

Biggest of 3 Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements and Ternary Operator.

Before watching this video, please make sure to watch and understand this short video: Find Biggest of 3 Numbers: C++

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
 
int big =  ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c )
  big = a;
else if( b > c )
  big = b;
else
  big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}

In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.

int big =  ( a>b && a>c )?a:(b>c?b:c);

if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Video Tutorial: Biggest of 3 Integer Numbers


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

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



Output
Enter 3 no’s
10
20
30
Among 10, 20, 30 Biggest is 30

Biggest of Two Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the two integer numbers in C++, using Ternary Operator.

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

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include < iostream .h >
#include < conio .h >
 
void main()
{
 int a, b;
 clrscr();
 
 cout< <"Enter 2 numbers\n";
 cin>>a>>b;
 
 int big;
 
 big = a > b ? a : b;
 
/*
 if( a > b )
  big = a;
 else
  big = b;
*/ 
 cout< <"\nBig is: "<<big;
 
 getch();
}

In this program we take 2 integer values from the user and using ternary operator decide the biggest of 2 numbers.

 big = a > b ? a : b;

if ( a > b ) is true, then value of a will be stored in variable big; else value of b will be stored in big.

Video Tutorial: Biggest of 2 Integer Numbers Using Ternary Operator


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

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




Output
Enter 3 no’s
404
302
Big is: 404