0% found this document useful (0 votes)
5 views

Control Statement 1

The document discusses various control statements in C including selection statements like if, if-else, and switch that allow for conditional execution, iteration statements like for, while, and do-while that allow for repetitive execution, and jump statements like goto, break, continue, and return. It also covers relational operators that are used to test conditions, logical operators that are used to combine conditions, and examples of how these control structures and operators work.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Control Statement 1

The document discusses various control statements in C including selection statements like if, if-else, and switch that allow for conditional execution, iteration statements like for, while, and do-while that allow for repetitive execution, and jump statements like goto, break, continue, and return. It also covers relational operators that are used to test conditions, logical operators that are used to combine conditions, and examples of how these control structures and operators work.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CONTROL STATEMENTS

1. LEARNING OBJECTIVES
Understanding meaning of a statement
and statement block

Learn about decision type control


constructs in C and the way these are used
Learn about looping type control
constructs in C and the technique of
putting them to use
Learn the use of special control constructs
such as goto, break, continue, and return

Learn about nested loops and their utility


2. CONTROL STATEMENTS INCLUDE
Selection Iteration Jump
Statements Statements Statements
• if • for • goto

• if-else • while • break

• switch • do-while • continue

• return
PROGRAM CONTROL
STATEMENTS/CONSTRUCTS IN ‘C’

Program Control
Statements/Constructs

Selection/Branching Iteration/Looping

Conditional Unconditional do-


for while
Type Type while

if-else-
if if-else
if
switch break continue goto
OPERATORS
<
! >

!= ==
Operators

<=
||
>=

&& !=
Equality and Logical
RELATIONAL OPERATORS
Operators

To Specify Symbol Used To Specify Symbol Used


less than <
Equal to ==
greater than >
Not equal to !=
less than or <=
equal to >= Logical AND &&
greater than or
equal to Logical OR ||

Negation !
POINTS TO NOTE
 If an expression, involving the relational operator, is true, it is given a value of 1. If
an expression is false, it is given a value of 0.
 Similarly, if a numeric expression is used as a test expression, any non-zero value
(including negative) will be considered as true, while a zero value will be
considered as false.

 Space can be given between operand and operator (relational or logical) but space
is not allowed between any compound operator like <=, >=, ==, !=. It is also
compiler error to reverse them.

 a == b and a = b are not similar, as == is a test for equality, a = b is an assignment


operator. Therefore, the equality operator has to be used carefully.

 The relational operators have lower precedence than all arithmetic operators.
A FEW EXAMPLES
The following declarations and initializations
are given:
int x=1, y=2, z=3;
Then,
 The expression x>=y evaluates to 0 (false).
 The expression x+y evaluates to 3 (true).
 The expression x=y evaluates to 2 (true).
LOGICAL OPERATORS MAY BE MIXED WITHIN RELATIONAL EXPRESSIONS BUT
ONE MUST ABIDE BY THEIR PRECEDENCE RULES WHICH IS AS FOLLOWS:

(!) NOT
operator && AND
operator || OR
operator
OPERATOR SEMANTICS

Operators Associativity
() ++ (postfix) -- (postfix) left to right
+ (unary) - (unary) right to left
++ (prefix) -- (prefix) * / % left to right
+- left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
?: right to left
=+=-=*=/= right to left
, (comma operator) left to right
CONDITIONAL EXECUTION
AND SELECTION
Selection Statements

The Conditional Operator

The switch Statement


SELECTION STATEMENTS
One-way decisions using if statement

Two-way decisions using if-else statement

Multi-way decisions

Dangling else Problem


ONE-WAY DECISIONS USING IF
STATEMENT
Flowchart for if construct
if(TestExpr)
stmtT; T F
TestExpr

stmtT
WRITE A PROGRAM THAT
PRINTS THE LARGEST AMONG
THREE
Algorithm
1. START
NUMBERS.
C Program
#include <stdio.h>
2. PRINT “ENTER THREE int main()
NUMBERS” {
int a, b, c, max;
3. INPUT A, B, C printf(“\nEnter 3 numbers”);
4. MAX=A scanf(“%d %d %d”, &a, &b, &c);
max=a;
5. IF B>MAX THEN MAX=B if(b>max)
6. IF C>MAX THEN MAX=C max=b;
if(c>max)
7. PRINT “LARGEST
max=c;
NUMBER IS”, MAX
printf(“Largest No is %d”, max);
8. STOP return 0;
}

You might also like