Expt2 If Ching

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

ADAMSON UNIVERSITY

College of Engineering
Computer Engineering Department

Computer Fundamentals and Programming

Experiment No. 2
CONDITIONAL if STATEMENT

SCORE

Name: Herald Rabbi G. Ching


Schedule: WF:7-10 AM
Date: August 31, 2016
Instructor: Engr. Yolly Austria
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)
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

Computer Fundamentals and Programming I

Page 2

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
where condition is the expression that is being evaluated. If this condition is
true, statement is executed. If it is false, statement is ignored (not
Computer Fundamentals and Programming I

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.

Example 1 - Testing for Range and the Number of Digits in a Number

Computer Fundamentals and Programming I

Page 5

//Program to illustrate the use of nested if statements


#include <iostream>
int main(void)
{
// define variable
int number;
// get number
cout<< "Enter a number between 1 and 99:
";
cin>> number;
if (number > 0 && number < 100 )
{
if (number < 10)
cout<< "One digit number\n";
else
cout<< "Two digit number\n";
} // end if
else
cout<< "Number not in range\n";
return 0;
} // end main

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 { }.

Computer Fundamentals and Programming I

Page 8

III. Procedures:
1.
2.
3.
4.

Encode the C++ program below.


Save as the file name indicated.
Compile and run the program by pressing Ctrl + F9.
Capture the output of the program by using snipping / print screen
tool.
5. Discuss the output of the program.
6. Upload your work in E-Learning, filename: LASTNAME.DOC. (Title
page and Output/Analysis pages only).

Computer Fundamentals and Programming I

Page 9

Computer Fundamentals and Programming I

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.

Computer Fundamentals and Programming I

Page 12

3.

Computer Fundamentals and Programming I

Page 13

You might also like