Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.

Similar presentations


Presentation on theme: "Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations."— Presentation transcript:

1 Programming Fundamentals

2 Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

3 The Conditional Operator This operator consists of two symbols, which operate on three operands. Is same as we write:

4 The Conditional Operator The part of this statement to the right of the equal sign is called the conditional expression: The question mark and the colon make up the conditional operator. The expression before the question mark is the test expression.

5 The Conditional Operator If the test expression is true, the entire conditional expression takes on the value of the operand following the question mark: alpha in this example. If the test expression is false, the conditional expression takes on the value of the operand following the colon: beta.

6 Logical Operators These operators allow you to logically combine Boolean variables (that is, variables of type bool, with true or false values). For example, If today is Sunday and its not raining then I will play cricket. The logical connection here is the word and, which provides a true or false value to the combination of the two phrases. Only if they are both true then I will play cricket.

7 Logical Operators

8

9 If input is ‘n’ then ?? If input is ‘Y’ then ?? If input is ‘N’ then ??

10 Structures A structure is a collection of simple variables. The variables in a structure can be of different types: Some can be int, some can be float, and so on. The data items in a structure are called the members of the structure. The syntax of a structure is almost identical to that of a class. A structure (as typically used) is a collection of data, while a class is a collection of both data and functions.

11 Output

12

13

14 Data type is not build in like int, float etc etc Its user defined. Using Structures

15

16 Defining the Structure The structure definition tells how the structure is organized: It specifies what members the structure will have.

17 Syntax of the Structure Definition The keyword struct introduces the structure definition. Next comes the structure name or tag, which is student (in our example). The declarations of the structure members— roll_numb, score_maths, score_PF and phone_numb —are enclosed in braces. A semicolon follows the closing brace, terminating the entire structure.

18 Syntax

19 Use of the Structure Definition The structure definition serves only as a model for the creation of variables of type structure. It does not itself create any structure variables; that is, it does not set aside any space in memory or even name any variables. This is unlike the definition of a simple variable, which set aside memory. A structure definition is merely a specification for how structure variables will look when they are defined.

20 Defining a Structure Variable The first statement in main() defines a variable, called student1 and student2 of type structure student. This definition reserves space in memory for student1 and student2.

21 Defining a Structure Variable How much space? Enough to hold all the members of student1— namely roll_numb, score_maths, score_PF and phone_numb. In this case there will be 4 bytes for each of the three ints (assuming a 32-bit system), and 8 bytes for the long.

22 Defining a Structure Variable The format for defining a structure variable is the same as that for defining a basic built-in data type such as int:

23 Accessing Structure Members Once a structure variable has been defined, its members can be accessed using something called the dot operator. Here’s how the first member can be assign a value:

24 Accessing Structure Members The structure member is written in three parts: the name of the structure variable (student1); The dot operator, which consists of a period (.); and the member name (roll_numb). The dot operator is also called as member access operator.

25 Initializing Structure Members

26 OUTPUT???

27

28 Task Check if 2 member initialization is ok instead of three???

29 Structures Within Structures

30 Initializing Nested Structures How to initialize a structure variable that itself contains structures? Each structure of type Distance, which is embedded in Room, is initialized separately. Remember that this involves surrounding the values with braces and separating them with commas.

31 Enumerations Structures can be looked at as a way to provide user-defined data types. A different approach to defining your own data type is the enumeration. Enumerated types work when you know in advance a finite (usually short) list of values that a data type can take on.

32

33 An enum declaration defines the set of all names that will be permissible values of the type. These permissible values are called enumerators. The enum type days_of_week has seven enumerators: Sun, Mon, Tue, and so on, up to Sat.

34 Syntax of enum specifier.

35 An enumeration is a list of all possible values. This is unlike the specification of an int, for example, which is given in terms of a range of values. In an enum you must give a specific name to every possible value.

36 Enumerations are treated internally as integers. This explains why you can perform arithmetic and relational operations on them. Ordinarily the first name in the list is given the value 0, the next name is given the value 1, and so on. In previous example, the values Sun through Sat are stored as the integer values 0–6.

37 Enumerations can hold int value or not Enum days={1,2} Check whether user can input value in enumeration variable

38 Output ???

39 Qusetions???


Download ppt "Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations."

Similar presentations


Ads by Google