Introduction to Programming: Decision Making Structures (If-Else, Switch) - Lab Submission, Lab Reports of Programming Languages

C++ Programs For Students Files

Typology: Lab Reports

2019/2020

Uploaded on 06/06/2020

kashif-khan-13
kashif-khan-13 🇦🇺

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1ST SEMESTER, 1ST Year) LAB EXPERIMENT # 6
________________________________________________________________________
Test Expression
Test Expression
Name: _____________________________________________ Roll No: _____________
Score: ____________Signature of the Lab Tutor: _____________ Date: 10/01/2017
________________________________________________________________________
PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to learn:
DECISION MAKING STRUCTUERE IF, ELSE-IF AND SWITCH IN C++
Discussion:
if, if-else and else if
The if conditional has the form:
if (a>b && a>c)
statement; Single Statement if body
else
statement; Single Statement else body
if (a>b && a>c)
{
statement;
statement; Multiple Statement if body
To introduce Decision making statements in C++, If, If - else, & switch statement
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Introduction to Programming: Decision Making Structures (If-Else, Switch) - Lab Submission and more Lab Reports Programming Languages in PDF only on Docsity!

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


Test Expression

Test Expression

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _____________ Date: 10/01/ ________________________________________________________________________

PERFORMANCE OBJECTIVE Upon successful completion of this experiment, the student will be able to learn: DECISION MAKING STRUCTUERE IF, ELSE - IF AND SWITCH IN C++

Discussion:

if, if-else and else if

The if conditional has the form:

if (a>b && a>c)

statement; Single Statement if body

else

statement; Single Statement else body

if (a>b && a>c)

statement;

statement; Multiple Statement if body

To introduce Decision making statements in C++, If, If - else, & switch statement

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


statement;

else

statement;

statement; Multiple Statement else body

statement;

T he condition is some expression whose value is being tested. If the condition resolves to a value of true,

then the statements are executed before the program continues on. Otherwise, the statements are ignored. If there is only one statement, the curly braces may be omitted, giving the form:

FLOW CHART OF IF-ELSE STATEMENT

Program (IfElse.cpp)

#include <iostream.h>

Test Expression

Body of if

Exit

False

True

Body of else

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


The else if is used to decide between two or more blocks based on multiple conditions:

If condition1 is met, the block corresponding to the if is executed. If not, then only if condition2 is met is the block corresponding to the else if executed. There may be more than one else if, each with its own condition. Once a block whose condition was met is executed, any else ifs after it are ignored. Therefore, in an if-else-if structure, either one or no block is executed.

An else may be added to the end of an if-else-if. If none of the previous conditions are met, the else block is executed. In this structure, one of the blocks must execute, as in a normal if-else.

Here is an example using these control structures:

#include

int main()

{

int x = 6;

int y = 2;

if(x > y)

cout << “x is greater than y\n”;

else if (y > x)

cout << “y is greater than x\n”;

else

cout << “x and y are equal\n”;

return 0;}

The output of this program is x is greater than y. If we replace lines 5 and 6 with

int x =2; int y = 6; then the output is y is greater than x. If we replace the lines with

int x =2; int y = 2; then the output is x and y are equal.

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


Integer or character variable Note: no semicolon here Integer or character constant

First case body

Causes exit from switch

Second case body

Third case body

The switch Statement

The switch statement is also the decision making statement like if statement and if-else statement. The switch statement is used when the bunch of decisions depend on the same variable. It works as if and if-else statements but it is some what clearer and easily understandable at glance. The switch statement starts with the keyword switch (lower case) followed by the variable in the parentheses (). The switch statement takes the decision according to the values of the variable in the parenthesis. The whole body of the switch statement is enclosed by curly braces. Then the switch statement matches the values of the variable with the cases in its body. The keyword case followed by the choice and the colon makes the block of code below it to be executed if the value of the switch variable matches with the choice in the case. You can give multiple cases in the switch statement and the switch statement will try to match the value of the variable with the cases and if no case is matched the control is returned to the default. The key word default followed by the colon causes the block of code below it to be executed if the switch statement does not match any of the case. The break statement is used after the block of code in each case, which causes the control to be transferred out of the switch statement when ever the case executes its block of code. The syntax of the switch statement is given below;

switch (ch)

case 1:

statement;

statement;

break;

case 2:

statement;

statement;

break;

case 3:

statement;

statement

break;

default:

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


Program (Switch.cpp)

#include <iostream.h> #include <conio.h>

void main() { int a, b; char ch;

cout<<”Enter first number: “; cin>>a;

cout<<”Enter second number: “; cin>>b;

cout<<”Enter the operator: “; ch=getche(); cout<<endl;

switch(ch) { case ‘+’: cout<<”a + b = ”<<a+b; break;

case ‘-’: cout<<”a - b = ”<<a-b; break;

case ‘’: cout<<”a * b = ”<<ab; break;

case ‘/’: cout<<”a / b = ”<<a/b; break;

default: cout<<”You have entered the wrong operator”; }

getch(); }

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6


EXERCISE

1) Convert the following into Pseudocode, make flow diagram for if-else and switch and then Write a C++ program using if-else & switch to generate the following output

Enter age>>

And prints the following:

If Age is greater than 45 then prints the message “You are old stay at home and wait for call”

If Age is greater than 30 and less than 45 then prints “Hey! man enjoy your life with your kids”

If Age is greater than 20 and less than 30 then prints “Cool! have a search for your best suit”

If Age is greater than 10 and less than 20 then prints “Work hard! Your days to study”

_If Age is less than 10 then prints the message “Oh! Kid your days to cry”

  1. Develop a C++ program which will compute the power dissipation of a resistor, when the user inputs current value and the resistance. Program warns the user if power dissipation is above 1 watt. The power dissipation is measured by P=I_^2 _R.
  2. Write a C++ program that displays: *** Welcome to Geometry Calculator *** Enter ‘C’ If you want to calculate area of circle Enter ‘R’ If you want to calculate area of rectangle Enter ‘S’ If you want to calculate area of square Enter ‘T’ If you want to calculate area of triangle Depending on the input by user, program should ask for required data and give the area in result.
  3. Write a program that calculates the total marks secured by a student in the following five courses: ITP, AP, FE, AC, IS. Also calculate the percentage. The program must assign grade according to the percentage secured by student. The program should also notify if the student is fail. Hint: Make use of nested if – else structure. CAN WE WRITE THE SAME CODE USING switch?? iF SO PLEASE WRITE THE CODE. 5)_ If integer variable currentNumber is odd, change its value so that it is now 3 times currentNumber plus 1, otherwise change its value so that it is now half of currentNumber (rounded down when currentNumber is odd).

LAB SUBMISSION DUE 7 TH^ DECEMBER 2017

******************* THE END *****************