0% found this document useful (0 votes)
20 views41 pages

Csir11-Introduction To Computer Programming: Unit Ii

This document discusses various control statements in C programming including if-else statements, while loops, do-while loops, for loops, nested control structures, switch statements, break statements, continue statements, and the comma operator. It provides examples and explanations of how each statement works. Key topics covered include using if-else for conditional branching, while, do-while and for loops for iterative execution, nested control structures, switch statements for multiple choices, and break/continue for terminating or skipping loops.

Uploaded by

bimbusbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views41 pages

Csir11-Introduction To Computer Programming: Unit Ii

This document discusses various control statements in C programming including if-else statements, while loops, do-while loops, for loops, nested control structures, switch statements, break statements, continue statements, and the comma operator. It provides examples and explanations of how each statement works. Key topics covered include using if-else for conditional branching, while, do-while and for loops for iterative execution, nested control structures, switch statements for multiple choices, and break/continue for terminating or skipping loops.

Uploaded by

bimbusbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CSIR11-INTRODUCTION TO

COMPUTER PROGRAMMING
UNIT II
Control Statements- Branching: if-else-looping: while- do-while-for; Nested control
Structures- switch statements- Break statements- Continue Statements-Comma operator-
goto statements.
DATE: 19/09/2023
Control Statements
• BRANCHING: THE if - else STATEMENT
• The if - else statement is used to carry out a logical test and then take one of
two possible actions, depending on the outcome of the test (i.e., whether the
outcome is true or false).
• The else portion of the if - else statement is optional.
• if ( expression) statement
• The expression must be placed in parentheses, as shown.
• In this form, the statement will be executed only if the expression has a
nonzero value (i.e., if expression is true).
• If the expression has a value of zero (i.e., if expression is false), then the
statement will be ignored.
• The statement can be either simple or compound.
Control Statements
• If with else clause:
Control Statements
• Example:
Control Statements
• Nested if else:

• it is not clear which expression (e1 or e2) is associated with the else
clause. The answer is e2. The rule is that the else clause is always
associated with the closest preceding unmatched (i.e., else-less) if.
Control Statements
• Example:
Control Statements
• If we wanted to associate the else clause with e1 rather than e2
Control Statements

• When a logical expression is encountered whose value is nonzero (true), the


corresponding statement will be executed and the remainder of the nested if -
else statements will be bypassed.
• The final else clause will apply if none of the expressions is true. It can be used to
provide a default condition or an error message.
Control Statements
• Example:
Control Statements
• LOOPING: THE while STATEMENT

• The statement will be executed repeatedly, as long as the expression is true


(Le., as long expression has a nonzero value).
• This statement can be simple or compound, though it is usually a compound
statement.
• It must include some feature that eventually alters the value of the
expression, thus providing a stopping condition for the loop.
Control Statements
• Example:
Control Statements
Control Statements

• Written more concisely as


Control Statements
• MORE LOOPING: THE do -while STATEMENT
• Sometimes, it is desirable to have a loop with the test for continuation at the end of
each pass.

• The statement will be executed repeatedly, as long as the value of expression is true
(i.e., is nonzero).
• Notice that statement will always be executed at least once, since the test for
repetition does not occur until the end of the first pass through the loop.
• The statement can be either simple or compound, though most applications will
require it to be a compound statement. It must include some feature that eventually
alters the value of expression so the looping action can terminate.
Control Statements
• Example:
Control Statements
• STILL MORE LOOPING: THE for STATEMENT

• where expression 1 is used to initialize some parameter (called an index) that


controls the looping action, expression 2 represents a condition that must be
true for the loop to continue execution, and expression 3is used to alter the
value of the parameter initially assigned by expression 1.
• Typically, expression 1 is an assignment expression, expression 2 is a logical
expression and expression 3 is a unary expression or an assignment
expression.
Control Statements
• When the for statement is executed, expression 2is evaluated and
tested at the beginning of each pass through the loop, and expression
3 is evaluated at the end of each pass.
• Thus, the for statement is equivalent to
Control Statements
• As a rough rule of thumb, while loops are generally used when the
number of passes is not known in advance, and for loops are generally
used when the number of passes is known in advance.
• From a syntactic standpoint all three expressions need not be included
in the for statement, though the semicolons must be present.
• The first and third expressions may be omitted if other means are
provided for initializing the index and/or altering the index. If the
second expression is omitted, however, it will be assumed to have a
permanent value of 1 (true); thus, the loop will continue indefinitely
unless it is terminated by some other means, such as a break or a
return statement
Control Statements
• Example:
Control Statements
• NESTED CONTROL STRUCTURES
• nested control structures can involve both loops and if - else statements.
Thus, a loop can be nested within an if - else statement, and an if - else
statement can be nested within a loop.
• Loops, like if - else statements, can be nested, one within another.
• The inner and outer loops need not be generated by the same type of control
structure.
• It is essential, however, that one loop be completely embedded within the
other -there can be no overlap.
• Each loop must be controlled by a different index.
Control Statements
• Example:
Control Statements
• THE switch STATEMENT
• The switch statement causes a particular group of statements to be chosen from
several available groups,
• The selection is based upon the current value of an expression which is included
within the switch statement.

