Skip to main content
Contents Index
Dark Mode Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.5 Initializing Variables
It is very important to always initialize your variables (that is, give initial values to your variables, even if you plan to change those values immediately). In the following video we discuss two ways to do so.
Separate declaration and initialization:
Declaration and initialization in one statement:
If you cannot see this codecast, please click here .
Video Description.
Separate declaration and initialization for a variable
Declaring and initializing a variable in one line
NEW SYNTAX: int money = 30;
declares an integer variable named "money" and stores the value 30 in it
Check Your Understanding Check Your Understanding
1.
Which of the following variable declaration and initialization statements is equivalent to:
int money = 20;
Great job!
money = int 20;
Not quite - try again!
int 20;
Not quite - try again!
int money 20;
Not quite - try again!
Hint .
Scroll up to the top of this page for the correct syntax.
2.
Find an equivalent variable declaration and initialization statement to:
int apples = 4;
Great job!
apples = 4 int;
Not quite - try again!
int 4;
Not quite - try again!
nt apples 4;
Not quite - try again!
Hint .
Scroll up to the top of this page for the correct syntax.