Showing posts with label c switch statement. Show all posts
Showing posts with label c switch statement. Show all posts

Wednesday, January 11, 2012

What happens if we try to assign an integer type value to a float type variable?

C puts a decimal point at the end and converts the method of storage to exponential binary form and store the result in the variable's memory cell. Thus,

p = 6/4;

takes the integer value (6/4, which becomes 1 due to the cutting off of the fractional part), converts this to 1.0 (with a decimal point meaning it is in exponential binary form), and stores this in the memory cell for the variable.

Tuesday, January 3, 2012

How does an if else if control structure work?

How does an if else if control structure work?

An if-else-if control structure shifts program control, step by step, through a series of statement blocks. Control stops at the relational expression that is True and executes the corresponding statement block. After execution of the statement block, control shifts to the end of the control structure. If none of the relational expressions is true, the final statement block is executed.

The form of the if else if control structure is:


if (relational_expression_1)
{
statement_block_1
}
else if (relational_expression_2)
{
statement_block_2
}
.
.
.
.
else if (relational_expression_n)
{
statement_block_n
}
else
{
statement_block
}

Can I next switch control structures?

Can I next switch control structures?

Yes, a nested switch control structure could take the following form:

switch (outer_expression)
{
case constant_outer1:
switch (inner_expression)
{
case constant_inner1:
statement inner_1a
statement inner_1b
..
..

case constant_inner2:
statement inner_2a
..
..
}
case constant_outer2:
statement outer_2a
statement outer_2b
..
..
case constant_outer3:
statement outer_3a
statement outer_3b
..
..
}

What does the switch statement do?

What does the switch statement do?

A switch statement or switch control structure is constructed similarly to the if-else-if control structure. It also is used to transfer control. Its syntax is:

switch(expression)
{
case constant1:
statement1a
statement1b
...
case constant2:
statement2a
statement2b
...
...
default:
statements
}

where the expression must be enclosed in a pair of parenthesis and must result in an integer type value when the program flow enters the switch block. A switch block must be bounded by a pair of braces. The terms constant1, constant2, and so on are integer type constant expressions. Note that all constant expressions are followed by colons. All the constant expressions must be unique, meaning that none can have the same value as another constant expression. Although not required, it is common that the last case type line is the keyword default. If no constant matches the value of the expression, the statements in the default case are executed. The default case is optional. If no default case is given and no constant matches the number, the entire switch block is ignored.