Professional Documents
Culture Documents
Conditional Structure
Turbo DEV
Control 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
# #
#
# # # #
# # 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;
# #
# # # #
#
# # #
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
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;
cout<<“CNIC”;
else
cout<<“Form-B”;
getch();
}
If then else /Multiple IF /Ternary Structure Examples
# #
# #
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
# #
# #
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
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();
}
}
#
#
void main ( ) {
char h;
OR Operator
||
cin >>h // k //e //a
#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: