Class 11: Loops Flashcards
When is break statement valid?
In a loop or switch construct
When is continue valid?
Only valid is loop contruct
What does break do
Exits innermost loop or switch statement which encloses it regardless of loop conditions
Continue what does it do
Causes the loop to stop its current iteration and begin to execute from the top again
Go to (jump statement)
Go to identifier; //identifier is called a label here
Identifier: statement;
Dangling else
The compiler does not pay attention to formatting, so even if the else is aligned with the first if, the compiler pairs it with the SECOND IF
Boolean issues
Int I; if(I) is better represented as if(I != 0)
Float x; if(!x) is better represented as if (x == 0.0)
Char c; if (c) is buffer represented as if(c != ‘\0’);
Int ptr; if (ptr) is better represented as if(*ptr != NULL)
How to write an infinite loop
For(;;)
While(1)
The common operators
Examples
I = a,b,c; //stores a into I
I = (a,b,c); //stores c into I
For loop
For(n = 1,m=10; n <=m; n++, m—)
WHile(c = getchar(), c != ‘\0’)
Enumerated data types
An enumerated data type is designed for variables that contain only a limited set of values
EnumContainer_type = {CUP = 8; PINT = 16, Quart = 32, HALF_GALLON = 64, GALLON = 128};
Enum Container_Type milk_bottle;
Otherwise, but default CUP = 0, PINT = 1….
Can you do this
Enum{CUP, PINT QUART, HALF_GALLON, GALLON}milk_bottle, gas_can, medicine_bottle;
Yes, if ther is only one declaration of the variables of a particular enumerated type, not Enum data type name, both statement may be combined. However no more variables of this enum data type can be declared later because not enum data type was given to it.
Enum month{ Jan = 1;, feb = 2, mar = 3, apr = 4, may = 5, jun = 6, July = 7, aug = 8, sep = 9, oct = 10, nov = 11, dec = 12}this_month;
This_month = feb;