0% found this document useful (0 votes)
27 views

Conditional Structure

The document discusses different conditional structures in C++ including if, if-else, if-else-if, nested if, logical operators, and switch structures. It provides syntax examples and sample programs to demonstrate how to use these conditional structures to make decisions based on conditions. The sample programs include finding even/odd numbers, grades based on test scores, maximum of three numbers, and more.

Uploaded by

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

Conditional Structure

The document discusses different conditional structures in C++ including if, if-else, if-else-if, nested if, logical operators, and switch structures. It provides syntax examples and sample programs to demonstrate how to use these conditional structures to make decisions based on conditions. The sample programs include finding even/odd numbers, grades based on test scores, maximum of three numbers, and more.

Uploaded by

MOON AWAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Conditional Structure

• A statement or set of statements that is executed when a particular


condition is true and ignored when the condition is false is called
conditional statement.
• Example:Suppose a program displays pass if the student gets 40 or
more than 40 marks.It displays fail when the marks are below 40. The
program checks the marks before displaying the message. This process
is known as decision-making or selection.
Relational operators
The relational operators are used to specify conditions in programs.A
relational operator compares two values.It produces result as true or
false.The relational operators are sometimes called the conditional
operators or comparison operators.
Relational expression
A relational expression is a statement that uses relational operators to
compare two values.The result of relational expression can be true or
false.Both sides of a relational expression can be constant , variable or
expression.
Examples:
Relational expression Result
53 > 20 True
0 >= 0 True
0 <=0 True
‘if’ Structure
If is a keyword in C++ language. If statement is a decision-making
statement. It is the simplest form of selection constructs.It is used to
execute or skip a statement or set of statements by checking a condition.
The condition is given as a relational expression.If the condition is
true , the statement or set of statements after if statement is executed.If
the condition is false, the statement or set of statements after if
statement is not executed.
Syntax of if statement
The syntax of if statement is as follows:
If(condition)
Statement;
This syntax is used for single statement. A set of statements can also be
made conditional. In this case , these statements are written in curly
brackets { }. The set of statements is also called compound statements.
If(condition)
{
Statement 1;
Continue…..
Statement 2;
:
Statement N;
}
Program on if statement.
Write a program that inputs a number and finds whether the number is
even or odd.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int num;
Cout <<“ Enter a number:”;
Cin>>num;
Continue….
if(num % 2 == 0)
Cout<<“ The number is even.”;
If(num % 2 != 0)
Cout<<“ The number is odd.”;
Getch();
}
Program2
Write a program that inputs three numbers and displays maximum
number.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a, b, c, max;
Cout<<“Enter first number:”;
Cin>>a;
Program2 continue….
Cout<<“Enter second number:”;
Cin>>b;
Cout<<“Enter third number:”;
Cin>>c;
Max = a;
if(b > max)
Max = b;
if(c > max)
Program2 continue…
Max = c;
Cout<<“The maximum number is “<<max;
getch();
}
Program:write a program that inputs two
numbers and finds if both are equal.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a,b;
Clrscr();
Cout<<“Enter a number:”;
Cin>>a;
Program3 continue….
cout<<“Enter 2nd number:”;
cin>>b;
if(a==b)
cout<<“both numbers are equal.”;
getch();
}
‘if-else’ Structure
If else statement is another type of if statement.It executes one block of
statement(s) when the condition is true and the other when it is false.In
any situation, one block is executed and the other is skipped.In if else
statement:
• Both blocks of statement can never be executed.
• Both blocks of statement can never be skipped.
Syntax
If(condition)
Statement;
Continue…
else
statement;
Two or more statements are written in curly brackets {}. The syntax for
compound statements in if else statement is as follows:
if (condition)
{
Statement1;
Statement2;
Continue….
:
Statement N;
}
else
{
Statement1;
Statement2;
Statement N;
}
Program: write a program that inputs a number
and finds whether it is even or odd use if –else
structure.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a;
Clrscr();
Cout<<“Enter a number:”;
Cin>>a;
Continue….
if(a%2==0)
cout<<a<<“ is even.”;
Else
cout<<a<<“ is odd.”;
getch();
}
Multiple ‘if-else-if’ Structure
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
The syntax of this structure is:
If(condition)
{
Block 1;
Continue…
}
Else if(condition)
{
Block 2;
}
Else if (condition)
{
Block 3;
}
Continue….
.
.
.
Else
{
Block N;
}
Program:write a program that inputs test score of
a students and displays his/her grades on
following criteria:
Test score Grade
>=90 A
80 —89 B
70 —79 C
60 —69 D
Below 60 F
Program…
#include <iostream.h>
#include<conio.h>
Void main()
{
Int score;
Clrscr();
Cout<<“Enter your test score:”;
Cin>>score;
If(score>=90)
cout<<“ Your grade is A.”;
else if(score>80)
Continue….
cout<<“Your grade is B.”;
else if(score>=70)
cout<<“Your grade is C.”;
else if(score>=60)
cout<<“Your grade is D.”;
else
cout<<“Your grade is F.”;
getch();
}
Nested ‘if’ Structure

