Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

Similar presentations


Presentation on theme: "1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)"— Presentation transcript:

1

2 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL) We will learn how to write independent functions that can be called from main() We do not use parameter passing, however, we learn how functions can return values Lab VI Continues (FINAL SESSION)

3 2 C++ Syntax Rules in Brief #include #…..other include files go here void main(void) { –Declarations of variables and constants –Assignment and control statements }

4 3 C++ Syntax Rules in Brief Make it a habit to put declarations in the beginning. For example, –int employeeID; –const int current-year=2000; –float deduction-amount; Once a data item has been declared, do not mention its data type in assignment statement int employeeID=23; isWRONG employeeID=23; isRIGHT

5 4 C++ Syntax Rules in Brief If you write a control statement, its conditions must be in parenthesis with no semicolon after the parenthesis For example, displaying employeeID if employeeID is 24; if (employeeID == 24) { cout<<“ID is “<<employeeID<<endl; } Please note “double-equal” in comparing, no semicolon after comparison statement and joining several displayable messages with <<

6 5 C++ Syntax Rules in Brief Different comparison statements if (employeeID != 24) { cout<<endl;} (if employeeID is not equal to 24, do a blank cout) if (employeeID >= 24) if (employeeID <=24) if (employeeID > 24) if (employeeID < 24) We could also use an else clause to capture the false results of comparison

7 6 C++ Syntax Rules in Brief For example, if employeeID is 24, display “ID is 24” else do a blank line display if (employeeID == 24) { cout<<“ID is “<<employeeID<<endl; } else {cout<<endl;} Use braces to show blocks of code even if only a single statement is there in if part or else part (for clarity purposes)

8 7 C++ Syntax Rules in Brief In assigning values, there can be only one destination shown on left of the single equal sign deductions = pay- (pay*tax-percentage); Please note the use of parenthesis to emphasize and make the expression very clear

9 8 The for Statement “for” is a loop statement that is controlled through a loop control variable for (lcv=1; lcv<=100; lcv++) The above loop will start with lcv=1 and it will run until lcv equals 100. The step size is 1 (lcv++) Compare it to the above for (lcv=1; lcv<=100; lcv+=2)

10 9 The for Statement “for” statement and while statement can produce identical loops “for” is preferred if the number of iterations is known beforehand. “while” is preferred if the total iterations cannot be computed in advance

11 10 Logical Operators Logical AND is represented by && Logical OR is represented by || Logical NOT is represented by ! Comparing if equal is represented by == Example: Write a logical expression to be true if x is restricted between 1 and 100 Write a logical expression to check if x equals y-50 and x is positive Experiment 5.5

12 11 Lab Exercise Develop a program that takes a number from the user and then enters a loop to keep displaying the result of successively multiplying the number by 2. The loop is terminated when the number exceeds half of INT_MAX (Do not forget to include the header file limits.h) Example: 2,4,8,16,32,64,…………..

13 12 Functions Functions are “subroutines”, “procedures”, “modules” or “program units”. Functions perform a given task, usually one function is dedicated to one task One advantage of using functions is to avoid repeated coding for the same job

14 13 Functions Reduce Code Size For example, consider a program that prints text in a box, something like below **************************************** * * The Journey Through Central Spine * * **************************************** We can achieve this printout with a program containing 9 cout statements OR we can develop a function to print aesterik lines

15 14 Program with 9 cout Lines cout<<“****************************************”<<endl; cout<<“* *”<<endl; cout<<“* The Journey Through Central Spine *”<<endl; cout<<“* *”<<endl; cout<<“****************************************”<<endl; cout<<“****************************************”<<endl;.

16 15 Same Program with a Function Print_three_star_lines(); cout<<“* *”<<endl; cout<<“* The Journey Through Central Spine *”<<endl; cout<<“* *”<<endl; Print_three_star_lines(); Now the program is more compact and readable. Actual function definition will be developed after the main() function’s code DEMONSTRATION OF PROGRAM

17 16 Function Specific Actions If you plan to work with functions, make sure that you name your functions according to C++ naming conventions (letters, underscores, digits, no digits in the beginning) As demonstrated, function data type is specified before its name. If the function does not return anything, void is used Also note the DECLARATION before the actual function code, ending with a ;

18 17 Function Call Notice the function name is simply repeated when it is called from the main routine print_three_star_lines(); The empty parenthesis indicate the fact that no data is passed to this function. A semicolon is a must to terminate any C++ statement (except loop starters)

19 18 The Events When a function is called from the main routine, the following events take place Control (of the running program) is transferred to the function as well as any data that may be needed Function performs its task and at the end, transfers the control back to the calling routine

20 19 Why does the Function name appear so many times? Count the number of times the function name is repeated in our program The name appears each time for a different purpose Firstly we have to declare the function. Function declaration is called its prototype void print_three_star_lines();

21 20 Why does the Function name appear so many times? Next, we have to define the function,i.e. its actual source code void print_three_star_lines() {………….} Finally, we have to make the function call print_three_star_lines(); What do you know about main function when it reads void main(){……….}?

22 21 Lab VI Continued We look at some C++ code segments We solve one programming problem Lab Assignment#5 due 12/7

23 22 Basic Concepts What will be the output? Don’t run this code, just predict void main() { int num1,num2; num1=12345; num2=6789; cout<<num1<<num2<<endl; }

24 23 Basic Concepts What is wrong with this code segment? void main() { int num1, myloop; while (myloop<24) cout<<“Once in myloop, show no mercy”<<endl; }

25 24 Basic Concepts What is wrong with this code segment? Void main() { int mybirthdate; mybirthdate =2; while (mybirthdate<24); { cout<<“Enter my birth date (date only)”<<endl; cin>> mybirthdate; } cout<<“you got it…………\n”; }

26 25 Basic Concepts What is the error in this code segment? int cans; for(int cans=1; cans<=100; cans++) { cout<<“Each one has a refund of 5 cents\n”; cout<<“count until 100 cans are done”; cans--; }

27 26 Basic Concepts Fix this code segment int k; cout >k; if (k=5) cout<<“You typed 5”; else cout<<“Nothing else please”;

28 27 Lab Programming Exercise Write a program that accepts a digit entered by a user and then displays it in words. For example, user enters 8, program displays, “You Entered Eight” User enters 0, program displays “You Entered Zero” and so on………..

29 28 Lab Assignment#5 Due 12/7 A car can hold 12 gallons of gasoline and it can travel 360 miles without refuelling. Write a program the displays the trip meter of the car followed by amount of fuel left. Every time the user enters letter ‘r’, 10 miles are added to the trip meter and appropriate amount of fuel is deducted. As the amount of fuel becomes 1 gallon, the program prints a warning.

30 29 Sample Output s Miles so far: 160 Fuel left: 6.66667 s r Miles so far: 170 Fuel left: 6.33333 r Miles so far: 180 Fuel left: 6


Download ppt "1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)"

Similar presentations


Ads by Google