#include int main(){ int base,power,result=1; cout "; cin>>base; cout ";...

Preview:

Citation preview

• #include<iostream>• #include<conio>• int main(){• int base,power,result=1;• cout<<"Plz enter base --> ";• cin>>base;• cout<<"Plz enter power --> ";• cin>>power;• for(int i=0;i<power;i++)• result=result*base;• cout<<"\nThe result is: "<<result;• getch();• return 0;• }

do-while Loopdo-while Loop• The structure of do-while loop can be

defined as:• do{• statement;• statement;• } while(condition);

Nested LoopsNested Loops• We can use loops inside loop body• Lot of 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

• #include<conio>• #include<iostream>• int main(){• for(int i=0;i<5;i++){• for(int j=0;j<5;j++){• cout<<"*";• }• cout<<"\n";• }• getch();• return 0;• }

#include<conio>

#include<iostream>

int main(){

for(int i=1;i<=10;i++){

for(int j=1;j<=i;j++)

cout<<"*";

cout<<endl;

}

getch();

return 0;}

#include<conio>

#include<iostream>

int main(){

for(int i=10;i>=0;i--){

for(int j=1;j<=i;j++)

cout<<"*";

cout<<endl;

}

getch();

return 0;

}

ReviewReview• Input and Output• Variable(type,space,range)• Operators

Arithmetic,Relational,Logical,Ternary,Unary operators

• Selective Flow Single,Double,Multiple

• Iterative Flow while loop, for loop, do-while loop

FunctionsFunctions• #include<iostream>• #include<iostream>• int main(){• clrscr();• cout<<“Good Luck”;• getch();• return 0• }

Program Writing StyleProgram Writing StyleStructured Approach• Action oriented approach• Consist of blocks(function)Object Oriented Approach• Consist of classes• Class is an type

object(action+qualities)

User-defined FunctionsUser-defined Functions 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

Simple FunctionSimple Function void line(){ for(int i=0;i<45;i++) cout<<“*”; cout<<endl; }

No Return Name No Parameter

Using FunctionUsing Function• Three important thing should be in

min when we use functions in main function

1. Function Definition: It is just body of function which is

below the main2. Function declaration3. Function call

Using FunctionUsing Function

Function Definition

Function Call

Function Declaration

• #include<conio>• #include<iostream>• void line();• void hash();• void dash();• int main(){• dash();• for(int i=0;i<5;i++)• line();• dash();• for(int i=0;i<5;i++)• hash();• getch();• return 0;• }

• void line(){• for(int i=0;i<45;i++)• cout<<"*";• cout<<endl;• }• void hash(){• for(int i=0;i<45;i++)• cout<<"#";• cout<<endl;• }• void dash(){• for(int i=0;i<45;i++)• cout<<"-";• cout<<endl;• }

Passing Values To functionsPassing Values To functions• We can pass any type of value to

function• We have to define the data type of

the values • During function call we send on those

values which a function can accept

Passing Values To functionsPassing Values To functions

ProblemProblem

Write a function which takes three integers as parameters and display the smallest integer

• #include<conio>• #include<iostream>• void small(int,int,int );• int main(){• small(123,135,140);• getch();• return 0;• }• void small(int a,int b,int c){• int small=a;• if(b<small)• small=b;• if(c<small)• small=c;• cout<<"Samll is:"<<small<<endl;• }

Recommended