+ All Categories
Home > Documents > switch statement

switch statement

Date post: 13-Mar-2016
Category:
Upload: callum-rocha
View: 78 times
Download: 1 times
Share this document with a friend
Description:
switch statement. For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one of the several possible cases break is used to jump out when match happens. char idChar; int aNumber, bNumber, cNumber; - PowerPoint PPT Presentation
Popular Tags:
41
switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one of the several possible cases break is used to jump out when match happens.
Transcript
Page 1: switch  statement

switch statementFor selecting one choice from a list of choices

switch statement evaluates an expression to determine a value

Match the value with one of the several possible cases

break is used to jump out when match happens.

Page 2: switch  statement

char idChar; int aNumber, bNumber, cNumber; swtich( idChar) { case ‘A’: aNumber=aNumber+1; break; case ‘B’: bNumber=bNumber+1; break; case ‘C’: cNumber=cNumber+1; default: cout<<“error”; }

Page 3: switch  statement

If we don’t use break it executes all commands after the selected choice.

break is used to skip over a part of a program. If we use break in loop it cause to jump out of the loop.

continue is used in loop and cause a program to skip the rest of the loop.

Page 4: switch  statement

#include<iostream.h>

const int Arsize=20; // define a constant integerint spaces=0;int main(){ char line[Arsiz]; cout<<“enter a line of text”<<endl; cin.get(line,Arsize);

for(int i=0; line[i]!=‘ \0’;i++) { cout<<line[i]; if (line[i]==‘.’) break; if ( line[i] != ‘ ‘) continue; spaces++; }

Page 5: switch  statement

cout<<“\n”<<spaces<<endl; return 0;

}

Page 6: switch  statement

What is the output of the following code

int i,j; int counter=0; int total=0; int n; cin>>n; for(i=0; i<n ; i++) { if( counter % 2 ) continue; for( j=0; j< n-i; j++) { total+= counter – j; if ( ! (j % 2) ) continue; counter ++; } }

Page 7: switch  statement

cout<<total;

Page 8: switch  statement

Writing to a Text FileWe need to declare the header file, which is fstream The fstream header file defines an ofstrem class

for handling the output.ofstream outFile;outFile.open(“sample.txt”);char name[20]; cin>> name;outFile<<name;

Page 9: switch  statement

cout<<“enter a file name “<<endl; cin>>name; ofstream fout; fout.open(name); if (! fout) // if ( !fout.is_open()){ cout<<“ file does not exist”<<endl; exit(1);} // if file exist open it otherwise it creates the file // we check whether it was successful or not double number; cin>>number;fout<<number;fout.close();

Page 10: switch  statement

Reading from Input File

We need to declare the header file, which is fstream The fstream header file defines an ifstrem

class for handling the input file

Page 11: switch  statement

#include<iostream.h>#include<fstream> //file I/O#include<cstdlib> // for exit() const int size=60; int main(){ char filename[size]; ifstream infile; cout<<“enter a file name”; cin.getline(filename,size); infile.open(filename); if( !infile.is_open()) { cout<<“not open”; exit(1); }

Page 12: switch  statement

double value; double sum=0.0; int count=0; infile>>value; while ( infile.good()) // while good input{ ++count; sum+=value; infile>>value;} if (infile.eof()) cout<<“end of file”<<endl; else if( infile.fail()) cout<<“data mismatch \n”;

Page 13: switch  statement

else cout<<“with no reason terminate \n”; if (count==0) cout<<“ no data \n”;else { cout<<count<<endl; cout<<“sum = “<<sum; cout <<“Average= “<<sum/count<<endl;} infile.close(); return 0;}

Page 14: switch  statement

Function Definition

Syntax typeName functionName(parameterList) { statements; return value; }

Page 15: switch  statement

#include<iostream.h> int simple(int val1, int val2); // int main(){ int a,b,sum; cout<<“enter two numbers”<<endl; cin>>a>>b; sum=simple(a,b); cout<<“the sum is = “<<sum;}

int simple ( int val1, int val2) { int total; total=val1+val2; return total;}

Page 16: switch  statement

int simple( int a,b); // not acceptable int simple( int , char);

We can send array to function as a parameter.

Page 17: switch  statement

#include<iostream.h> const int ArSize=8; int sum_arr(int arr[], int n); int main(){ int numbers[ArSize]={1,3,4,8,9,-1,2,0}; int sum=sum_arr(numbers,ArSize); cout<<“sum of the numbers is “<<sum;} int sum_arr(int arr[ ],int n){ int total=0; for(int i=0;i<ArSize;i++) total+= arr[i]; return total;}

