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

Post on 14-Jan-2016

229 views 1 download

Tags:

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

ProgrammingFundamentals

Today’s Lecture

The Conditional Operator Logical Operators Structures Enumerations

The Conditional Operator

This operator consists of two symbols, which operate on three operands.

Is same as we write:

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.

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.

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.

Logical Operators

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

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.

Output

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

Using Structures

Defining the Structure

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

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.

Syntax

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.

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.

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.

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:

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:

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.

Initializing Structure Members

OUTPUT???

Task

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

Structures Within Structures

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.

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.

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.

Syntax of enum specifier.

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.

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.

Enumerations can hold int value or not Enum days={1,2}

Check whether user can input value in enumeration variable

Output ???

Qusetions???