Professional Documents
Culture Documents
Expt2 If Ching
Expt2 If Ching
Expt2 If Ching
College of Engineering
Computer Engineering Department
Experiment No. 2
CONDITIONAL if STATEMENT
SCORE
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)
if (expression)
{
single statement;
statement1;
statement2;
//..... and so on as long as you want
statement_last;
}
but if more statements are to be executed as a compound statement or
program block the format becomes
Page 2
Page 3
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 if
statements.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 an if...else statement is optional there is an
ambiguity when else is omitted from a nested if sequence. Which of the ifs
does the else belong to? This is solved by associating the else with the
closest else-less if statement. For example in the following fragment:
Computer Fundamentals and Programming I
Page 4
if ( n > 0 )
if (a > b)
z = a;
else
z = b;
The else goes with the inner if. If this isn't what you want braces must be
used:
if ( n > 0 )
{
if (a > b)
z = a;
}
else
z = b;
Generally it is better always to use braces in if ... else statements, even
if you are only using one statement and could miss out writing them. This will
ensure that your code will do what you want, not what you think you want.
You will find that I have mixed the style up - sometimes putting them in,
sometimes not.
Page 5
c. else if statement
The else if part of an if construct permits the implementation of multiway branch decisions.
if (expression1)
{
statements 1;
}
else if (expression2)
{
statements 2;
}
else if (expression3)
{
statements 3;
}
else
{
Computer Fundamentals and Programming I
Page 6
statements 4;
}
These expressions are evaluated in order, if any expression is true, the
statement associated with it is executed and this terminates the whole chain.
As many decisions as desired may be included within the chain. As always,
the code for each statement is either a single statement or a group in
braces.
The last else part handles the "none of the above" or default case,
when none of the other conditions are satisfied. This is optional and can be
omitted.
Schematic
Representation of
an else if statement
else if statements are evaluated starting from the first one, then working
down the list. This means that if there is more than one statement which is
true, only the first one will be executed. The others will be ignored. So if you
have code like this:
x = 10;
if (x>100)
do_thing_1;
else if (x>5)
do_thing_2;
else if (x>0)
do_thing_3;
..... and so on...
Then only thing_2 will execute as it is the first statement which is
TRUE. Even though x is bigger than zero (the third statement), thing_3 will
not run as it has been preempted by thing_2. Having found one TRUE
Computer Fundamentals and Programming I
Page 7
statement, the program executes it, then goes straight to the end of the else
if block.
The if + else structures can be concatenated with the intention of verifying a
range of values. The following example shows its use telling if the present
value stored in x is positive, negative or none of the previous, that is to say,
equal to zero.
if (x > 0)
cout<< "x is positive";
else if (x < 0)
cout<< "x is negative";
else
cout<< "x is 0";
Remember that in case we want more than a single instruction to be
executed, we must group them in a block of instructions by using curly
brackets { }.
Page 8
III. Procedures:
1.
2.
3.
4.
Page 9
Page 10
Laboratory Exercise 2
Computer Fundamentals and Programming I
Page 11
CONDITIONAL if STATEMENT
Direction: Demonstrate the corresponding output for each program
in the boxes below. Give your analysis and observation for
each output.
PROGRAM OUTPUT
ANALYSIS /
OBSERVATION
1.
2.
Page 12
3.
Page 13