Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PLD Experiment 5

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

Lab 5: Conditional Statement: IF/ELSE Statement

5.1 Introduction
C++ if Statement

if (testExpression)

// statements

The if statement evaluates the test expression inside parenthesis.


If test expression is evaluated to true, statements inside the body of if is executed.
If test expression is evaluated to false, statements inside the body of if is skipped.

How if statement works?

Flowchart of if Statement

Above figure describes the working of an if statement.


Example 1: C++ if Statement

PROGRAM # 5.1

#include <iostream>

using namespace std;

int main()

int number;

cout << "Enter an integer: ";

cin >> number;

// checks if the number is positive

if ( number > 0)

cout << "You entered a positive integer: " << number << endl;

cout << "This statement is always executed.";

return 0;

Output 1

Enter an integer: 5

You entered a positive number: 5

This statement is always executed.

Output 2

Enter a number: -5

This statement is always executed.

C++ if...else

The if else executes the codes inside the body of if statement if the test expression is true and
skips the codes inside the body of else.
If the test expression is false, it executes the codes inside the body of else statement and skips
the codes inside the body of if.

How if...else statement works?

Flowchart of if...else

Example 2: C++ if...else Statement

PROGRAM #5.2

#include <iostream>

using namespace std;

int main()

{
int number;

cout << "Enter an integer: ";

cin >> number;

if ( number >= 0)

cout << "You entered a positive integer: " << number << endl;

else

cout << "You entered a negative integer: " << number << endl;

cout << "This line is always printed.";

return 0;

Output

Enter an integer: -4

You entered a negative integer: -4.

This line is always printed.

C++ if...elseif….else

The if...else statement executes two different codes depending upon whether the test expression is
true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The nested if...else statement allows you to check for multiple test expressions and execute different
codes for more than two conditions.

Syntax of if...elseif….else

if (testExpression1)

// statements to be executed if testExpression1 is true

}
else if(testExpression2)

// statements to be executed if testExpression1 is false and


testExpression2 is true

else if (testExpression 3)

// statements to be executed if testExpression1 and testExpression2 is


false and testExpression3 is true

else

// statements to be executed if all test expressions are false

Example 3: C++ if...elseif….else

PROGRAM # 5.3

#include <iostream>

using namespace std;

int main()

int number;

cout << "Enter an integer: ";

cin >> number;

if ( number > 0)

cout << "You entered a positive integer: " << number << endl;
}

else if (number < 0)

cout<<"You entered a negative integer: " << number << endl;

else

cout << "You entered 0." << endl;

cout << "This line is always printed.";

return 0;

Output

Enter an integer: 0

You entered 0.

This line is always printed.

Conditional/Ternary Operator ?:

A ternary operator operates on 3 operands which can be used instead of a if...elsestatement.


Consider this code:

if ( a < b ) {

a = b;

else {

a = -b;

You can replace the above code with:

a = (a < b) ? b : -b;
The ternary operator is more readable than a if...else statement for short conditions.

5.2 Prelab
1. Execute PROGRAM #5.1 and explain.
2. Execute PROGRAM #5.2 and explain.
3. Execute PROGRAM #5.3 and explain.

5.3 Lab Procedure

1. Write a program that will execute a simple calculator program.


2. Write a program of a simple ATM machine

5.4 Sample Output


Output 1

Input First Number: 1


Input Second Number: 4
MENU:
[1] ADD
[2]SUBTRACT
[3]MULTIPLY
[4]DIVIDE
Operation: 1
Sum is 5

Output 2

MENU
[1]Balance Inquiry
[2]Deposit
[3]Withdraw
[4]Balance Inquiry
Transaction: 1
Current Balance: 0
………
Transaction: 2

Enter Deposit Amount: 100


Current Balance: 100
………
Transaction: 3

Enter Withdraw Amount: 50


Current Balance: 50

You might also like