An if statement within an if statement is called nested if statement.In


nested structure ,the control enters into the inner if only when the outer
condition is true.Only one block of statements are executed and the
remaining blocks are skipped automatically.
Syntax
if (condition)
if (condition)
{
statement(s);
Syntax continue…
}
else
{
Statement(s);
}
else
{
Statement(s);
}
Write a program that inputs three numbers and
displays the smallest number by using nested if
condition.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a, b, c;
Cout<<“Enter three number:”;
Cin>>a>>b>>c;
if(a<b)
Program continue…
if (a<c)
cout<<a<<“is smallest number.”;
else
cout<<c<<“is smallest number.”;
Else
if (b<c)
cout<<b<<“is smallest number.”;
else
cout<<c<<“is smallest number.”;
Program continue….
getch();
}
Program:write a program that inputs three
numbers and displays whether all numbers are
equal or not by using nested if condition.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a, b, c;
Cout<<“Enter three number:”;
Cin>>a>>b>>c;
if(a==b)
if(a==c)
Program continue…
cout <<“All numbers are equal.”;
else
cout<<“Numbers are different.”;
else
cout<<Numbers are different.”;
getch();
}
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.
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 (&&)
The symbol used for AND operator is (&&).It is used to evaluate two
conditions.It produces true result if both conditions are true.It produces
false result if any one condition is false.
Condition1 Operator Condition2 Result
False && False False
False && True False
True && False False
True && True True
Program:write a program that inputs three
numbers and displays the maximum number by
using logical operators.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int a, b, c;
Cout<<“Enter three number:”;
Cin>>a>>b>>c;
If(a>b && a>c;
cout<<“Maximum number is”<<a;
Program continue….
else if(b>a && b>c)
cout <<“Maximum number is”<<b;
else
cout<<“Maximum number is”<<c;
getch();
}
OR Operator (||)
The symbol used for OR operator is (||). It is used to evaluate two
conditions.It produces true result if either condition is true.It produces
false result if both conditions are false.
Condition1 Operator Condition2 Result
False || False False
False || True True
True || False True
True || True True
Program:write a program that inputs a character
and displays whether it is a vowel or not.
#include <iostream.h>
#include<conio.h>
Void main()
{
Char ch;
Clrscr();
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’)
Program continue….
cout<<“You entered a vowel:”<<ch;
else
cout<<“You did not enter a vowel:<<ch;
getch();
}
NOT Operator. (!)
The symbol used for NOT operator is (!).It is used to reverse the result
of a condition.It produces true result if the condition is false.It produces
false result if the condition is true.
Operator Condition Result
! True False
! False True
Program: write a program that inputs a number
and displays whether it is even or odd by using
logical operator”!”.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int n;
Cout<<“Enter any number:”;
Cin>>n;
If(!(n%2==0))
cout<<“You entered odd number.”;
Program continue…
else
cout<<“You enter even number.”;
getch();
}
‘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 of’ switch ’ Structure
Switch (expression)
{
case constant 1:
Statement(s);
break;
case constant 2:
Statement(s);
break;
Syntax continue….
case constant 3:
Statement(s);
break;
:
:
:
Default:
Statement(s);
}
Program:write a program that inputs number of
day of the week and displays the name of the day.
If user enters 1,it displays “Friday” and so on.
#include <iostream.h>
#include<conio.h>
Void main()
{
Int n;
Cout<<“Enter number of a weekday:”;
Cin>>n;
Switch(n)
{
Program continue….
case 1:
cout<<“Friday”;
Break;
case 2:
cout<<“Saturday”;
Break;
case 3:
cout<<“Sunday”;
Break;
Continue….
case 4:
cout<<“Monday”;
Break;
case 5:
cout<<“Tuesday”;
Break;
case 6:
cout<<“Wednesday”;
Break;
Continue….
case 7:
cout<<“Thursday”;
Break;
Default:
cout<<“Invalid number”;
}
getch();
}

You might also like