Skip to Content
Combining Step 1 and Step 2

We can both declare and assign a value to a variable in a single initialization statement.

Suppose we have these two lines:

// Declare a variable int score; // Initialize a variable score = 0;

We can actually combine these two lines into a single line of code:

int score = 0;

This means we are declaring an integer called score and setting it equal to 0.

Note: We only need to declare a variable one time! And it is highly suggested to initialize a variable before using it later.



In your program, declare and initialize an int variable named year and initialize its value as 2019.


Type the following commands in the terminal and press enter:


g++ variable.cpp


Folder Icon

Take this course for free

Already have an account?