Exercise 6 – Compound Types und Kontrollfluss

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Structure.
Structures Spring 2013Programming and Data Structure1.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Lecture 16 CIS 208 Friday March 18 th, Last bit on structs, I swear Structs in Debugger It’s your friend.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops and Iteration for Statements, while Statements and do-while Statements.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Array, Structure and Union
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Java Programming Language Lecture27- An Introduction.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Computer Programming II
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Programmer Defined Types and Classes
Structures, Unions, Enumerations
11 Chapter Structured Data
Exercise 1 – Datentypen & Variablen
Programming Structures.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Structures Lesson xx In this module, we’ll introduce you to structures.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
For Loops October 12, 2017.
Arrays, For loop While loop Do while loop
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Structures putting data together.
One-Dimensional Array Introduction Lesson xx
Passing Structures Lesson xx
Derived types.
Structures.
Pointers, Dynamic Data, and Reference Types
Arrays November 8, 2017.
Review for Final Exam.
Chapter 11: Structured Data.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Windows Programming Lecture 04
Chapter 1: Introduction to Data Structures(8M)
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Chapter 10 Structures and Unions
Review for Final Exam.
Repetition Statements (Loops) - 2
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Standard Version of Starting Out with C++, 4th Edition
design OO words Debug Variables Data types
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
CSE 206 Course Review.
Pointers, Dynamic Data, and Reference Types
Structures Chapter 4.
Presentation transcript:

Exercise 6 – Compound Types und Kontrollfluss Informatik I / D-ITET Informatik I / D-ITET Exercise 6 – Compound Types und Kontrollfluss 1

Agenda Structures Unions Enumerations Loops (for, while, do while)

Structures Structures are user-defined Types Variable declaration: Type definition: Variable declaration: Accessing member variables: struct MyNewDataType { member_type1 member1; member_type2 member2; ... }; MyNewDataType myNewDataObject; myNewDataObject.member1 = ...;

Structures Definition Initialization Assignment struct MyStruct{ int i; float f; }; MyStruct m1; MyStruct m2 = {1, 2.0f}; m1 = m2; // m1.i = m2.i; m1.f = m2.f;

Structures Nesting Type of member variables can be arbitrary Self-defined types can also be used Example: No recursive nesting (no Date struct inside another Date)! Recursive data structures later in course struct Date{ int day; int month; int year; }; struct Person { Date birthday; string name; int age; };

Unions Definition Initialization If they don‘t have the same size, the union takes the size of the biggest element Values of the union array after initialization: union MyUnion{ char c[4]; // On the same memory int i; // On the same memory }; c[3] c[2] c[1] c[0] MyUnion u1; u1.i = 1; I = 0x00000001 c[0] == 1, c[1] == 0, c[2] == 0, c[3] == 0

Enumerations Definition Assignment is limited: enum Months{January, February, March}; enum Months{January = 1, February, March}; enum Months{January = 1, June = 6, March = 3}; int a = February; Months a = 2; // ERROR

Loops - Overview for loops while loops do – while loops

for Loop Declare and initialize loop variable: Check continue condition BEFORE: Increment AFTER loop int i=0; i < 4; i++ for (int i=0; i < 4; i++) { cout << “i is: “ << i; } Sidenote: Shown here is the C99 standard. Older C-code might require this: int i; for (i=0; i < 4; i++)

while Loop Declare and initialize loop variable: Check continue condition BEFORE: Increment AFTER loop int i=0; i < 4; i++ int i=0; while (i < 4) { cout << “i is: “ << i; i++; }

For and While Comparison int i; for (i=0; i < 4; i++) { cout << “i is: “ << i; } For and while can be used equivalently Loop variable i stays valid after the loop if declared outside of braces For loops for known length iteration While loops to loop until complex condition is met int i=0; while (i < 4) { cout << “i is: “ << i; i++; }

do while Loop Declare and initialize loop variable: Check continue condition AFTER: Increment AFTER loop int i=0; i < 4; i++ int i=0; do { cout << “i is: “ << i; i++; } while (i < 4);