How to assign values to variables in C++



How to assign values to variables in Python and other languages

This article discusses methods to assign values to variables.

 

Method 1: Direct Initialisation Method

 

// C++ code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // initialising variables directly
    int a = 5;
    // printing value of a
    cout << "The value of a is: " << a;
}

Output:

The value of a is: 5

 

Method 2: Using Conditional Operator (?:)

Below is the syntax in other popular languages.

// C++ code to demonstrate variable assignment
// upon condition using Conditional Operator
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // initialising variables using Conditional Operator
    int a = 20 > 10 ? 1 : 0;
    // printing value of a
    cout << "The value of a is: " << a;
}

Output:

The value of a is: 1

Last Updated on November 13, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs