39
Programming Fundamentals

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

Embed Size (px)

Citation preview

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

ProgrammingFundamentals

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

Today’s Lecture

The Conditional Operator Logical Operators Structures Enumerations

Page 3: Programming Fundamentals. 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:

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

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.

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

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.

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

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.

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

Logical Operators

Page 8: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 9: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

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

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

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.

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

Output

Page 12: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 13: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 14: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

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

Using Structures

Page 15: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 16: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

Defining the Structure

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

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

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.

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

Syntax

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

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.

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

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.

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

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.

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

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:

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

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:

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

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.

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

Initializing Structure Members

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

OUTPUT???

Page 27: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 28: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

Task

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

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

Structures Within Structures

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

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.

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

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.

Page 32: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations
Page 33: Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations

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.

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

Syntax of enum specifier.

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

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.

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

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.

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

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

Check whether user can input value in enumeration variable

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

Output ???

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

Qusetions???