6_C_Part_3_Control_Structures.pdf

While 1 the former is more visually distinctive but

Info icon This preview shows pages 27–33. Sign up to view the full content.

while (1) ... ` The former is more visually distinctive, but both forms are used. ` In industry code that I saw, while(1) was used almost exclusively
Image of page 27

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

` Used to link related expressions together ` Evaluated from left to right ` The value of the right-most expression is the value of the whole combined expression ` Parentheses are necessary; if no parentheses surround an expression with commas, the commas are separators , and not the comma operator! ` Example: i = a, b, c; /*stores a into i – commas here are separators*/ i = (a, b, c); /*stores c into i – these commas are comma operators*/ ` For loop: for (n=1, m=10; n<=m; n++, m--) ` While: while (c=getchar(), c!= ‘0’) ` Exchanging values: (t=x, x=y, y=t); /*Better not to use comma operator unless you want to use this as a boolean, for example, to control a loop or an if or if-else conditional*/
Image of page 28
` An enumerated type is one whose values are symbolic constants rather than literals. ` Declaration example: enum Container_Type {CUP, PINT, QUART, HALF_GALLON, GALLON}; ` Declaration of a variable of the above type: enum Container_Type milk_bottle;
Image of page 29

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

` Variables declared with an enumerated type are actually stored as integers. ` Internally, the symbolic names are treated as integer constants, and it is legal to assign them values, e.g.: enum Container_Type {CUP=8, PINT=16, QUART=32, HALF_GALLON=64, GALLON=128}; Otherwise, by default, CUP =0, PINT=1, QUART=2, etc. ` Caution: don’t mix them indiscriminately with integers – even though it is syntactically valid to do so. milk_bottle = -623; /*A bad idea, and likely to lead to trouble*/ int a = PINT; /*Also a bad idea, and likely to lead to trouble*/
Image of page 30
` If there is only one declaration of variables of a particular enumerated type (i.e. no type name), both statements may be combined: enum {CUP, PINT, QUART, HALF_GALLON, GALLON} milk_bottle, gas_can, medicine_bottle; ` milk_bottle, gas_can, and medicine_bottle are now all instances of the enum type, and all these variables can be assigned CUP, PINT, etc. ` No more variables of this type can be declared later because no type name was given to it ` Nor can pointers to this variable type be declared
Image of page 31

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

#include <stdio.h> main() { enum month {jan = 1, feb=2, mar=3, apr=4, may=5, jun = 6, jul=7, aug=8, sep=9, oct=10, nov = 11,dec=12 } this_month; this_month = feb; printf(“month : %d\n",this_month); } Output??
Image of page 32
#include <stdio.h> main() { enum month {jan = 1, feb=2, mar=3, apr=4, may=5, jun = 6, jul=7, aug=8, sep=9, oct=10, nov = 11,dec=12 } this_month; this_month = feb; printf(“month : %d\n",this_month); } Output?? Output month: 2
Image of page 33
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern