Monday, 8 April 2019

4. DECISION-MAKING STATEMENTS (PART-2)

     DECISION-MAKING STATEMENTS 


When the above code is compiled and executed, it produces the following result: 


Value of a is not matching 

Exact value of a is : 100 

Switch Statement 
 A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. 

Syntax 

 The syntax for a switch statement in C++ is as follows: 



The following rules apply to a switch statement: 
 • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. 
 • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. 
 • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. 
 • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. 
 • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 
 • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. 
 • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. 

Flow Diagram :









This would produce the following result: 



You passed 

Your grade is D 

Nested if Statement 
 It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s). 




When the above code is compiled and executed, it produces the following result: 


Value of a is 100 and b is 200 

Exact value of a is : 100 

Exact value of b is : 200 



3. DECISION-MAKING STATEMENTS 




Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
 Following is the general from of a typical decision making structure found in most of the programming languages: 




C++ programming language provides following types of decision making statements.

Statement Description     if statement An ‘if’ statement consists of a boolean expression  followed by one or more statements.   if...else statement An ‘if’ statement can be followed by an optional  ‘else’ statement, which executes when the boolean  expression is false.   switch statement A ‘switch’ statement allows a variable to be tested 



 C++
   for equality against a list of values.   nested if statements You can use one ‘if’ or ‘else if’ statement inside  another ‘if’ or ‘else if’ statement(s).   nested switch statements You can use one ‘switch’ statement inside another  ‘switch’ statement(s). 


If Statement
 An if statement consists of a boolean expression followed by one or more statements.

Syntax
 The syntax of an if statement in C++ is:


if(boolean_expression)

{

// statement(s) will execute if the boolean expression is true

}

If the boolean expression evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Flow Diagram 



 When the above code is compiled and executed, it produces the following result:


a is less than 20; 

value of a is : 10 

if…else Statement
 An if statement can be followed by an optional else statement, which executes when the boolean expression is false.




If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.








When the above code is compiled and executed, it produces the following result:


a is not less than 20; 

value of a is : 100 

if...else if...else Statement
 An if statement can be followed by an optional else if...else statement, which is very usefull to test various conditions using single if...else if statement.
 When using if , else if , else statements there are few points to keep in mind.
 • An if can have zero or one else's and it must come after any else if's. 
 • An if can have zero to many else if's and they must come before the else. 
 • Once an else if succeeds, none of he remaining else if's or else's will be tested. 



2. CONSTANTS/LITERALS

2. CONSTANTS/LITERALS   

Constants refer to fixed values that the program may not alter and they are called literals.
 Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.
 Again, constants are treated just like regular variables except that their values cannot be modified after their definition.
 Integer Literals
 An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
 Here are some examples of integer literals:


212 // Legal
215u // Legal
0xFeeL // Legal 078 // Illegal: 8 is not an octal digit
032UU // Illegal: cannot repeat a suffix

Following are other examples of various types of Integer literals:


85 // decimal
0213 // octal 0x4b // hexadecimal
30 // int
30u // unsigned int 30l // long 30ul // unsigned long

Floating-point Literals
 A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.






While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
 Here are some examples of floating-point literals:


3.14159 // Legal
314159E-5L // Legal
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent .e55 // Illegal: missing integer or fraction

Boolean Literals
 There are two Boolean literals and they are part of standard C++ keywords:
 • A value of true representing true.
 • A value of false representing false.
 You should not consider the value of true equal to 1 and value of false equal to 0.
 Character Literals
 Character literals are enclosed in single quotes. If the literal begins with L (uppercase only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of variable. Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a simple variable of char type.
 A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
 There are certain characters in C++ when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a list of some of such escape sequence codes:

Escape sequence Meaning     \\ \ character   \' ' character   \" " character   \? ? character 



\a Alert or bell   \b Backspace   \f Form feed   \n Newline   \r Carriage return   \t Horizontal tab   \v Vertical tab   \ooo Octal number of one to three digits   \xhh . . . Hexadecimal number of one or more digits 


Following is the example to show a few escape sequence characters:


#include <iostream>

using namespace std;


int main()

{

cout << "Hello\tWorld\n\n";
 return 0;

}



When the above code is compiled and executed, it produces the following result:


Hello World

String Literals
 String literals are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.




You can break a long line into multiple lines using string literals and separate them using whitespaces.
 Here are some examples of string literals. All the three forms are identical strings.


"hello, dear"


"hello, \


dear"


"hello, " "d" "ear"

Defining Constants
 There are two simple ways in C++ to define constants:
 • Using #define preprocessor.
 • Using const keyword.
 The #define Preprocessor
 Following is the form to use #define preprocessor to define a constant:


#define identifier value

Following example explains it in detail:


#include <iostream>

using namespace std;


#define LENGTH 10

#define WIDTH 5

#define NEWLINE '\n'


int main()

{


int area;


area = LENGTH * WIDTH;


 C++



cout << area;

cout << NEWLINE;

return 0;

}

When the above code is compiled and executed, it produces the following result:


50
 The const Keyword
 You can use const prefix to declare constants with a specific type as follows:


const type variable = value;

Following example explains it in detail:


#include <iostream>

using namespace std;


int main()

{

const int LENGTH = 10;

const int WIDTH = 5;

const char NEWLINE = '\n';

int area;


area = LENGTH * WIDTH;
 cout << area;

cout << NEWLINE;

return 0;

}

When the above code is compiled and executed, it produces the following result:


50

Note that it is a good programming practice to define constants in CAPITALS. 

1. Starting of c++

1. OVERVIEW 


C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.
 C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features.

 C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.

 C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
 Note: A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.

 Object-Oriented Programming
 C++ fully supports object-oriented programming, including the four pillars of object-oriented   development:
 • Encapsulation 
 • Data hiding 
 • Inheritance 
 • Polymorphism 
 Standard Libraries
 Standard C++ consists of three important parts:
 • The core language giving all the building blocks including variables, data types and literals, etc. 
 • The C++ Standard Library giving a rich set of functions manipulating files, strings, etc. 
 • The Standard Template Library (STL) giving a rich set of methods manipulating data structures, etc. 
 The ANSI Standard
 The ANSI standard is an attempt to ensure that C++ is portable; that code you write for Microsoft's compiler will compile without errors, using a compiler on a Mac, UNIX, a Windows box, or an Alpha.
The ANSI standard has been stable for a while, and all the major C++ compiler manufacturers support the ANSI standard.
 Learning C++
 The most important thing while learning C++ is to focus on concepts.
 The purpose of learning a programming language is to become a better programmer; that is, to become more effective at designing and implementing new systems and at maintaining old ones.
 C++ supports a variety of programming styles. You can write in the style of Fortran, C, Smalltalk, etc., in any language. Each style can achieve its aims effectively while maintaining runtime and space efficiency.

 Use of C++ 
 C++ is used by hundreds of thousands of programmers in essentially every application domain.
 C++ is being highly used to write device drivers and other software that rely on direct manipulation of hardware under real-time constraints.
 C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts.

Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.