• where expression results in an integer value.


• Note that expression may also be of type char.
• The embedded statement is generally a compound statement that specifies alternate
courses of action.
• Each alternative is expressed as a group of one or more individual statements within
the overall embedded statement.
Control Statements
• For each alternative, the first statement within the group must be
preceded by one or more case labels (also called case prefixes).
• The case labels identify the different groups of statements (i.e., the
different alternatives) and distinguish then from one another.
• The case labels must therefore be unique within a given switch
statement.
Control Statements
Control Statements
• where expression 1, expression 2, . . . , expression m represent constant, integer-
valued expressions.
• Usually, each of these expressions will be written as either an integer constant or
a character constant.
• Each individual statement following the case labels may be either simple or
complex.
• When the switch statement is executed, the expression is evaluated, and control
is transferred directly to the group of statements whose case-label value matches
the value of the expression.
• If none of the case label values matches the value of the expression, then none
of the groups within the switch statement will be selected. In this case control is
transferred directly to the statement that follows the switch statement.
Control Statements
• One of the labeled groups of statements within the switch statement
may be labeled default. This group will be selected if none of the case
labels matches the value of the expression.
• In a practical sense, the switch statement may be thought of as an
alternative to the use of nested if - else statements, though it can only
replace those if - else statements that test for equality.
Control Statements
• Example:
Control Statements
• THE break STATEMENT
• The break statement is used to terminate loops or to exit from a switch.
• It can be used within a for, while, do -while, or switch statement.
• The break statement is written simply as
break;
• without any embedded expressions or statements.
• The break statement causes a transfer of control out of the entire switch statement,
to the first statement following the switch statement.
• If a break statement is included in a while, do - while or for loop, then control will
immediately be transferred out of the loop when the break statement is
encountered.
• This provides a convenient way to terminate the loop if an error or other irregular
condition is detected.
Control Statements
• In the event of several nested while, do - while, for or switch
statements, a break statement will cause a transfer of control out of
the immediate enclosing statement, but not out of the outer
surrounding statements.
Control Statements
• Example:
Control Statements
• Example:
Control Statements
• THE continue STATEMENT
• The continue statement is used to bypass the remainder of the current pass
through a loop.
• The loop does not terminate when a continue statement is encountered.
• Rather, the remaining loop statements are skipped and the computation
proceeds directly to the next pass through the loop.
• The continue statement can be included within a while, a do - while or a for
statement.
• It is written simply as
continue;
• without any embedded statements or expressions.
Control Statements
• Example:
Control Statements
• Example:
Control Statements
• THE COMMA OPERATOR (,)
• Is used primarily in conjunction with the for statement. This operator permits
two different expressions to appear in situations where only one expression
would ordinarily be used.
for ( expression 1a, expression 1b; expression 2; expression 3) statement
• where expression 1a and expression 1b are the two expressions, separated by
the comma operator, where only one expression (expression 1) would
normally appear.
• These two expressions would typically initialize two separate indices that
would be used simultaneously within the for loop.
Control Statements
• Similarly, a for statement might make use of the comma operator in the
following manner.
for ( expression 1; expression 2; expression 3a, expression 3b) statement
• Here expression 3a and expression 3b, separated by the comma
operator, appear in place of the usual single expression.
• In this application the two separate expressions would typically be used
to alter (e.g., increment or decrement) two different indices that are
used simultaneously within the loop.
• For example, one index might count forward while the other counts
backward.
Control Statements
• Example:
Control Statements
• THE goto STATEMENT
• The goto statement is used to alter the normal sequence of program
execution by transferring control to some other part of the program.
• In its general form, the goto statement is written as
goto label;
• where label is an identifier that is used to label the target statement to which
control will be transferred.
• Control may be transferred to any other statement within the program. (To be
more precise, control may be transferred anywhere within the current
function.)
Control Statements
• The target statement must be labeled, and the label must be followed by a
colon.
• Thus, the target statement will appear as
label: statement
• Each labeled statement within the program (more precisely, within the
current function) must have a unique label; i.e., no two statements can have
the same label.
• All of the popular general-purpose programming languages contain a goto
statement, though modern programming practice discourages its use.
• The goto statement was used extensively, however, in early versions of some
older languages, such as Fortran and BASIC.
Control Statements
• Example:
Thank you

You might also like