Posts

Showing posts from June 18, 2017

switch statement in cplus plus

Image
The switch construct The switch statement is very similar to the if-else-if ladder of section statements. Here is its general form: switch (expression) { case constant_1: statement_1 or block_1; break; case constant_2: statement_2 or block_2; break; case constant_3: statement_3 or block_3; break; // More cases may follow default statement_X or block_X; } In the switch construct, as in the if-else-if construct, multiple possible blocks of code are being selected from, and only one of them will typically be executed. The presence of the keyword break allows the execution to come out of the switch statement. Omission of break takes control through the next case statements irrespective of whether the value of case matches or not. This can also be used constructively when same operation has to be performed on a number of cases. Each case or possible block of code is initiated by the appearance of the case reserved word, and is terminated by the appearan...

if-else statement in C plus plus

Image
The if-else statement in C++ The if then else statement is used when the question is not whether to execute a statement or statements, but which statement or statements to execute. The simplest if-else construct consists of two options, and its format is: If (logical expression) statement 1 or block 1; else statement 2 or block 2; next_statement If logical expression is TRUE, statement1 (or block1) is executed and then next_statement is executed. If logical expression is FALSE, statement2 (or block2) is executed and then next_statement is executed. For e.g., the following program segment informs the user concerning the existence of real solutions in the quadratic equation. Let a, b, c be the coefficients of the quadratic equation. Then if the roots are real, the discriminate d=b 2 -4ac will be greater than or equal to zero.i.e., D=b*b-4*a*c; If(d>=0) then Message: “roots are real” else Message: “roots are imaginary” For e.g., The following program segme...

If statement in c++

If statement The if section structure is used to select or determine a course of action that depends on the values of program variables. A very simple example of the need for such a structure would be a program to find the roots of quadratic equation based on the values of coefficients of the equation. Depending on the value of a, b, and c in the quadratic equation there are either 0, 1,0r 2 roots of the equation. The procedure followed is to evaluate the discriminate and based on its value we would assign 0, 1, or 2 to the variable indicating the number of roots of the equation. The simple if statement is used when a course of action consists of (a) executing a statement or block of statements, or (b) NOT executing the statement or block of statements. The syntax of If statement is: If (logical expression) statement 1; next_statement If the logical expression is TRUE, statement is executed and then next_statement is executed. If the logical expression is FALSE, ...

for statement in c plus plus

Image
The for statement The syntax of the for statement is: For (initialization; test expression; update expression) { // Boby of loop – block of statements } The three parts of the for statement determine the loop actions. First, the loop initializes the relevant variables. Then there is a test expression, evaluated as true or false. If the test expression is TRUE (this means the test expression is non-zero) the sequence of instructions which forms the body of the loop is executed. Finally, expressions are updated and the process returns to the test expression part of the loop. (The initialization in a loop takes place only when the program enters the loop.) The sum program can be rewritten using for statement as follows: int sumSoFar = 0; //(Start of loop; test condition; increment the loop variable;) for ( int i=1; I < =100; I = i+1)  { sumSoFar = sumSoFar +I; // This is the body of the loop } Example of for loop Here are some examples of simple for loop...

Enumerated data types in c++

Enumerated data types Simple integer constants are required to represent domain specific data. For example, Colour of an automobile can be specified by a set of statements using cost qualifiers: Const int cRED =0; Const int cBLUE = 1; ……… Int auto_colour; Auto_colour=cBLUE; However, there are no checks. Since variable auto_colour is an integer, the following assignments are valid: auto_colour=-1; …….. auto_colour=rand(); Of course, these statements lose the semantics; variable auto_colour doesn’t any longer represent a colour, it is just an integer. It is thus important to be able to tell the compiler: “Variable auto_colour is supported to represented a colour, that is one of the defined set of choices RED, BLUE,…” and then have the compiler check, pretty thoroughly, that auto_colour is only used in this way throughout the program. This leads to “enumerated data types”. Enumerated data types are an alternative way of declaring a set of inte...