Faculty: Ch.
Lavanya Susanna, dept of cse
UNIT II
Syllabus:
Bitwise Operators: Exact Size Integer Types, Logical Bitwise Operators, Shift Operators.
Selection & Making Decisions: Logical Data and Operators, Two Way Selection, Multiway
Selection, More Standard Functions
Repetition: Concept of Loop, Pretest and Post-test Loops, Initialization and Updating, Event and
Counter Controlled Loops, Loops in C, Other Statements Related to Looping, Looping
Applications, Programming Examples
Control statements: These statements control the flow of the program and out of them
Selection statements: There are two types of Flowchart:
selection statements 1)if statement and 2)switch
statement
1)The if statement is of different forms.
Simple if statement
if.-else statement(Two way selection
statement)
if - else if statement(Multi way selection
statement)
a. Simple if statement
Syntax:
if (condition)
{
Statements;
}
Faculty: Ch.Lavanya Susanna, dept of cse
Working: Flowchart:
the condition is true, if block is executed
the condition is false, the statements
outside if block gets executed i.e if block
statements are skipped
The condition should always result in a
value which would be treated as true (1)
or false (0).
Other than 0(zero), all other values are
considered as true.
Example:
If (27)
printf("hello");
In above example, hello will be
printed. Working:
C program to demonstrate simple if
the condition is true, if block is executed
statement and else block statements are skipped
the condition is false, else block is
#include<stdio.h>
executed i.e if block statements are
void main()
skipped
{
int m=40,n=40; The condition should always result in a
if (m == n) value which would be treated as true (1)
{ or false (0).
printf("m and n are equal");
} Programs:
Output: m and n are equal C program to find largest of two numbers
b.if....else statement (Two way selection #include <stdio.h>
statement) void main()
{
Syntax: int a,b;
if (condition) printf(“enter a and b values");
{ scanf(“%d%d”,&a,&b);
Statements; if(a>b)
} printf(“ the largest value is %d\n”,a);
else
else printf(“ the largest value is %d\n”,b);
{ }
Statements;
}
Faculty: Ch.Lavanya Susanna, dept of cse
C program to find given number is even or else
odd printf(“the character is not an oval\n”);
}
#include <stdio.h>
void main()
{
int a ;
printf(“enter a value");
scanf(“%d”,&a); c. if - else if statement (Multi way selection
if(a%2==0) statement)
printf(“number is even\n”); Syntax:
else
printf(“number is odd\n”); Syntax:
}
if(condition1)
{
Statement 1;
}
else if (condition2)
C program to find a person is eligible for vote {
or not Statement 2;
}
#include <stdio.h> else
void main() {
{ Statement ;
int age; }
printf(“enter the person age\n”): Flowchart:
scanf(“%d”,&age);
if(age>=18)
printf(“person is eligible for vote\n”);
else
printf(“person is not eligible for vote\n”);
}
C program to check whether given letter is
oval or not
#include <stdio.h>
void main()
{
char ch;
printf(“enter a character\n”);
scanf(“%c”,&ch);
if ( ch ==„a‟ ||ch ==„e‟ ||ch ==„i‟ ||ch ==„o‟ ||ch
==„u‟ )
printf(“the character is an oval\n”);
Faculty: Ch.Lavanya Susanna, dept of cse
Working: Rules for using switch statement:
the condition 1 is true, statement1is 1. The expression (after switch keyword)
executed i.e statement2 and else block must yield an integer value i.e the
statement are skipped expression should be an integer or a
the condition1 is false, Condition2 is variable or an expression that evaluates to
tested- Condition2 is true statement2 is an integer.
executed i.e else block statement is 2. The case label values must be unique.
skipped 3. The case label must end with a colon(:)
the condition2 is false , else block 4. break statements are used to exit the
statements are executed switch block. It isn't necessary to
The condition should always result in a use break after each block, but if you do
value which would be treated as true (1) not use it, then all the consecutive blocks
or false (0). of code will get executed after the
C program to find largest of three numbers: matching block.
#include <stdio.h> 5. default case is executed when none of the
void main() mentioned case matches
{ the switch expression.
int A, B, C;
printf("Enter the numbers A, B and C\n "); Flowchart:
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf(" the largest number is %d \n", A);
else if (B >= A && B >= C)
printf("%d is the largest number is %d \n",
B);
else
printf("the largest number is %d \n", C);
}
Output:
Enter the numbers A, B and C
5 6 8
the largest number 8
2. Switch statement:( Multi way selection
statement)
Switch statement is a control statement
that allows to choose only one choice among the
many given choices. It is used when there are
many test conditions.
Syntax:
switch (expression)
{
case value1: statements ;
break;
case value2: statements;
break;
...
default: default statements;
}
Faculty: Ch.Lavanya Susanna, dept of cse
break;
case 'U':
printf("Vowel");
Working:
break;
The value of expression in switch is default: printf("Consonant");
compared to the values present in }
different cases. }
It executes that block of code which
matches the case value. Or
If there is no match, then default block is #include <stdio.h>
executed (if present). void main()
C program to check vowel or consonant {
using switch case char ch;
printf("Enter any alphabet\n ");
C program to check vowel or consonant using scanf("%c", &ch);
switch case switch(ch)
{
#include <stdio.h> case 'a':
void main() case 'e':
{ case 'i':
char ch; case 'o':
printf("Enter any alphabet\n "); case 'u':
scanf("%c", &ch); case 'A':
switch(ch) case 'E':
{ case 'I':
case 'a': case 'O':
printf("Vowel"); case 'U':
break; printf("Vowel");
case 'e': break;
printf("Vowel"); default: printf("Consonant");
break; }
case 'i': }
printf("Vowel"); Output:
break; Enter any alphabet
case 'o': E
printf("Vowel"); Vowel
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
Faculty: Ch.Lavanya Susanna, dept of cse
C program to implement basic calculator C program to print day of week using switch
using switch case
#include <stdio.h> #include <stdio.h>
int main() void main()
{ {
char op; int week;
float a , b, result; printf("Enter week number(1-7)\n ");
printf("Enter an operator (+, -, *,/)\n "); scanf("%d", &week);
scanf("%c", &op); switch(week)
printf("Enter a and b values\n "); {
scanf("%d %d", &a, &b); case 1:
switch (op) printf("Monday");
{ break;
case '+':result= a+b; case 2:
printf(“the result after addition is printf("Tuesday");
%f\n”,result); break;
break; case 3:
case '-': result= a-b; printf("Wednesday");
printf(“the result after subtraction is break;
%f\n”,result); case 4:
break; printf("Thursday");
case '*': result= a*b; break;
printf(“the result after multiplication case 5:
is %f\n”,result); printf("Friday");
break; break;
case '/': result= a/b; case 6:
printf(“the result after division is printf("Saturday");
%f\n”,result); break;
break; case 7:
default: printf("Error! operator is not printf("Sunday");
correct"); break;
} default:
} printf("Invalid input! Please enter
week number between 1-7.");
Output: }
Enter an operator (+, -, *,/) }
+ Output:
Enter a and b values Enter week number(1-7)
2 3 2
the result after addition is 5.00000 Tuesday
Faculty: Ch.Lavanya Susanna, dept of cse
Iteractive or Loop statements: In any
programming language including C, loops are
used to execute a set of statements repeatedly Flowchart:
until a particular condition is satisfied.
There are mainly two types of loops:
1. Entry Controlled loops (pre-test loops):
In this type of loops the test condition is
tested before entering the loop body. For
Loop and While Loop are entry controlled
loops.
2. Exit Controlled Loops(post-test loop): In
this type of loops the test condition is
tested or evaluated at the end of loop
body do – while loop is exit controlled
loop.
1. While loop: Repeats a statement or group of
statements while a given condition is true. It tests
the condition before executing the loop body.
It is completed in 3 steps.
• Variable initialization
• condition Working:
• Variable increment or
decrement Initialization statement (counter variable)
Syntax: is executed only once
Next condition is tested, if it is true then
variable initialization; while statements are executed. The
while(condition) condition of the loop is tested before the
{ body of the loop is executed; hence it is
statements; called an entry-controlled loop.
variable increment or decrement;
Again control goes to test the condition.
}
This process is repeated until condition is
Eg: i = 1 ;
fslae
while(i<=5)
If condition is false, while statements are
{
not executed.
printf(“%d\t”, i);
i++; A loop becomes an infinite loop if a
} condition never becomes false
Count-controlled repetition requires
control variable (or loop counter)
initial value of the control variable
increment (or decrement) by which
the control variable is modified each
iteration through the loop
Faculty: Ch.Lavanya Susanna, dept of cse
condition that tests for the final value
of the control variable
A count-controlled repetition will exit after
running a certain number of times. The
count is kept in a variable called an index C program to find factorial of given number:
or counter. When the index reaches a
certain value (the loop bound) the loop will #include<stdio.h>
end. void main()
{
When we do not know in advance the int num , fact=1 , i ;
printf("Enter a number\n");
number of times we want to execute a
scanf("%d",&num);
statement, we cannot use count-controlled i=1;
repetition. In such an instance, we would while(i <= num)
use event controlled loops {
fact = fact * i;
Eg: i = 1 ; (counter controlled while i++;
loop) }
while(i<=5) printf("Factorial is %d\n",fact);
{ }
printf(“%d\t”, i);
i++;
}
Here counter variable is i
2.do while loop: It is more like a while
statement, except that it tests the condition at the
end of the loop body.
C Program to print first 10 natural numbers
do while loop can be addressed as an exit
control loop or post-test loop
#include<stdio.h>
It is completed in 3 steps.
void main( )
• Variable initialization
{
• Variable increment or
int x;
decrement
x = 1;
• condition
while(x <= 10)
{
Syntax:
printf("%d\t", x);
variable initialization;
x++;
do
}
{
}
statements;
variable increment or decrement;
} while(condition) ;
Eg: i = 0 ;
Faculty: Ch.Lavanya Susanna, dept of cse
do
{
printf(“%d\t”, i);
i++; output:
} while(i<=5); 1 2 3 4 5
Flowchart:
Difference between while and do while loop
WHILE DO-WHILE
Condition is checked first Statement(s) are
then statement(s) is executed atleast
executed. once, there after
condition is
checked.
It might occur At least once the
statement(s) is executed statement(s) is
zero times, If condition is executed.
Working: false.
Initialization statement (counter variable) No semicolon at the end Semicolon at the
is executed only once
Next the body of do while loop is of while. end of while.
executed
Next condition is tested, if it is true then while(condition) while(condition);
do while statements are executed. The
condition of the loop is tested after the If there is a single Brackets are always
body of the loop is executed once; hence
it is called an exit -controlled loop. statement, brackets are not required.
Again control goes to test the condition.
This process is repeated until condition is required.
false
If condition is false, do while statements Variable in condition is variable may be
are not executed.
initialized before the initialized before or
A loop becomes an infinite loop if a
condition never becomes false
execution of loop. within the loop.
while loop is entry do-while loop is
Faculty: Ch.Lavanya Susanna, dept of cse
controlled loop. exit controlled loop. Next increment/decrement statement is
executed
Syntax:while(condition) do { statement(s); } Next condition is tested
This process is repeated until condition is
{ statement(s); } while(condition); false
A loop becomes an infinite loop if a
condition never becomes false
3.For loop: Repeats a set of statements a specific
number of times. It is a entry controlled loop as C program to find factorial of a number
condition is tested before for body.
#include<stdio.h>
Syntax: void main()
for( initialization ; condition ; {
increment/decrement) int num , fact=1 , i ;
{ printf("Enter a number\n");
statements; scanf("%d",&num);
} for(i=1; i<=num; i++)
Eg: {
for ( i= 1; i<= 5; i++) fact = fact * i;
{ }
printf(“%d\t”, i); printf("Factorial is %d\n",fact);
} }
Output:
enter a number
5
Factorial is 120
C program to find given number is prime
Flowchart: number or not
#include <stdio.h>
void main()
{
int i, num, count = 0;
printf(" enter a number \n");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
if(num%i==0)
{
count++;
}
Working: }
Initialization statement is executed only if(count==2)
once {
printf(“it is a prime number\n");
Next condition is tested if it is true then
for statements are executed }
else
Faculty: Ch.Lavanya Susanna, dept of cse
{ enter a number
printf(“it is not a prime number\n”); 5
} it is a prime number
}
Output:
Nested for loops: Nested for loops, i.e
one for loop inside another for loop.
Syntax:
for(initialization ; condition ;
increment/decrement)
{
for(initialization ; condition ;
increment/decrement)
{
Statement ;
}
}
Flowchart:
Working:
outer loop initialization statement
executes first then control goes to
condition .if condition is true then control
goes to inner loop.
Inner loop executes as long as its
condition is false.
Now control comes to outer loop
increment/decrement statement and then
condition is tested.
The above process continues until outer
loop condition is false
C program to print following pattern
12345
Faculty: Ch.Lavanya Susanna, dept of cse
12345 1
12345 12
12345 123
12345 1234
#include <stdio.h> 12345
void main() #include <stdio.h>
{ void main()
int i, j; {
for(i = 1; i <= 5; i++) int i, j;
{ for(i = 1; i <= 5; i++)
for(j = 1; j <= 5; j++) {
{ for(j = 1; j <= i; j++)
printf("%d\t ", j); {
} printf("%d\t ", j);
printf("\n"); }
} printf("\n");
} }
C program to print following pattern }
Event-Controlled Repetition
Sometimes, loop control may need to be based on the value of what we are processing. In this
case, we would event-controlled repetition. Event-controlled repetition is sometimes called
indefinite repetition because it is not known in advance how many times the loop will be
executed. It is a repetition procedure for solving a problem by using a event value (also called
a signal value, a dummy value or a flag value) to indicate "end of data entry". The event value
itself is not a part of the processed data.
An example of when we would use event-controlled repetition is when we are processing data
from a file and we do not know in advance when we would reach the end of the file.