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

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Structure.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
True or false A variable of type char can hold the value 301. ( F )
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
Data Types.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Selection Structures (if & switch statements) (CS1123)
Input, Output, and Processing
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Simple Data Types Built-In and User Defined Chapter 10.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
MIT AITI Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Programming Fundamentals Enumerations and Functions.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Bill Tucker Austin Community College COSC 1315
LESSON 06.
Lecture 4b Repeating With Loops
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
And now for something completely different . . .
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Classes and Objects.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Primitive Types and Expressions
C Language B. DHIVYA 17PCA140 II MCA.
Variables and Constants
Structures Chapter 4.
Getting Started With Coding
Presentation transcript:

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:

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???