Computer Fundamentals If Else Statement

Topics: ASCII, Bracket, Switch statement Pages: 8 (1609 words) Published: August 28, 2013
ADAMSON UNIVERSITY
College of Engineering
Computer Engineering Department

Computer Fundamentals and Programming

Experiment No. 2
CONDITIONAL if STATEMENT

I. OBJECTIVES

a. To implement the different conditional if statements in C++ programs. b. To be able to identify the application and limitations of the different conditional if statements.

II. DISCUSSION
A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done to perform our program.

With the introduction of control sequences we are going to have to introduce a new concept: the block of instructions. A block of instructions is a group of instructions separated by semicolons (;) but grouped in a block delimited by curly bracket signs: { and}. Different conditional structure:

a. if and else statement
The general decision making capability in C++ is provided by the if statement. The format of this statement is | if (expression) program statement|

You will see what sort of `expression' you need to use in the following section. The program statement may be a single statement or may be a block of statements enclosed within braces `{}'. If it is a single statement the format is

| if (expression) single statement;|

but if more statements are to be executed as a compound statement or program block the format becomes

| if (expression) { statement1; statement2; //..... and so on as long as you wantstatement_last;}|

A program block, or compound statement, is a series of statements places inside curly braces {}. When the if statement is true, then all the statements inside the {} are run. When the if statement is false, then none of them are run and the program jumps to the next statement after the closing brace `}'

The condition to be tested appears in parenthesis like this ()after the keyword if. If the condition is false the following statement, or compound statement, is ignored but if it is true it is executed. Schematic Representation of an if statement|

|
|

If there is an integer numeric expression in the brackets () such as | if (total) { ...|

then a value of zero for total is considered FALSE and the following statements will not be executed. If the value of total is non-zero it is regarded as TRUE.
This is almost certainly wrong:
if (expression); // ends with semicolon
It is used to execute an instruction or block of instructions only if a condition is fulfilled. Its form is: if (condition) statement
wherecondition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional structure.

For example, the following code fragment prints out x is 100 only if the value stored in variable x is indeed 100: if (x == 100)
cout<< "x is 100";
If we want more than a single instruction to be executed in case that condition is true we can specify a block of instructions using curly brackets { }: if (x == 100)
 {
  cout<< "x is ";
  cout<< x;
 }
We can specify what we want that happens if the condition is not fulfilled by using the keyword else. Its form used in conjunction with if is: if (condition)
statement1;
else
statement2;

For example:
if (x == 100)
 cout<< "x is 100";
else
 cout<< "x is not 100";
prints out on the screen x is 100 if indeed x is worth 100, but if it is not -and only if not- it prints out x is not 100.

b. Nested if Statements
Nested if statements occur when if statements appear inside other ifstatements.if statements can be nested, but care should always be taken to ensure that the else statement is associated with the correct if.

Because the else part of...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Fundamentals of Computer Essay
  • fundamentals of computer Essay
  • computer Essay
  • Computer Fundamentals Essay
  • Computer Fundamentals Essay
  • Computer Fundamentals Basic Essay
  • Essay on Basic Computer Fundamentals
  • Computers Essay

Become a StudyMode Member

Sign Up - It's Free