Showing posts with label Scope. Show all posts
Showing posts with label Scope. Show all posts

Wednesday, March 22, 2017

C++ Part 40

Review Part III
Naming Constants with the const Modifier
When you initialize a variable inside a declaration, you can mark the
variable so that the program is not allowed to change its value. To do this
place the word const in front of the declaration, as described below:
SYNTAX const Type_Name Variable_Name = Constant;
EXAMPLES const int MAX_TRIES = 3;
const double PI = 3.14159;

Boolean (bool) values are true and false
In C++, a Boolean expression evaluates to the bool value true when it is
satisfied and to the bool value false when it is not satisfied.
The Type bool Is New
Older versions of C++ have no type bool, but instead use the integers 1 and
0 for true and false. If you have an older version of C++ that does not
have the type bool, you should obtain a new compiler.


Multiway if-else Statement
SYNTAX
if (Boolean_Expression_1)
Statement_1
else if (Boolean_Expression_2)
Statement_2
.
.
.
else if (Boolean_Expression_n)
Statement_n
else
Statement_For_All_Other_Possibilities
EXAMPLE
if ((temperature < −10) && (day == SUNDAY)) cout << "Stay home."; else if (temperature < −10) //and day != SUNDAY cout << "Stay home, but call work."; else if (temperature <= 0) //and temperature >= −10
cout << "Dress warm."; else //temperature > 0
cout << "Work hard and play hard."; The Boolean expressions are checked in order until the first true Boolean expression is encountered, and then the corresponding statement is executed. If none of the Boolean expressions is true, then the Statement_For_All_Other_Possibilities is executed.

Blocks
A block is some C++ code enclosed in braces. The variables declared in a
block are local to the block and so the variable names can be used outside
of the block for something else (such as being reused as the name for a
different variable).

Scope Rule for Nested Blocks
If an identifier is declared as a variable in each of two blocks, one within
the other, then these are two different variables with the same name. One
variable exists only within the inner block and cannot be accessed outside
of the inner block. The other variable exists only in the outer block and
cannot be accessed in the inner block. The two variables are distinct, so
changes made to one of these variables will have no effect on the other of
these two variables.


The break Statement
The break statement can be used to exit a loop statement. When the
break statement is executed, the loop statement ends immediately and
execution continues with the statement following the loop statement. The
break statement may be used in any form of loop, in a while loop, in a dowhile
loop, or in a for loop. This is the same break statement that we
have already used in switch statements.

Repeat “This Many Times”
A for statement can be used to produce a loop that repeats the loop body
a predetermined number of times.
Pseudocode
Repeat the following This_Many times:
Loop_Body
Equivalent for Statement
for (int count = 1; count <= This_Many; count++) Loop_Body Example for (int count = 1; count <= 3; count++) cout << "Hip, Hip, Hurray\n";

Testing a Loop
Every loop should be tested with inputs that cause each of the following
loop behaviors (or as many as are possible): zero iterations of the loop
body, one iteration of the loop body, the maximum number of iterations
of the loop body, and one less than the maximum number of iterations of
the loop body. (This is only a minimal set of test situations. You should also
conduct other tests that are particular to the loop you are testing.)

Debugging a Very Bad Program
If your program is very bad, do not try to debug it. Instead, throw it out
and start over.

C++ Part 35

Typical Program Organization

# include <iostream>  //Setup section used by compiler
storage class of a variable defines the lifetime and visibility of a variable.
Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible.
There are five types of storage classes in C++.
They are:
  1. Automatic
  2. External
  3. Static
  4. Register // Next Post
  5. Mutable // Next Post
______________________________

1. Automatic Storage Class

Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default.
This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.

Syntax of Automatic Storage Class Declaration

datatype var_name1 [= value];
or
auto datatype var_name1 [= value];

Example of Automatic Storage Class

auto int x;
float y = 5.67;

2. External Storage Class

External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.

Syntax of External Storage Class Declaration

extern datatype var_name1;
For example,
extern float var1;

Example of External Storage Class

Example 1: C++ program to create and use external storage.
File: sub.cpp
int test=100;  // assigning value to test

void multiply(int n)
{
    test=test*n;
}
File: main.cpp
#include<iostream>
#include "sub.cpp"  // includes the content of sub.cpp
using namespace std;

extern int test;  // declaring test

int main()
{
    cout<<test<<endl;
    multiply(5);
    cout<<test<<endl;
    return 0;
}
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100 in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.
Note: Run the main.cpp program
Output
100
500

3. Static Storage Class

Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.

Syntax of Static Storage Class Declaration

static datatype var_name1 [= value];
For example,
static int x = 101;
static float sum;

_________

source:  www.programtopia.net

Saturday, March 11, 2017

Intro to C++ Part 5


  • Storage class: How/where in memory is the variable stored? 
  • Type: What is the set of values for the variable?
  • Name: What is the name of the variable? 
  • Scope: Where in the program is the variable visible? 
  • Value: Which value is currently stored in the variable?

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...