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

Programming_Week5

The document covers various types of loops in programming, including counter-controlled loops, sentinel-controlled loops, and nested loops, along with their structures and examples. It also discusses the use of functions, including function definitions, declarations, and calls, as well as passing values to functions. Additionally, it provides examples of user-defined functions for calculating averages and determining even or odd numbers.

Uploaded by

ayeshanoor777325
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)
1 views

Programming_Week5

The document covers various types of loops in programming, including counter-controlled loops, sentinel-controlled loops, and nested loops, along with their structures and examples. It also discusses the use of functions, including function definitions, declarations, and calls, as well as passing values to functions. Additionally, it provides examples of user-defined functions for calculating averages and determining even or odd numbers.

Uploaded by

ayeshanoor777325
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/ 37

Last Week….

 Counter Controlled Loops


 while Loop
 Increment (++) & Decrement
Operator (--)
 for Loop
 Controlling Loops
 break
 continue
do while
Loop
do-while Loop

 do-while loop is 1…Many


loop
The structure of do-while loop
can be defined as:
 do{
 statement;
 statement;
 } while(condition);
Example 1
 using namespace std;
 #include<iostream>
 int main(){
 int x=0;
 do{
 cout<<" "<<x;
 x++;
 }while(x<5);
 return 0;
}
Counter Controlled
Loops(Essentials)
• #include<iostream>
A variable • using namespace std;
(Counter) • int main(){
 Start • int x=0;
Value
 End
• do{
Condition
• cout<<" "<<x;
 Sequence
• x++;
• }while(x<5);
• return 0;
• }
 #include<iostream>
 using namespace std;
 int main(){
 int add=0, marks;
Counter Controlled Loops
Ask 10 students to enter marks and return 
average
for (int i=0; i<10; i++)
{
 cout<< “Enter your
Marks”;
 cin>>marks;
 add=add+marks;
 }
 cout<< add/10.0;
 return 0;
Sentinel
Controlled
Loops
Sentinel Controlled Loops

 Example: Ask user to enter


marks as many he want to
enter (end with -1 variable)
and calculate average
 Inputssuch as 95, 96, 75, 74,
89 and –1.
 -1here is known as sentinel
value OR Flag Value
Sentinel Controlled Loops
 int main(){
 int add=0, i=0, marks;
 while(true){
 cout<< "\n Enter your Marks (-1 to quit)-
>";
 cin>>marks;
 if (marks==-1) break;
 i++;
 add=add+marks;
 }
 float
av=static_cast<float>(add)/i;
 cout<<"\nTotal Students="<<i;
Sentinel Controlled Loops
• int main(){
• int add=0, i=0, marks;
• while(mark!=-1){
• cout<< "\n Enter your Marks (-1
to quit)->";
• cin>>marks;
• i++;
• add=add+marks;
• }
• float
av=static_cast<float>(add+1
)/i-1;
• cout<<"\nTotal
Students="<<i-1;
• cout<<"\n\nAverage
Marks="<<av;
Sentinel Controlled Loops
 #include<iostream>
 using namespace std;
 int main(){
 char x;
 while(true){
 cout<<"\nPlease Enter a
• Force the user to
enter a required capital Letter->";
 cin>>x;
input. For
 if(x>='A'&&x<='Z')
example enter
 break;
capital letter
 else{
 cout<<"\nThis is not
correct Input..";
 cout<<"\nEnter once
more";
 }
 }
Output
Enforcing to enter
required input
 #include <iostream>
 using namespace std;
 int main(){
 char x;
 cout<<"\nPlease Enter a capital Letter->";
 cin>>x;
 while(!(x>='A'&&x<='Z')){
 cout<<"\nNot Capital...Please Enter a capital
Letter->";
 cin>>x;
 }
 return 0;
Nested
Loops
Nested Loops
 We can use loops inside
loop body
 Lotof care is needed in this
type of structure
 First inner loop complete its
iteration and then control
shift to outer one, this
process continues till end
 So may problem solved with
nested e.g. Sorting(2
level ) , Matrix Multiplication
Example
 using namespace std;
 #include<iostream>
 int main(){
 for(int i=0;i<5;i++){ Upper loop

 for(int j=0;j<5;j++){ Inner loop

 cout<<"*";
 }
 cout<<"\n";
 }
 return 0;
On each iteration of upper loop inner loop
} complete its iterations from start to end
Example
using namespace std;
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=i;j++){
cout<<"*";}
cout<<endl;
}
return 0;}
Example
using namespace std;
#include<iostream>
int main(){
for(int i=10;i>=0;i--){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
Exampleusing namespace std;
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=10-i;j++){
cout<<" ";
}
for(int j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
Code
Arrangement
Structured Approach
Styles
 Action oriented approach
 Consistof blocks(function)+
Structures
Object Oriented Approach
 Consist of classes
 Class
is type object( action+
properties)
Using Function
 Three important aspects for
using functions:
1. Function Definition(code
of function):
It is just body of function which
is mostly written below the
main function
2. Function declaration
3. Function call
User-defined Functions
(Definition)
return-value-type function-
name(parameter-list)
{
declarations and statements
}
 Return-Type: No(void) return or type
 Function-name: Any(identifier rules)
 Parameter: No(void) or no. of
parameters with types
 Body of the function
Simple Function
(Function Definition)

No Return Name No Parameter

void line( ){
for(int i=0;i<45;i++)
cout<<“*”;
cout<<endl;
}
Function
declaration
 Function declaration is given if
the function definition written
below the main function
 Function declaration consists
of:
 void line();
Function call
 A function is executed only when it
is called
 Mostly a function is called inside
the main (or in other function)
 Code written inside function will
never execute if it is not called
 line();
 #include <iostream>
 void line( ); \\ Function Declaration
 using namespace std;
 int main ( ) {
Using Function
 line( ); \\ Function Call
 cout<<"Data Type Memory Reserved\n";
 line(); \\ Function Call
 cout<<"bool 1 Bit"<<endl
 <<"char 1 Byte"<<endl
 <<"int 4 Byts"<<endl;
 line( ); \\ Function Call
 return 0;
 }
 void line( ){
 for(int i=0;i<40;i++)
 Function
cout<<"*"
 Definition
cout<<endl;
 }
Example
• void line(){
 #include<iostream> • for(int i=0;i<45;i++)
 void line(); • cout<<"*";
 void hash(); • cout<<endl;
 void dash(); •}
 int main(){ • void hash(){
 dash(); • for(int i=0;i<45;i++)
 for(int i=0;i<5;i++) • cout<<"#";
 line();
• cout<<endl;
• }
 dash();
• void dash(){
 for(int i=0;i<5;i++)
• for(int i=0;i<45;i++)
 hash();
• cout<<"-";
 return 0; • cout<<endl;
} •}
Example
Write a function which input 5 integers and
calculate•average
#include <iostream>
• void average();
• using namespace std;
• int main() {
• average();
• return 0;
•}
• void average(){
• int a,b,c,d,e;
• cout<<"\n Enter Five
Numbers:";
• cin>>a>>b>>c>>d>>e;

Example Write a function which input an integer number
and tells whether it is even or odd (using switch)

• #include <iostream>
• void evenOdd();
• using namespace std;
• int main() {
• evenOdd();
• return 0;
•}
• void evenOdd(){
• int number;
• cout<<"\n Enter a Number:";
• cin>>number;
• switch(number%2){
• case 0: cout<<"\nNumber is even";
break;
• case 1: cout<<"\n Number is odd";

Passing Values To
functions
During function call we send
values which a function can
accept
 We can pass any type of
value to the function
 We have to define the data
type of the values in function
definition
Passing Values to a Function
 void add(int a, int b); OR void add (int, int);
 int main(){
 add (7,8);
 add (9,10);
 int x=5, y=10;
 add (x,y);
 return 0;
 }
• void add( int a, int b){
• int add;
• add= a+b;
• cout<<“ Addition is:
“<<add<<endl;
Problem

Write a function which


takes three integers as
parameters and display the
smallest integer
 #include<iostream>
 using namespace std; Example
 void s(int,int,int );
 int main(){
 s(123,135,140);
 int a,b,c;
 cin>>a>>b>>c;
 s(a,b,c);
 return 0;
}
 void s(int a,int b,int c){
 int small=a;
 if(b<small)
 small=b;
 if(c<small)
 small=c;
 cout<<"Samll
Functions
 #include<iostream>
 #include<iomanip>
 using namespace std;
 int main(){
 cout<<“Good
Luck”<<setw(7)<<“For
Your”<<setw(10)<<“Future”;
 return 0
}

You might also like