Chapter # 5: Conditional Structures
Chapter # 5: Conditional Structures
Conditional Structures
Chapter Outline
Control Structure
Relational Operators
‘if’ Structure
‘if-else’ Structure
Multiple ‘if-else-if’ Structure
Nested ‘if’ Structure
Compound Condition
‘switch’ Structure
Conditional Operator
‘goto’ statement
Control Structure
> Greater than operator returns if the value on left hand side is greater than the value on the right-hand side. Otherwise
return false.
< Less than operator returns true if the value of left-hand side is less than the value on right hand side. Otherwise return
false.
== Equal to operator return true if the values on both hand side are equal. Otherwise return false
>= Greater than operator returns if the value on left hand side is greater than or equal the value on the right-hand side.
Otherwise return false.
<= Less than operator returns true if the value of left-hand side is less than or equal to the value on right hand side.
Otherwise return false.
!= The not equal to operator return true if the values on both hand side are not equal. Otherwise return false
Relational Expression
A relational expression is a statement that use Relational Results
relational operators to compare two values. Expression
The result of relational expression can be true
or false.
100 > 15 True
Both sides of relational operators can be
constant, variable or arithmetic expression.
Example:
25 > 5 False
Some examples of relational expression are;
30 <= 12 False
‘if’ Statement
If statement is a decision making statement.
It execute or skip a statement based on a condition.
The statement is executed only if a condition is true.
Otherwise the statement is not executed.
Syntax
if(condition)
statement;
else
statement;
‘if’ statement Program
#include <iostream>
using namespace std;
int main () {
int num = 10;
if (num % 2 == 0)
{
cout<<"It is even number";
}
return 0;
}
‘if-else’ condition
‘if-else’ statement also check a condition.
It executes if block if condition is true
otherwise else block is executed.
Both blocks can never be executed.
Both blocks can never be skipped.
Syntax
if(condition)
statement;
else
statement;
‘if-else’ Example
#include <iostream.h>
#include <conio.h>
main()
{
int n;
cout<<"Enter a number \n";
cin>>n;
if( n % 2 == 0)
cout<<"The number is even\n";
else
cout<<"The number is \a odd\n";
getche();
}
Multiple ‘if-else-if’
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.
Syntax
if(condition)
{
Block 1; }
else if (condition)
{
Block 2; }
.
.
else
{
Block N; }
#include <iostream.h>
#include <conio.h>
main() {
int marks;
cout<<"Enter your test score \n";
cin>>marks;
if( marks >= 90)
‘if-else-if
cout<<"Your grade is = A \n";
else if ( marks >=80)
#include <iostream.h>
#include <conio.h>
main()
{
int a, b,c;
cout<<"Enter three numbers\n";
cin>>a>>b>>c;
if( a < b)
if(a < c)
cout<<"The smallest number is = "<<a<<endl;
if(b < a)
if(b < c)
cout<<"The smallest number is = "<<b<<endl;
if( c < a)
if( c < b)
cout<<"The smallest number is = "<<c<<endl;
getche();
}
Compound Condition
A type of condition 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.
Example
For example a program inputs two numbers. It displays OK if one number is greater
than 100 and second is less than 100. Compound condition is executed by using
logical operators.
Logical Operators
#include <conio.h>
#include <stdlib.h>
main() {
cout<<"1. Convert ASCII value to character\n“;cout<<"2. Convert Character to ASCII value\n";
switch(op) {
case 1:
cin>>number;
break;
case 2:
cin>>ch;
cout<<int(ch)<<endl;
break;
default:
getche(); }
Conditional operator is a decision-making structure.
It can be used in place of simple if-else structure.
Conditional It is also called ternary operator as it uses three operands.
Operators Syntax
(condition) ? true case statement : false case statement ;
#include <iostream.h>
#include <conio.h>
main()
Conditional {
Operator int marks;
cout<<"Enter your marks\n";
Program cin>>marks;
cout<<"Result = "<<(marks > 40 ? "Pass": "fail");
getche();
}
‘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 created with a valid identifier followed by a colon(:).
Syntax
goto label;
‘goto’ statement program
#include <iostream.h>
#include <conio.h>
main()
{
int n;
n = 1;
loop:
cout<<"c++\n";
n++;
if(n <= 5)
goto loop;
cout<<"Program is ended\n";
getche();
}