You are on page 1of 29

Lab Manual V

Conditional Structure
Turbo DEV
Control Structure

Sequence Selection Iterative


Function Call
Structure Structure Structure

# # # #
# # # #
void main () void main () void main () void fun (int);
{ { { void main ()
int a,b; int a,b; int a,b; {
a=10; a=10; a=1; int a,b;
b=4; b=4; b=4; a=1;
c=a+b; While (a>5) b=4;
a=b-c; If (a>b) { cout<<a; fun(a);
cout<<c; cout<<c a++} getch();
}
getch(); getch(); getch(); foid fun (int c)
} } } { }
If Structure (Syntax)

Conditional Structure Ternary


Operator

if If else Multiple if else Nested if switch


If Structure (Examples)

Conditional Structure

if If else Multiple if else Nested if switch

# #
#
# # # #
# # void main () void main ( ) { void main () {
void main () { int a,b; int a,b;
# int a,b; a=1; b=4; c=5; m=1;
void main () { a=80;
if (a > b) switch (m) {
int a,b; if (a > 80)
{ cout<<“A”; If (a>c) case 1:
int a,b; a=1; b=4; cout<<“a is large”; cout<<“jan”;
else if (a>70)
break;
a=1; b=0; if (a > b) cout<<“B”; else
Case 2:
if (a > b) cout<<“a is large”; else if (a>60) cout<<“c is large”;
Cout<<“C”; cout<<“feb”;
cout<<“a is large”; else else break;
eles if (a>50)
If(a>c)
cout<<“b is large”; Cout<<“D”;
Cout<<“ b is large”;
------
else default:
getch(); Cout<<:fail”; else cout<<:invalid
} getch(); Cout<<“c is large” number”;
getch();
} }
getch(); } getch();}
If Structure

if ( Condition )

#
#
void main ( )
{
int a,b; #include <iostream>
a=3; b=2; using namespace std ;
int main ( ) {
if (a > b) int a,b;
cout<<“a is large”<<a;
a=3; b=2;
if (a > b)
getch(); cout<<“a is large”<<a;
} return 0; }
If Structure Examples
# # # # #
# # # # #
void main ( ) void main ( ) void main ( ) void main ( ) void main ( )
{ { { { {
int m; m=50; int age; age=20; int a; a=1; int light; light=0; int a,b; a=b=1;

if (m >= 50) if (age >= 18) if (a > 0) if (light= = 0) if (a = = b )

cout<<“Pass”; cout<<“CNIC”; cout<<“+ve”; cout<<“Off”; cout<<“equal”;

getch( ); getch( ); getch( ); getch( ); getch( );


} } } } }

# #
# # # #
#
# # #
void main ( ) void main ( )
void main ( )
void main ( )
void main ( )
{ {
{ { {
int m; m=40; int a,b; a=3; b=1;
int m; m=50; int m; m=50; int m; m=49;
if (m >= 50)
if (m >= 50) if (m >= 50) if (m >= 50) if (a = = b )
{
{
cout<<“Pass”; cout<<“Pass”; cout<<“Pass”;
cout<<“Pass”; cout<<“equal”;
cout<<“cognates”; cout<<“cognates”;
cout<<“cognates”;
} cout<<“Fail”;
} cout<<“not equal”;
getch( ); getch( ); getch( );
getch( ); getch( );
} } }
} }
If then else Structure Examples

If (Condition) Used when options


are only two:

On/Off
{ Statement ; } //True Yes/No

True/False
else Even/Odd

1/0
{ Statement; } //False Pass/Fail
If then else Structure Examples
# # # #
#
# # # #
#
void main () void main () void main () void main ()
void main ()
{ { { {
{
int m; m=50; int age; age=20; int a; a=1; int light; light=1;
int a,b; a=b=1;
If (m >= 50) If (age >= 18) If (age > 0) If (light= = 0)
If (a = = b )
cout<<“Pass”; cout<<“CNIC”; cout<<“+ve”; cout<<“Off”;
cout<<“equal”;
else else else else
else
cout<<“Fail”; cout<<“Form-B”; cout<<“-ve”; cout<<“ON”;
cout<<:not equal”;
getch();
getch(); getch(); getch(); getch();
}
} } } }
#
#
# # void main () {
# #
void main () # int sal, bonus;
# void main ()
{ void main () cin>>sal;
void main () {
int n; n=5; { If (sal > 100000 )
{
bonus=sal*25/100;
int m,n; m=5; n=10; if (condition)
if (n % 2 = = 0) if (condition)
else
if (m > n) { Statements;
{ cout<<“even”; Statements bonus=sal * 10/100;
Cout<<“m large”<<m; Statements; }
Cout<<n; } else
else else
else statements sal=sal + bonus;
Cout<<“n large”<<n; { statements ;
{ cout<<“Odd”; cout<<“sal after
statements; }
Cout<<n; } bonus”<<sal;
getch();
getch();
} getch();
getch(); } getch(); }
}
}
Ternary Operator OR (If then else )

