0% found this document useful (0 votes)
159 views31 pages

Chapter # 5: Conditional Structures

This document chapter discusses conditional structures in programming. It covers control structures like sequence, selection, and repetition. It also discusses relational operators, if, if-else, nested if, and logical operators. Examples are provided to demonstrate how conditional statements work.

Uploaded by

Shoaib Kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views31 pages

Chapter # 5: Conditional Structures

This document chapter discusses conditional structures in programming. It covers control structures like sequence, selection, and repetition. It also discusses relational operators, if, if-else, nested if, and logical operators. Examples are provided to demonstrate how conditional statements work.

Uploaded by

Shoaib Kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter # 5

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

Statement used to control the flow of execution in a program


Instructions are organized in three ways to control the execution flow
Control Structures are used to implement program logic
Types
 The different types of control structure;
 Sequence
 Selection
 Repetition
 Function call
Sequence
Statements are executed in the same order in which
they are specified.
The control flow from one statement to other in logic
sequence.
All statements are executed once
Example
 Suppose the program inputs two numbers and display
average of screen. The program uses two statements to input
numbers. One statement is used to calculate average and one
statement is used to display average. These statements are
executed in sequence to find the average number. All
statements are executed once when the program is executed.
Selection
Select a statement or set of statements to execute
based on condition.
Statement is executed when a particular condition is
true and ignore when the condition is false.
Different types of selection structure are if, if-else, and
switch.
Example
 Suppose a program inputs the marks of student and display a
message whether a student is pass or fail.
Repetition
Execute a statement or set of statement repeatedly.
Also called iteration structure or loop.
Different types of repetition are while, do while and
for.
Example
 Suppose the user want to display “I love Pakistan” on screen
for 100 times.
Function Call
Statement that moves the control to another block of code.
The control returns back after executing all statements in block.
The remaining statements are executed immediately after function call when control is
returned.
Relational Operators
• The relational operators are used to specify the conditions in programs.
Operators Description

> 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)

Program       cout<<"Your grade is = B \n";


      else if ( marks >=70)
      cout<<"Your grade is = C \n";
      else if ( marks >=60)
      cout<<"Your grade is = D \n";
      else
      cout<<"Your grade is = F \n";
      getche(); }
Nested ‘if’ Structure
A nested if is an if statement that is the target of
another if statement. Nested if statements means
an if statement inside another if statement.
Syntax
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}
Nested if Example

#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

Logical operators are used to evaluate compound conditions.


There are three logical operators in C++ language.
  AND operator
  OR operator
  NOT operator
And Operator Condition 1 Operator Condition 2 Result

 The symbol for AND operator is (&&).


False && False False
 It is used to evaluate two conditions.
 It produces true result if both conditions are true. False && True False
 It produces false result if any one condition is false.
 Example
True && False False
 Suppose we have two variables A = 100 and B
= 50. The compound condition (A> 10) && True && True True
(B>10) is true. It contains two conditions, and
both are true. So the whole compound
condition is also true.
#include <iostream.h>
And Operator #include <conio.h>
Example main()
{
      int a, b, c;
      cout<<"Enter three numbers\n";
      cin>>a>>b>>c;
      if(( a > b) && ( a > c))
      cout<<a<<" is the maximum number\n";
      else if (( b > a ) && (b > c))
      cout<<b<<" is the maximum number\n";
      else
      cout<<c<<" is the maximum number \n";
      getche();
}
Or Operator
Condition 1 Operator Condition 2 Result
The symbol used for OR operator is (||).
 It is used to evaluate two conditions.
It produces true result if either condition is true. False || False False
It produces false result if both conditions are
false. False || True True

Example True || False True


 Suppose we have two variables A = 100 and B = 50.
The compound condition ( A > 50 ) || ( B > 50 ) is true. True || True True
It contains two conditions, and one condition ( A > 50 )
is true. So, the whole compound condition is true.
#include <iostream.h>
Or Operator #include <conio.h>
Program main()
{
      char ch;
      cout<<"Enter a character \n";
      cin>>ch;
      if(( ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch=='I')
      ||(ch=='o')||(ch=='O')||(ch=='u')||(ch=='U'))
      cout<<"Entered character is a vowel\n";
      else
      cout<<"Entered character is not a vowel\n";
      getche();
}
Not Operator
The symbol for NOT operator is ( ! ). It is Operator Condition Result
used to reverse the result of a condition. It
produces true result if the condition is false. ! True False
It produces false result if the condition is
true.
! False True
Example
Suppose we have two variables A = 100
and B = 50. The condition ! ( A == B)
is true. The result of ( A == B ) false
but NOT operator converts it into true.
#include <iostream.h>
Not Operator #include <conio.h>
Program main()
{
      int a, b, c;
      cout<<"Enter three numbers\n";
      cin>>a>>b>>c;
      if(( a > b) && ( a > c))
      cout<<a<<" is the maximum number\n";
      else if (( b > a ) && (b > c))
      cout<<b<<" is the maximum number\n";
      else
      cout<<c<<" is the maximum number \n";
      getche();
}
‘switch’ Structure
 The switch statement is another control 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;
                Break;
Case constant 2:
                Statement;
                Break;
Case constant N:
                Statement N;
                Break;
Default:
                statement;
}
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

main() {

     int number, op; char ch;

      cout<<"1. Convert ASCII value to character\n“;cout<<"2. Convert Character to ASCII value\n";

      cout<<"Enter your choice\n"; cin>>op;

      switch(op) {

                case 1:

‘switch’                      cout<<"Enter the ASCII value\n";

                     cin>>number;

Program                      cout<<char(number)<<endl;

                     break;

                case 2:

                     cout<<"Enter the character\n";

                     cin>>ch;

                     cout<<int(ch)<<endl;

                     break;

                default:

                        cout<<"Invalid Choice\n"; }

                        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();
}

You might also like