2.12: Variable Assignment and Initalization

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

there are two alternative techniques for initializing variables.

The first is to enclose the initialization value in a set of parentheses, just after the variable name. Here is how you would define the value variable and initialize it to 5 using this technique: int value(5); The second alternative, introduced in C++ 11, is to enclose the initialization value in a set of curly braces, just after the variable name. Here is how you would define the value variable and initialize it to 5 using the brace notation: int value {5}; // This only works with C++ 11 or higher.

The ____________word is intended to simplify the syntax of declarations that are more complex than the ones .

auto key

Declare a numerical variable precise and initialize it to the value 1.09388641.

double precise=1.09388641;

Given two double variables , bestValue and secondBestValue, write some code that swaps their values . Declare any additional variables as necessary.

double tempValue; tempValue=bestValue; bestValue=secondBestValue; secondBestValue=tempValue;

Given an integer variable drivingAge that has already been declared , write a statement that assigns the value 17 to drivingAge.

drivingAge=17;

Declare a variable temperature and initialize it to 98.6.

float temperature=98.6;

Given two integer variables num and highest, write a statement that gives highest the same value that num has.

highest = num;

When a value is assigned to a variable as part of the variable's definition, it is called an ___________

initialization

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand=13;

Declare and initialize the following variables : A variable monthOfYear, initialized to the value 11 A variable companyRevenue, initialized to the value 5666777 A variable firstClassTicketPrice, initialized to the value 6000 A variable totalPopulation, initialized to the value 1222333

int monthOfYear=11; long companyRevenue=5666777; int firstClassTicketPrice=6000; long totalPopulation=1222333;

Write a statement that declares an int variable presidentialTerm and initializes it to 4.

int presidentialTerm=4;

Given three already declared int variables , i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of i and then assign j's value to i. The original value of i, which was saved in temp, can now be assigned to j.

temp=i; i=j; j=temp;

It checks to make sure the value you are initializing the variable with matches the data type of the variable. For example, assume that doubleVal is a double variable with 6.2 stored in it. Using the assignment operator, it is possible to write either of the following statements: int value1 = 4.9; // This will store 4 in value1 int value2 = doubleVal; // This will store 6 in value2

the brace notation offers what advantage?

Declare a string variable named str and initialize it to Hello.

string str="Hello";

Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values . Declare any additional variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.

int temp; temp=firstPlaceWinner; firstPlaceWinner=secondPlaceWinner; secondPlaceWinner=temp;

The most common way to initialize a variable is to use the assignment operator in the variable definition statement.

int value = 5;

Write a declaration of an int variable year and initialize it to 365.

int year=365;

Given two int variables , i and j, which have been declared and initialized , and two other int variables , itemp and jtemp, which have been declared , write some code that swaps the values in i and j by copying their values to itemp and jtemp, respectively, and then copying itemp and jtemp to j and i, respectively.

itemp=i; jtemp=j; i=jtemp; j=itemp;

In C++ terminology, the operand on the left side of the = symbol must be an __________

lvalue.

Write a statement to set the value of num to 4 (num is a variable that has already been declared ).

num=4;

The operand on the right side of the = symbol must be an_________

rvalue.

The ___________of a variable is the part of the program where the variable may be used.

scope


Kaugnay na mga set ng pag-aaral

Muscles/ imbalances/ corrections

View Set

Textbook Chapter 6: Groups and Teamwork

View Set

Advanced Pharm Final Questions, Katzung pharmacology chap1 questions, Pharm II - MCQs, Pharm ABX : Overview + Penicillin

View Set

Unit 2 : Visual Perception & Object Recognition

View Set