How can I copy all the members of one structure variable into the memory reserved for another structure variable???
We can use a single assignment statement with no need to reference individual members of the functions or elements of arrays within the structures. For instance,
rr=pp;
copies all of the values of the members of the structure variable pp into the memory reserved for the structure variable rr. It is very simple to copy structures. Note, though, that we cannot perform manipulations on the structure in this manner. In other words,
rr = 3*pp;
is not a legal C statement because we cannot apply the multiplication operator on a structure variable without referencing the individual members of the structure.
Showing posts with label c decision structures. Show all posts
Showing posts with label c decision structures. Show all posts
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
}
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
..
..
}
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.
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.
Subscribe to:
Posts (Atom)