Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Decision Control Structure (2)

The document outlines the objectives and scope for practicing decision control structures in programming, specifically focusing on if statements, if-else statements, and switch statements. It provides syntax explanations and examples for each type of statement, demonstrating their usage in C++. Additionally, it includes exercises and home tasks for further practice on these concepts.

Uploaded by

f2024266821
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Decision Control Structure (2)

The document outlines the objectives and scope for practicing decision control structures in programming, specifically focusing on if statements, if-else statements, and switch statements. It provides syntax explanations and examples for each type of statement, demonstrating their usage in C++. Additionally, it includes exercises and home tasks for further practice on these concepts.

Uploaded by

f2024266821
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Decision Control Structure

5.1 Objective
The student should practice the following statements:
1. if statements
2. if-else statements
3. switch statement

5.2 Scope
By the end of this lab a student should know:
1. Syntax of if and if-else statements.
2. Syntax of switch statements.

5.3 Useful concepts


If Statement:
The if statement allows your program to execute a single statement or a block of statements
enclosed between braces if a given condition true. The general form for single selection structure is:
if (condition){
statement;

}
Next statement;

Here, the condition is any expression that returns a Boolean value. If there is only a single
statement following the if-clause, the use of braces is optional. However, a compound statement cannot
be used without curly braces.
If-else statements
In case you want to have an alternative action to be taken if the condition is not satisfied, then use
an if-else statement. The general form is:
if (condition)
Statement1;
else
Statement2;
Here, statement may be a single statement or a compound statement enclosed in curly braces (i.e.,
a block). The condition is any expression that results in a Boolean value.
Multiple if-else-if statements
The if-else-if statement can be used to choose one block of statements from many blocks of statements. It
is used when there are many options and only one block of statements should be executed on the basis of
a condition.
Switch statement:
Another useful statement in C++ is the switch statement. This statement is somehow similar to if
statement in giving you multiple options and do actions accordingly. But its behaviour is different. This
statement tests whether an expression matches one of a number of constant integer values labelled as
cases. If one matches the expression, execution starts at that case. This is the general structure of the
switch statement:
switch (expression)
{
case constant: statement(s);
break;
case constant: statement(s);
break;
case constant: statement(s);
break;
default: statement(s);
}
The default clause is optional. If it is not there and none of the cases matches, no action is taken. The
break keyword is used to skip the switch statement. For example if a case matches the expression and no
break key words are used, the execution will go for all the statements in all cases.

5.4 Examples:

Example : Demonstration of if-else statement.

#include<iostream>
using namespace std;
int main()
{
int marks,total,percent=0;
cout<<"Enter the marks obtained in PF:";
cin>>marks;
cout<<"Enter the total marks:";
cin>>total;
percent = (marks*100)/total;
if (percent >= 50)
{
cout<<"Congragulations you have passed the subject."<<endl;
cout<<"WELL DONE!"<<endl;
}
else
{
cout<<"Sorry you could not pass the course."<<endl;
cout<<"Better luck next time. "<<endl;
}
cout<<"THANK YOU."<<endl;

return 0;
}

The output for the above program is shown below

Example: Input a number from user and display whether it is a positive or negative number
#include<iostream>
using namespace std;
int main()
{
int input;
cout<<"Enter a number:";
cin>>input;
if (input>0)
cout<<"A positive number was entered."<<endl;
cout<<"Thank You."<<endl;
return 0;
}
Output for the program might be one of the following:
Example 5.5: Input five values from the user and display the maximum number from the list.

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"enter 1st number: ";
cin>>a;
cout<<"enter 2nd number: ";
cin>>b;
cout<<"enter 3rd number: ";
cin>>c;
cout<<"enter 4th number: ";
cin>>d;
cout<<"enter 5th number: ";
cin>>e;
if(a>b && a>c && a>d && a>e)
cout<<a<<" is the greatest"<<endl;
else if(b>a && b>c && b>d && b>e)
cout<<b<<" is the greatest"<<endl;
else if(c>a && c>b && c>d && c>e)
cout<<c<<" is the greatest"<<endl;
else if(d>a && d>b && d>c && d>e)
cout<<d<<" is the greatest"<<endl;
else if(e>a && e>b && e>c && e>d)
cout<<e<<" is the greatest"<<endl;

return 0;
}
The output for this program is:
Example 5.6: Prompt the user to enter the salary and grade of an employee. If the employee has a grade
greater than 15 then add 50% bonus to the employee’s salary. Otherwise if the employee’s grade is less
than 15 then add 25% bonus to the employee’s salary.

#include<iostream>
using namespace std;
int main()
{
int grad;
double sal,bonus;
cout<<"Enter salary :";
cin>>sal;
cout<<"Enter grade :";
cin>>grad;
if (grad > 15)
{
bonus = sal*(50.0/100.0);
cout<<"Total salary with 50 percent bonus:"
<<(bonus+sal)<<endl;
}
else if (grad <= 15)
{
bonus = sal* (25.0/100.0);
cout<<"Total salary with 25 percent bonus:"
<<(bonus+sal)<<endl;
}

return 0;
}

The output of this program is

Here each condition is tested one by one starting from the first one. If one of them is true, then the
statement associated with that condition is executed and the rest are ignored.
Example 5.7: Demonstration of switch statement.

#include<iostream>
using namespace std;
int main() {
char grade;
cout<<"Enter a grade in capital letters: ";
cin>>grade;
switch(grade)
{
case 'A':
cout<<"You have scored 90% marks."<<endl;
break;
case 'B':
cout<<"You have scored 80% marks."<<endl;
break;
case 'C':
cout<<"You have scored 70% marks."<<endl;
break;
case 'D':
cout<<"You have scored 60% marks."<<endl;
break;
case 'E':
cout<<"You have scored less than 50%."<<endl;

case 'F': break;

cout<<"You have entered an invalid grade."<<endl;


default:
break;

}
return 0;
}

Output for this


program is shown
below:
5.5 Exercise for Lab

Exercise 5.1: Prompt the user to input 5 values and display the minimum number amongst them.
Exercise 5.2: Input five values through the user and display the number of positives, the number of
negatives and the number of zeros amongst the 5 values.
Exercise 5.3: Prompt the user to input a character and display whether it is a vowel or consonant using
switch statement.

5.6 Home Task


1. Ask the user to enter marks obtained in a course and the total marks of the course. Then display a
menu

Press 1 to calculate percentage.


Press 2 to display grade.
If the user presses 1 then percentage should be displayed and if the user presses 2 the grade
against the marks should be displayed. (Hint: use switch statement for menu selection and else if
to display the grade).
2. Prompt the user to enter 3 values. For any equal values, the program should display the
numbers that are equal. (For example user input 34,6,34 the program should display the
message that the 1st and 3rd values are equal).

You might also like