Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views27 pages

L02 - Control Statements

The document provides an overview of control statements in C programming, including operators, branching, and looping constructs. It explains various types of operators such as arithmetic, relational, logical, and bitwise operators, as well as control flow statements like if-else, switch-case, for, while, and do-while loops. Additionally, it discusses the use of break and continue statements in loops, along with examples to illustrate their functionality.
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)
4 views27 pages

L02 - Control Statements

The document provides an overview of control statements in C programming, including operators, branching, and looping constructs. It explains various types of operators such as arithmetic, relational, logical, and bitwise operators, as well as control flow statements like if-else, switch-case, for, while, and do-while loops. Additionally, it discusses the use of break and continue statements in loops, along with examples to illustrate their functionality.
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
You are on page 1/ 27

C Programming

Control Statements

"Defined path for a Stable career in Core Technologies“


[email protected]
INDEX
❑Operators
❑Control Statements
❑Branching
❑Jump Statements
❑Loops

🌐 etechprowess.com
Operators
• An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built-in operators and provides the following types
of operators .

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
C Arithmetic Operators
• An arithmetic operator performs mathematical operations such as
addition, subtraction, multiplication, division etc on numerical values
(constants and variables).

Operator Meaning of Operator
+ addition or unary plus

subtraction or unary
-
minus

* multiplication
/ division
remainder after division
%
(modulo division)
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to
bit-level which makes processing faster and saves power. Bitwise operators are used in C programming to perform bit-
level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right


Relational Operators
• A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.
Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0


Logical Operators
• An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used in
decision making in C programming.

Operator Meaning Example

If c = 5 and d = 2
Logical AND. True
then, expression
&& only if all operands
((c==5) && (d>5))
are true
equals to 0.

If c = 5 and d = 2
Logical OR. True
then, expression
|| only if either one
((c==5) || (d>5))
operand is true
equals to 1.

Logical NOT. True If c = 5 then,


! only if the operand expression !(c==5)
is 0 equals to 0.
Assignment Operators
• Assignment operators are used to assigning value to a variable.
• The left side operand of the assignment operator is a variable and right side
operand of the assignment operator is a value.
• The value on the right side must be of the same data-type of the variable on the
left side otherwise the compiler will Operators Meaning of operators
raise an error.
= Assignment operator

+= Add AND assignment operator

-= Subtract AND assignment operator.

*= Multiply AND assignment operator.

/= Divide AND assignment operator.

%= Modulus AND assignment operator.


Special Operators
Below are some of the special operators that the C programming
language offers.
•The Comma Operator
•Type cast Operator
•Reference operator or Address Operator ("&")
•Dereference operator ("*") or Pointer Operator
•sizeof operator
Example for relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Example of Pointer demonstrating the use of & and *
#include <stdio.h>
Size of operator
int main()
{ #include <stdio.h>
int *p; int main() {
int a = 16;
int var = 10;
printf("Size of variable a : %d\n",sizeof(a));
p= &var; printf("Size of int data type : %d\n",sizeof(int));
printf("Value of variable var is: %d", var); printf("Size of char data type : %d\n",sizeof(char));
printf("\nValue of variable var is: %d", *p); printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
printf("\nAddress of variable var is: %p", &var);
return 0;
printf("\nAddress of variable var is: %p", p); }
printf("\nAddress of pointer p is: %p", &p);
return 0;
}
Order of evaluation in c
Order of evaluation refers to the operator precedence and associativity rules according to
which mathematical expressions are evaluated.
Operators in the same row have the same precedence. The compiler will evaluate them in
any order.Operators in different rows have different precedence. Rows are in order of
decreasing precedence.
The order of precedence can be altered by using parentheses around the parts of the
mathematical expression that needs to be evaluated first.
#include <stdio.h>
int main() {
int answer_1 = 0; // stores order evaluated answer
int answer_2 = 0; // stores left to right evaluation
answer_1 = 2 * 3 + 4 / 2;
answer_2 = 2 * (3 + 4) / 2;
printf("Answer with order of evaluation: %d\n", answer_1);
printf("Answer without order of evaluation: %d\n", answer_2);
return 0;
}
Table of the order of evaluation
Control Flow Statements
Control-Flow Statements
• C supports an unconditional set of branching statements that transfer the control to
another location in the program.
• C provides two styles of flow control:
• Branching: It is deciding what actions to take and looping is deciding how many times to
take a certain action.
• Looping : Loops provide a way to repeat commands and control how many times they are
repeated.

• if...else Statement
• switch...case
• for Loop
• while Loop
• break and continue
if...else Statement
• This is the most simple form of the branching statements.
• It takes an expression in parenthesis and an statement or block of statements. if the
expression is true then the statement or block of statements gets executed otherwise
these statements are skipped.
IF IF ELSE IF
if (expression) if (expression)
{ {
Block of statements; Block of statements;
} }
else if(expression)
IF else {
if (expression) Block of statements;
{ }
Block of statements; else
} {
else Block of statements;
{ }
Block of statements;
}
switch case Statements
• The switch statement is much like a nested if .. else statement. Its mostly a matter of
preference which you use, switch statement can be slightly more efficient and easier
to read.
• Syntax:
• switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
……….
.........
default:
// code to be executed if all cases are not matched;
}
• Note: If we do not use the break statement, all statements after the matching label are also executed.
The default clause inside the switch statement is optional.
for loop
• A for loop is a repetition control structure which allows to write a loop that is
executed a specific number of times.
• Syntax:
• for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
while loop
• A while is much similar to for loop
• A while loop in C programming repeatedly executes a target statement as long as a
given condition is true.
• Syntax
• while(condition) {
// statement(s);
}

• Question: Can you tell the difference between for and while loop , and where it can be used
do - while loop
• do ... while is just like a while loop except that the test condition is checked at
the end of the loop rather than the start.
• This has the effect that the content of the loop are always executed at least
once.
• It is also referred as exit controlled loop
• Syntax
• do
{
/*Single statement
or
Block of statements;*/
}while(expression);

• Question: What is the difference between while & do-while loop


Spot the difference

Program 01

Program 02
Spot the difference

Output of Program 01

Output of Program 02
break Statement
• The break is a keyword in C which is used to bring the program control out of the loop.
• break statement is used inside loops or switch statement.
• break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops.
• Syntax:
• // loop or switch case
break;
break Statement
• Example – break Statement
continue Statement
• The continue statement is used to bring the program control to the beginning of the
loop.
• continue statement skips some lines of code inside the loop and continues with the
next iteration.
• It is mainly used for a condition so that we can skip some code for a particular
condition.

• Syntax:
• // loop statements
continue;
continue Statement
• Example – continue Statement
THANK YOU

eTECH Prowess

Phone:+91 7676651416, 080 4890 919

Email:[email protected]

http://etechprowess.com

You might also like