Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.

Similar presentations


Presentation on theme: "1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab."— Presentation transcript:

1

2 1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab on April 24th, ….To Earthquake Zone) Lab session-VI deals with functions We will learn how to write independent functions that can be called from main() Students are given examples of functions

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 Lab Exercise VI-A 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,…………..

10 9 Lab Assignment#4 Due 5/1 A classroom has the maximum capacity of 37 people, write a program that accepts the total number of students wishing to register for a course. This program checks for any violations of fire safety code. It asks the user to input the total number of students. If this number is more than the maximum allowed, it prints a warning and shows how many people will not get the course otherwise it shows the additional number of people who can be admitted to the course. Bonus 2 marks for making it a loop and asking again and again until the number of students is exactly equal to the capacity

11 10 Lab VI Continued We look at some C++ code segments Introducing functions We solve one programming problem

12 11 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; }

13 12 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; }

14 13 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”; }

15 14 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--; }

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

17 16 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

18 17 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

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

20 19 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

21 20 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 ;

22 21 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)

23 22 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

24 23 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();

25 24 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(){……….}?

26 25 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………..


Download ppt "1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab."

Similar presentations


Ads by Google