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

Conditional Structures: MED UET Peshawar

The document discusses different conditional structures in C++ including compound conditions, logical operators, if-else statements, switch statements, ternary operators, and goto statements. It provides examples of programs using each structure to check conditions, compare values, and direct program flow. Key differences between nested if-else and switch statements are also outlined.

Uploaded by

Hasnaat Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Conditional Structures: MED UET Peshawar

The document discusses different conditional structures in C++ including compound conditions, logical operators, if-else statements, switch statements, ternary operators, and goto statements. It provides examples of programs using each structure to check conditions, compare values, and direct program flow. Key differences between nested if-else and switch statements are also outlined.

Uploaded by

Hasnaat Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

CONDITIONAL STRUCTURES

Dr. Fakhre Alam Khan


MED UET Peshawar
Compound Condition
 A type of comparison in which more than one conditions are evaluated is called compound condition.
 It is used to execute a statement or set of statements by testing many conditions.
 Compound condition is executed by using logical operators.

Logical Operators
 Logical operators are used to evaluate compound conditions. There are three logical operators in C++.

a) AND operator(&&) : It produces true result only, if both conditions are true. Otherwise false.

b) OR operator ( || ) : It produces false result only, if both conditions are false. Otherwise true.

c) NOT operator (!) : It is used to reverse the result of a condition. It produces true result if
the condition is false and vice versa.
Write a program that inputs three numbers and displays the maximum number by using logical operators.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a, b , c;
cout<<"Enter three numbers: ";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"Maximum number is "<<a;
else if(b>a && b>c)
cout<<"Maximum number is "<<b;
else
cout<<"Maximum number is "<<c;
getch();
}
Write a program that inputs a character and displays whether it is a vowel or not.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter any character: ";
cin>>ch;
if(ch=='A' || ch=='a' || ch=='E' || ch=='e' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u')
cout<<"You entered a vowel: "<<ch;
else
cout<<"You did not enter a vowel: "<<ch;
getch();
}
Write a program that inputs three digits and displays all possible combinations of these digits.
Solution: else else
#include <iostream.h> { {
#include <conio.h> if((a==b) && (a==c)) cout<<b<<c<<a<<''\t";
void main() cout<<a<<b<<c; cout<<b<<a<<c<<"\t";
{ else cout<<a<<b<<c;
clrscr(); { }
int a,b,c; if(a==b) }
clrscr(); { }
cout<<"Enter three digits:"; cout<<a<<b<<c<<"\t"; }
cin>>a>>b>>c; cout<<a<<c<<b<<"\t"; getch();
if((a !=b) && (b!=c) && (c!=a)) cout<<c<<b<<a; }
{ }
cout<<a<<b<<c<<"\t“; else
cout<<a<<c<<b<<"\t"; {
cout<<b<<a<<c<<"\t"; if(a==c)
cout<<c<<a<<b<<"\t"; {
cout<<c<<b<<a; cout<<a<<c<<b<<"\t";
} cout<<a<<b<<c<<"\t";
cout<<b<<a<<c;
}
‘switch’ Structure
 The switch statement is another conditional structure.
 It is a good alternative of nested if-else.

 It can be used easily when there are many choices available and only one should be executed.

 Nested if becomes very difficult in such situation

Syntax
switch (expression)
{
case constant 1:
statement(s); First case body
break;
case constant 2:
statement(s); Second case body
break;

case constant N:
statement(s); Nth case body
break;
default:
statement(s); Default case body
}
Write a program that inputs a floating point number, an operator and another floating point number. It displays the
result by performing the operation on the given numbers. If the operator is a division, it should check to make sure that
the divisor is not equal to zero. If the operator is not a +, -, *, or / then the program should print an error message.
Solution:
cout<<a+b<<endl;
#include <iostream.h> break;
#include <conio.h> Case'-':
void main() cout<<a-b<<endl;
{ break;
clrscr(); case'*':
float a , b; cout<<a*b<<endl;
char op; break;
cout<<"Enter a floating point number: case '/':
"; cin>>a; if (b == 0)
cout<<"Enter an operator: "; cout<<"Division by zero!"<<endl;
cin>>op; else
cout <<"Enter second floating point number: " ; cout<<a/b<<endl;
cin>>b; break;
switch(op) default:
{ cout<<"lnvalid operator!“<<endl;
case'+': }
getch();
}
Difference between nested 'if-else' and 'switch'

Switch Nested ‘if’


1. It is easy to use when there are multiple choices. 1. It is difficult to use when there are multiple choices.
2. 1t uses single expression for multiple choices. 2. It uses multiple expressions for multiple choices.
3. It cannot check a range of values. 3. It can check a range of values.
4. It checks only constant values. You cannot use 4. It can check variables also. You can use a constant or
variables with case statement. variable in relational expressions.
The condition is specified as relational or logical expression. The condition is
evaluated to true or false.
It is executed if expression evaluates to true .
It is executed if expression evaluates to false.

Conditional Operator
 Conditional operator is a decision-making structure.
 It can be used in place of simple if-else structure.
 It is also called ternary operator as it uses three operands.

Syntax
(condition)? true-case statement: false-case statement;
condition

true-case
false-case
Example:
 Suppose we have a variable A. The following
statement: X = (A>50)? 1 : 0;
will assign l to X if the condition A >5O is true. It will assign 0 to X the condition is false.
The above statement can be written using if-else statement as follows:
if (A>50)
X=1;
else
X =0;
Write a program that inputs a number and displays whether it is divisible by 3 or not by using conditional operator.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n;
cout<<"Enter number:“;
cin>>n;
(n%3==0 ? cout<<"Divisible by 3" : cout<<"Not divisible by 3");
getch();
}
'goto' Statement
 The 'goto' statement is used to move the control directly to a particular location of the program by
using label.
 A label is a name given to a particular line of the program.
 A label is created with a valid identifier followed by a colon ( : ).

Syntax
goto Label;
Write a program that displays “Mechanical Engineer Rocks" five times using goto statement.

Solution:
# include
<iostream.h> #include
<conio.h> void main{)
{
clrscr();
int n=1;
loop:
cout<<n<<": Mechanical Engineer
Rocks"<<endl; n++;
if (n<=5) goto loop;
cout<<"End of
Program"; getch();
}
Assignment 2
(Write down algorithm and make flowchart for all problems)

Q1. Write a program that inputs a value and type of conversion. The program should then display the output
after conversion. The program should include the following conversions:
1 cm = 0.394 inches
1 liter = 0.264 gallons,
1 kilometer = 0.622 miles
1 kilogram = 2.2 pounds
Make sure that the program accepts only valid choices tor the type of conversion.

Q2. Write a program that displays the following output:


Number Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125

Q3. Write a program that inputs marks of three subjects. If the average of marks is more than 80, it displays two messages "You
are above standard!" and "Admission granted!".

Q4. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is also divisible by
400. Write a program that inputs a year such as 1996, 1800 and 2010 etc. It will display ''Leap year" if it is a leap year
otherwise displays "Not a leap year".

You might also like