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

Lecture#07 (1)

Uploaded by

abdullahsnap1919
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)
25 views

Lecture#07 (1)

Uploaded by

abdullahsnap1919
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/ 40

Programming Fundamentals

Cs-111
(4 CrH = 3 Theory 1 Lab)
Lecture No. 07

Department of Computational Sciences


The University of Faisalabad (TUF)
Home-work
Home-work
Nested if Statement (The if-else Nested within the if Part)

if (expression-1)
statement1;
else
if (expression-2)
statement2;
else
statement3;
Chain if Statement (The if-else Nested within the else Part)

if (expression-1)
statement1;
else if (expression-2)
statement2;
else
statement3;
Selection Statements
Consider the following statements:
• if today is Thursday I have test today
• If you will run fast, u will catch the bus
• if x is dividable by 2 then x is even number
if Statement
(Single Selection)
• If statement is used in c++ for selection purposes
• Structure of if statement is:
True(1) OR False(0)

if(condition)
statement;
Example
• int main(){
• int num1,num2;
• cout<<"Enter two integers-->:";
• cin>>num1>>num2;
• if(num1==num2)
• cout<<num1<<" is equal to "<<num2<<endl;
• if(num1!=num2)
• cout<<num1<<" is not equal to "<<num2 <<endl;
• if(num1<num2)
• cout<<num1<<" is less than "<<num2 <<endl;
• if(num1>num2)
• cout<<num1<<" is greater "<<num2 <<endl;
• return 0;
• }
int main(){
int num1,num2;
cout<<"Enter two integers-->:";
cin>>num1>>num2;
if(num1==num2)
cout<<num1<<" is equal to
"<<num2<<endl;
if(num1!=num2)
cout<<num1<<" is not equal to "<<num2
<<endl;
if(num1<num2)
cout<<num1<<" is less than "<<num2
<<endl;
if(num1>num2)
cout<<num1<<" is greater "<<num2
<<endl;
return 0;
int main(){
int num1,num2;
cout<<"Enter two integers-->:";
cin>>num1>>num2;
if(num1==num2)
cout<<num1<<" is equal to
"<<num2<<endl;
if(num1!=num2)
cout<<num1<<" is not equal to "<<num2
<<endl;
if(num1<num2)
cout<<num1<<" is less than "<<num2
<<endl;
if(num1>num2)
cout<<num1<<" is greater "<<num2
<<endl;
return 0;
Problem

Prompt user to enter a value. Check if the number


is even then display a message that number is
even. If number if odd then display message
number is odd
int main( ) Example
{
int num;
cout<<"Enter a number --> ";
cin>>num;
if(num%2 == 0)
cout<<endl<<num<<": is an even number";
if(num%2 != 0)
cout<<endl<<num<<": is an odd number";
getch();
return 0;
}
Conditional Statements
(Double Selection)
• if today is Thursday I have test today else
we have match today
• If you will run fast, you will catch the bus
else you have to take a cab
• if x is dividable by 2 then x is even number
else x is odd
if-else Statement
• Used for double selection
• Syntax of if-else if as:
if(condition)
statement;
else
statement;
Example
#include<iostream>
using namespace std;
int main( )
{
int num;
cout<<"Enter your grade --> ";
cin>>num;
if(num>= 50)
cout<<endl<<"Congraulations u r Passed \n";
else
cout<<"You are failed";
return 0;
}
Problem

Ask user to enter two numbers. Display


which number is greater between two
Example
#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter first num --> ";
cin>>num1;
cout<<"Enter second num --> ";
cin>>num2;
if(num1 > num2)
cout<<endl<<num1<<": is greater \n";
else if (num2 > num1)
cout<<num2<<": is greater";
else
cout<<": both are equal";
return 0;
}
Using Character in Conditional
Statements
• Ask the user to enter a character among (A, B, OR C)
• Display following output messages:
• If he entered A : Display message “A FOR Apple”
• If he entered B : Display message “B FOR Balloon”
• If he entered C : Display message “A FOR Cartoon”
Example
char a;
cout<<"Enter a Character (A, B, Or C)";
cin>>a;
if (a== 'A’)
cout<<"\n A FOR Apple ";
if (a== 'B’)
cout<<"\n B FOR Balloon ";
if (a== 'C’)
cout<<"\n A FOR Cartoon ";
Code Flow
• In any programming language the flow of the
code can be of four types
1. Sequential
2. Selection
3. Repetition
4. Go to  Obsolete Style
Code Flow
1. Sequential
2. Selection
- Single Selection(if)
- Double Selection (if-else, Ternary Operator )
-Multiple Selection (else-if, switch)
3. Repetition
- while Loop
- do-while Loop
-for Loop
Multiple Selection
(else-if)
• The structure of else if can be defined as:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
……
else
statement;
#include<iostream> Example
using namespace std;
int main(){
int x;
cout<<"Please enter a number between 1 to three:";
cin>>x;
if(x==1)
cout<<"\n You will go to Heaven";
else if(x==2)
cout<<"\n You will go to Hell";
else if(x==3)
cout<<"\n You will remain on earth";
else
cout<<"You have enter some thing other than 1,2 ,3";
return 0;
switch(n){ Multiple Selection
case 1: (switch)
statement;
statement;
break;
case 2:
statement;
statement;
break;
….
default:
statement;
statement;
int main(){
Switch
int main(){
int x; int x;
cout<<"Please enter a number between 1 to cout<<"Please enter a number
three:"; between 1 to three:";
cin>>x; cin>>x;
switch (x){ if(x==1)
case 1: cout<<"\n You will go to Heaven";
cout<<"\n You will go to Heaven"; else if(x==2)
break; cout<<"\n You will go to Hell";
case 2: else if(x==3)
cout<<"\n You will go to Hell"; cout<<"\n You will remain on earth";
break; else
case 3: cout<<"You have enter some
cout<<"\n You will remain on earth"; thing other than 1,2 ,3";
break; return 0;
default: }
cout<<"You have enter some thing ";
}
return 0;
}
Two Number Calculator
• Ask the user to enter two number
• Then ask to enter the operator(+,-,\,*,%)
• Display the result
Logical Operators
• These operators are used to combine
multiple conditions
• Three logical operators are:
And  &&
Or  ||
Not  !
Problem
A bank gives loan in two situations
1. If customer is male and his age is more than
25 and his salary is more than 25000
2. If customer is a female and her age is more
than 30 and her income is 20000
Program: Ask user to enter age,gender(m\f) and
income.Check whether he\she is eligible to
get loan
Problem
int main( )
{
int age,income;
char gender;
cout<<"Please enter ur Age :";
cin>>age;
cout<<"\nPlease enter ur Income :";
cin>>income;
cout<<"\nPlease enter ur gender(m for male and f for
female) :";
cin>>gender;
Problem
if(age>25 && gender=='m' && income>25000)
cout<<"\n\n\n Sir,You can apply for Loan";
else if(age>30 && gender=='f'&& income>30000)
cout<<"\n\n\n Madam,You can apply for Loan!!!";
else
cout<<"\n\n\n You are not eligible for loan!!!";
return 0;
}
Ternary Operator(?:)
• Ternary operator exactly work as if-else
statement do
• Structure of ternary operator is:
• (condition ? statement : statement)
Operator Precedence &
Associativity

You might also like