True : False
Ternary Operator
(Condition) ? State : State ;

If (Condition)

{ State ; } //True
If else
else

{ State; } //False
Ternary Operator (If then else ) Example
#
#
void main ()
{
int age; age=20;

(age >= 18) ? cout<<“CNIC” : cout<<“Form-B” ;


getch( ); #
} #
void main ()
{
int age; age=20;

If (age >= 18)

cout<<“CNIC”;
else
cout<<“Form-B”;

getch();
}
If then else /Multiple IF /Ternary Structure Examples

If Then else Multiple If Ternary Operator

# #
# #
void main ( ) #
void main ( )
{ #
{
int m; m=50; void main ( )
int m; m=50;
{
if (m >= 50) int m; m=50;
If (m >= 50)
cout<<“Pass”; cout<<“Pass”;
(m >= 50) ? cout<<“Pass” : cout<<“Fail” ;
else If (m < 50)
cout<<“Fail”; getch( ) ;
cout<<“Fail”;

getch(); }
getch();
}
}
If then else /Multiple IF /Ternary Structure Examples

If Then else Multiple If Ternary Operator

# #
# #
void main ( ) #
void main ( )
{ #
{
int m; m=50; void main ( )
int m; m=50;
{
if (m %2= = 0) int m; m=50;
If (m%2= = 0)
cout<<“Even”;
(m %2= = 50) ? cout<<“Even” : cout<<“Odd” ;
cout<<“Even”;
else If (m %2 != 0)
cout<<“Odd”; getch( ) ;
cout<<“Odd”;

getch(); }
getch();
}
}
Multiple If - else Structure Examples

If (Condition)
{ Statement ; }
Used when options
are more than two
else if (Condition)
such as
{ Statement; } Blood group

else if (Condition) Grades

{ Statement; } Week days

Months
else if (Condition) Season
{ Statement; }
Semester

MCQs
else
{statement; }
Multiple If-else Structure Examples
# # #
void main ( ) { void main ( ) { void main ()
int c; int tv,tax; int units; {
cin>>units>>tv>>tax; int sal, bonus;
cin>>c; //45 if (units >= 1000) cin>>sal; //60000
if (c >= 48)
bill=units * 10;
cout<<“A-grade”;
else if (units >= 700) If (sal >= 100000 )
else if (c >= 39)
bill=units * 7; bonus=sal*50/100;
cout<<“B-grade”;
else if (units >= 500)
else if (c >= 30)
bill=units * 5; else if (sal >= 50000)
cout<<“C-grade”;
else if (units >= 300) bonus=sal * 25/100;
else if (c >= 24)
bill=units * 3;
cout<<“D-grade”;
else if (units >= 200) else
else
bill=units * 2; bonus=sal * 10/100;
cout<<“Fail”;
else
getch();
bill=units * 1; sal=sal + bonus;
}
bill=bill+TV+Tax;
cout<<“total-bill”<<bill; cout<<“sal after bonus”<<sal;
getch();} getch(); }
Criteria: Criteria: Criteria:
A-Grade if marks >= 48 Units > 1000 then PU:10 If salary greater equal 100000 then person
B-Grade if marks >= 39 Units > 700 then PU:7 will get 5o% bonus of his salary
C-Grade if marks >= 30 Units > 500 then PU:5
D-Grade if marks >= 24 Units > 300 then PU:3 If salary greater equal 50000 then person will
F-Grade below 24 Units > 200 then PU:2 get 25% bonus of his salary
Unit less 200 then PU:1
Also add TV:25 fee and Below 50000, person will get 10% bonus of
Tax:200 his salary
Multiple If-else Structure Examples (Logical Operator)
#
#
void main ( ) {
void main ( ) {
int c;
int c;
cin>>c; //48 // 33
cin>>c; // 50 // 20
if (c >= 48)
if (c >= 48)
cout<<“A-grade”;
cout<<“A-grade”;
else if (c > = 39 && c < 48)
else if (c >= 39)
cout<<“B-grade”;
cout<<“B-grade”;
else if (c > = 30 && c < 39)
else if (c >= 30)
cout<<“C-grade”;
cout<<“C-grade”;
ese if (c > = 24 && c < 30)
ese if (c >= 24)
cout<<“D-grade”;
cout<<“D-grade”;
else
else
cout<<“Fail”;
cout<<“Fail”;
getch();
getch();
}
}

Criteria: Use of logical operators to make good sense


A-Grade if marks >= 48 of the logic
B-Grade if marks >= 39
C-Grade if marks >= 30
D-Grade if marks >= 24
F-Grade below 24
Switch Structure (alternative of Multiple If-else)
#
# #
void main ( ) {
void main ( ) { void main ( ) {
int wd; int wd; int m;
cin>>wd; //2 cin>>w //5 cin>>m //6 //13 //0
if (wd= =1) switch (w) Switch (m)
cout<<“Mon”; { {
else if (wd= = 2) case 1: case 1:
cout<<“Mon”; cout<<“Jan”;
cout<<“Tues”;
break; break;
else if (wd= = 3) case 2: case 2:
cout<<“Wednes”; cout<<“Tues”; cout<<“Feb”;
ese if (wd= = 4) break; break;
cout<<“Thurs”; case 3: case 3:
ese if (wd= = 5) cout<<“Wed”; cout<<“March”;
break; break;
cout<<“Fri”;
case 4: case 4:
ese if (wd= = 6) cout<<“Thurs”; cout<<“April”;
cout<<“Sat”; break; break;
ese if (wd= = 7) case 5: case 5:
cout<<“Sun”; cout<<“Fri”; cout<<“May”;
else break; break;
case 6: case 6:
cout<<:invalid number”;
cout<<“Sat”; cout<<“June”;
getch();} break; break;
Criteria:
}
If week day is 1 then M case 7: -----
If week day is 2 then Tues cout<<“Sun”; case 12:
If week day is 3 then wed break; cout<<“December”;
If week day is 4 then thu default: break;
If week day is 5 then Fri cout<<“invalid number”; default:
If week day is 6 then Sat } cout<<“invalid number”;
If week day is 7 then Sun getch();}i }
getch();}i
Multiple If-else/Multiple IF / Switch
Multiple If Switch
Multiple If Else
void main ( ) { void main ( ) {
int m; int m;
void main ( ) {
m=1; m=1;
int m;
m=1; Switch (m)
if (m= =1)
if (m= =1) cout<<“Spring”; {
cout<<“Spring”; case 1:
else if (m= = 2) if (m= = 2) cout<<“Spring”;
cout<<“Autumn”; break;
cout<<“Autumn”;
else if (m= = 3) case 2:
if (m= = 3)
cout<<“Winter”; cout<<“Autumn”;
cout<<“Winter”;
ese if (m= = 4) break;
cout<<“Summer”; if (m= = 4)
else cout<<“Summer”; case 3:
cout<<“Winter”;
cout<<:invalid number”;
If(m < 1) break;
cout<<:invalid number”;
getch();} case 4:
Criteria: } If(m > 4) cout<<“Summer”;
If Value is 1 then Spring cout<<:invalid number”; break;

If Value is 2 then Autumn getch(); default:


} cout<<“invalid number”;
If Value is 3 then Winter }

if Value is 4 then Summer getch();}

Otherwise invalid option


Nested If Structure Examples
#
# void main ( ) {
void main ( ) { int a,b,c;
int a,b,c; cin>>a>>b>>c;
if (a>b)
cin>>a>>b>>c; if (a>c)
cout<<“a is large”;
if (a>b && a>c) else
cout<<“ a is large”; cout<<“ c ia large”;
else
else if (b>a && b>c)
cout<<“ b is large”; if (b>c)
cout<<“b is large”;
else else
cout<<“c is large”; cout<<“c is large;
getch();
} getch();
}
Logical Operators Structure Examples
AND Operator
NOT Operator
T AND T: T
NOT ! (T): False
T AND F: F
NOT ! (F): T
#
#
void main ( ) {
void main ( ) {
int a,b,c;
int n;
cin>>a>>b>>c;
cout<<“enter value of n=“;
if (a>b && a>c)
cin>>n //4 //3
cout<<“ a is large”;
If (!(n%2= = 0)
else if (b>a && b>c)
cout<<“odd”;
cout<<“ b is large”;
else
else
cout<<“even”;
cout<<“c is large”;
getch();
getch();
}
}

#
#
void main ( ) {
char h;
OR Operator
||
cin >>h // k //e //a

If (h= =‘a’ || h = = ‘i’ || h = = ‘o’|| h = = ‘u’ || h= = ‘e’)


cout<<“ character is vowel”;
else
cout<<“not vowel”;
ASCII Code
#include<iostream>
#using namespace std;
int main ( ) {
char ch;
ch = ‘a’;
int code = ch; //ASCII value
cout<<code;
return 0;
}

#include<iostream>
#using namespace std;
int main ( ) {
char ch;
ch = ‘C’;
int code = ch; //ASCII value
cout<<code;
return 0;
}
ASCII Code
#include<iostream>
#using namespace std;
int main ( ) {
char ch1, ch2;
ch1 ='a';
ch2='C';
int sum;
sum = ch1+ch2;
cout<<sum;
return 0; }

#include<iostream>
#using namespace std;
int main ( ) {
char ch1, ch2;
ch1 ='a';
int sum;
sum = ch1+1;
cout<<sum;
return 0; }
Develop given below programs
Using Chapter 3 & 5 concept
Class Responsibility:

Practice and Develop


Different programs using

conditional structure and


run
using
Turbo/DevC++

You might also like