Page 18: switch  statement

We can write

int sum_arr(int *arr, int n);

Page 19: switch  statement

Function and Pointers

double (*pf) (int );// pf points to a function that return double; double *pf(int); // pf is a function that return a pointer to a// double

Page 20: switch  statement

double pam( int ); double (*pf) (int); pf = pam;

double ned(double); int ted (int); double (*pf)(int) pf= ned; /// ? pf= ted; /// ?

Page 21: switch  statement

double pam(int); double (*pf) (int); pf=pam; double x=pam(4); double y=(*pf) (5); // call pam;

We can write double y= pf(5);

Page 22: switch  statement

#include<iostream.h> double betsy(int); double pam(in); void estimate (int lines, double (*pf)(int)); void main(){ int code; cout<<“how many line of code do you need?\n”; cin>>code; cout<<“ here is betsy estimate\n”; estimate(code, betsy); cout<<“ here is pam estimate\n”; estimate(code, pam); }

Page 23: switch  statement

double betsy( int lns){ return 0.05 * lns;}

double pam(int lns){ return 0.03*lns+0.0004*lns*lns;}

void estimate(int lns, double (*pf)(int)){ cout<<lns<<“lines will take “; cout<<(*pf)(lns)<<“hours(s) \n”;}

Page 24: switch  statement

Recursive function

In C++ a function can call itself.

Note that main() can’t call itself.

Recursive function are used in backtracking.Also are used in Artificial Intelligence.

Page 25: switch  statement

Example for void function

void recurs( argumentList){ statemts1; if (test) recurs (arguments); statements2; }

Page 26: switch  statement

More Example#include<iostream.h> long factorial ( int n);

int void main(){ int number; long fac_num; cout<<“enter a number less than 11”; cin>>n;fac_num=factorial(n); cout<<“factorial of “<<n<<“ is “<<fac_num;}

Page 27: switch  statement

long factorial ( int n){ if (n <= 1) return 1; else return (n*factorial(n-1));}

Page 28: switch  statement

Write a program to read some numbers and print out them in reverse order.

Input zero to quit. We are not allowed to use array.

Page 29: switch  statement

void print_reverse(int n ){ cout<<“enter a number : zero for

quit”<<endl; cin>>n; if( n ==0) return; print_reverse(n); cout<<n; }

Page 30: switch  statement

Write a program to print out all the string of size n of 0, 1 . ( n is an input);

Page 31: switch  statement

include <iostream.h> include<stdlib.h> void all_string(int n, int max_size, int arr[ ]) { if(n==max_size) { for( int i=0; i<max_size; i++) cout<<arr[i]; cout<<endl; return; } arr[n]=0; all_string(n+1,max_size; arr); arr[n]=1; all_string(n+1,max_size;arr);}

Page 32: switch  statement

int void main(); { int size; cout<<“enter an positive number”; cin>>size; if( size < 1) { cout<<“error”; exit(1); } int array=new int[size]; all_string(0,size,array); }

Page 33: switch  statement

Write a program to print out power set of set{1,2,…,n} ( n is an input).

Write a Maze program.

Write a program to find all m-subset of an n-set.

Page 34: switch  statement

Inline function

When the time execution of a function is short it’s better to declare it as an inline function

#include<iostream.h> inline double square(double x){ return x*x; }

Page 35: switch  statement

int main(){ double a,b; double c=13.0; a= square(5.0); b=square(4.5+7.5); cout<<a<< “ “<<b; return 0; }

Page 36: switch  statement

Call by Value & Call by Referencevoid swap ( int a, int b) { int temp; temp=a; a=b; b=temp; }int main(){ int num1=4, num2=9; swap(num1,num2); cout<<num1<< “ “ <<num2; }

Page 37: switch  statement

void swap1(int &a, int &b){ int temp; temp=a; a=b; b=temp;} void swap2(int *a, int *b){ int temp; temp=*a; *a=*b;*b=temp;}

Page 38: switch  statement

int main(){ int num1=4, int num2=9; swap1(num1,num2); cout<<num1<<“ “<<num2; swap2( &num1, &num2); cout<<num1<<“ “<<num2;}

Page 39: switch  statement

When Using Reference Argument

To allow you to alter a data in the calling function

To speed up the program by passing a reference instead of an entire data object

Page 40: switch  statement

Function Overloading

Having more than one function with the same name.

list of argument of a function are called signature of a function.

We can have more than one function with the same name but they must have different signatures.

Page 41: switch  statement

void print ( char * str, int length); void print( double d, int length); void print( long l, int d); void print (long v, int length);


Recommended