





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C++ Programs For Students Files
Typology: Lab Reports
1 / 9
This page cannot be seen from the preview
Don't miss anything!






(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:
To introduce Decision making statements in C++, If, If - else, & switch statement
(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6
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
#include <iostream.h>
Test Expression
Body of if
Exit
False
True
Body of else
(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.
(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;
(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6
#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(); }
(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 6
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”
LAB SUBMISSION DUE 7 TH^ DECEMBER 2017
******************* THE END *****************