0% found this document useful (0 votes)
5 views15 pages

C Operators and Control Statements Guide

This document covers operators in C, type conversion, typecasting, and decision control statements including conditional and unconditional branching. It details various conditional branching statements such as if, else-if, nested if-else, else-if ladder, and switch statements, along with their syntax and usage. Additionally, it explains break and continue statements, as well as the goto statement for altering program execution flow.

Uploaded by

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

C Operators and Control Statements Guide

This document covers operators in C, type conversion, typecasting, and decision control statements including conditional and unconditional branching. It details various conditional branching statements such as if, else-if, nested if-else, else-if ladder, and switch statements, along with their syntax and usage. Additionally, it explains break and continue statements, as well as the goto statement for altering program execution flow.

Uploaded by

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

Module 2 – Part B

Operators in C, Type conversion and typecasting


Decision control and looping statements: Introduction to decision control, Conditional
branching statements, Iterative statements, nested loops
break and continue statements, goto statements

INTRODUCTION TO DECISION CONTROL

C support two types of decision control statements that can alter the flow of sequence of
execution.

1. Conditional Branching statements


2. Unconditional Branching statements

CONDITIONAL BRANCHING STATEMENTS

It helps to jump from one part of the program to another depending on whether a particular
condition is satisfied or not.

These includes

1. If Statement
2. else-if Statement
3. Nested if- else Statement
4. else-if ladder
5. Switch Statements

1. If Statement
 It is the simplest form of decision control statement.
 It takes an expression in parenthesis and a statement or block of statements.
 First the test expression is evaluated. If the expression is true then the statement or block
of statements gets executed otherwise these statements are skipped and the execution will
jump to statement X.
 There is no semicolon after the text expression.
 General Form:
.

*****(For more programs please refer textbook)


2. else-if Statement

 First the condition is tested. If it evaluates to True, then block of statements block - 1 will be
executed and the statements block 2 is skipped. Otherwise if the expression is false,
statements block-2 will be executed and statements-1 block will be ignored. Now in any case
after the statement block 1 or 2 get executed the control will pass to statement x.
****(For more programs please refer textbook)

3. Nested if-else Statement


 Nested If in C is helpful if you want to check the condition inside a condition.
****(For more programs please refer textbook)
4. else-if ladder
The conditions are evaluated from the top downward. As soon as a true condition is found, the
statements associated with it are executed and the rest of the ladder is bypassed. If none of the
conditions are true, the final else is executed. That is, if all other conditional tests fail, the default
block of statements is executed. There is no limit of the number of else if statements, but the last
branch has to be an else statement.
****(For more programs please refer textbook)

5. switch Statements
 A switch case statement is a multi-way decision statement.

switch(expression)
{
case value1:
block-1
break;
case value2:
block-2
break;
case value3:
block-3
break;
.
.
.
default:
block-default
}
 Switch statements are mainly used in 2 situations
 When there is only one variable to evaluate in the expression.
 When many conditions are being tested for
 The value of expression is tested against the values, value1, value2 etc. one after another.
When a match is found, the block associated with that case is executed until the break
statement or the end of the switch statement is reached. The default-block is executed if
no matches are found. Although the default case is optional.
 The break statements inside the switch statement are optional. If the break statement is
omitted, execution will continue on into the next case's statements until either a break or
the end of the switch is reached.
 The break statements tell the compiler to jump out of the switch case statement and
execute the statement following the switch case construct.
 General syntax

The following points are noteworthy regarding a switch statement:

a) The expression must evaluate to an integer type. Thus, you can use character or integer
values in the cases, but floating-point expressions are not allowed. If character constants
are used in the switch statement, they are automatically converted to integers.
b) The case values should be constants. Variables are not allowed.
c) Every case label must evaluate to a unique constant expression value.
d) case label must end with a semicolon.
e) Even if there are multiple statements to be executed in each case there is no need to
enclose them within a pair of braces (unlike if, and else).
f) C permits nested switch statement, ie, a switch statement within another switch
statement.
g) The default case may appear anywhere within the switch statement - it need not
necessarily be placed at the end.
h) The default is optional, and if it is not present, no action takes place if all matches fail.
****(For more programs please refer textbook)
break and continue statements

break Statement
The break statement has two uses. You can use it to terminate a case in the switch statement.
You can also use it to force immediate termination of a loop. Any statement inside the loop
following the break will be neglected and the control goes out of the loop. break stops the current
iteration and skips the succeeding iterations (if any) and passes the control to the first statement
outside the loop. This is illustrated in the following code:
continue Statement
 When the compiler encounters a continue statement then the rest of the statement in the
loop are skipped and the control is unconditionally transferred to the loop continuation
portion of the nearest enclosing loop.
 Syntax: just type the keyword continue followed by a semicolon.
continue;
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, it 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. 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 must have a unique label; i.e., no two statements can
have the same label.
 If the label is placed after the goto statement, then it is called Forward Jump and in case
it is located before the goto statement, it is said to be a Backward Jump.

You might also like