Module Ii
Module Ii
Module Ii
Control flow: If statement, if….else statement, nested if ..else statement, switch statements, looping
– for loop , while loop, do … while statements, nested loop structure, break, continue and go to
statements.
if statement
switch statement
conditional operator statement (? : operator)
goto statement
The if statement may be implemented in different forms depending on the complexity of conditions
to be tested. The different forms are,
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
5. Simple if statement
Simple if statement
if(expression)
statement x;
If the expression returns true, then the statement-inside will be executed, otherwise statement-
inside is skipped and only the statement-outside is executed.
Example:
#include <stdio.h>
void main( )
int x, y;
x = 15;
y = 13;
if (x > y )
}
#include<stdio.h>
void main()
int number;
printf("Type a number:");
scanf("%d", &number);
if (number < 0)
number = -number;
if...else statement
if(expression)
else
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and
statement-block2 is executed.
Example:
#include <stdio.h>
void main( )
int x, y;
x = 15;
y = 18;
if (x > y )
else
}
}
#include<stdio.h>
void main() {
int num;
scanf("%d", &num);
if (num < 0)
else
#include <stdio.h>
#include<conio.h>
void main() {
int num;
scanf("%d", &num);
if(num % 2 == 0)
else
getch();
if(test condition2 )
{
statement block1;
else
statement block2;
else
statement block3;
Ststement x;
if expression is false then statement-block3 will be executed, otherwise the execution continues and
enters inside the first if to perform the check for the next if block, where if expression 1 is true the
statement-block1 is executed otherwise statement-block2 is executed.
Example:
#include <stdio.h>
void main( )
int a, b, c;
printf("Enter 3 numbers...");
if(a > b)
if(a > c)
else
else
if(b > c)
else
}
Switch statement
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the
case match is found, a block of statements associated with that particular case is executed.
Syntax:
switch( expression )
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
Statement-x;
Value-1, 2, n are case labels which are used to identify each case individually. Remember that case
labels should not be same as it may create a problem while executing a program. Case labels always
end with a colon ( : ). Each of these cases is associated with a block.
Whenever the switch is executed, the value of test-expression is compared with all the cases which
we have defined inside the switch. Suppose the test expression contains value 4. This value is
compared with all the cases until case whose label four is found in the program. As soon as a case is
found the block of statements associated with that particular case is executed and control goes out
of the switch.
The break keyword in each case indicates the end of a particular case. The default case is an optional
one. Whenever the value of test-expression is not matched with any of the cases inside the switch,
then the default will be executed. Otherwise, it is not necessary to write default in the switch.Once
the switch is executed the control will go to the statement-x, and the execution of a program will
continue.
Program to find the given number is even or odd
#include<stdio.h>
#include<conio.h>
void main()
scanf("%d", &num);
rem = num%2;
switch(rem)
break;
break;
getch(); }
Program to print week days
#include<stdio.h>
#include<conio.h>
void main()
int day;
scanf("%d", &day);
switch(day)
case 1: printf("SUNDAY.");
break;
case 2: printf("MONDAY.");
break;
case 3: printf("TUESDAY.");
break;
case 4: printf("WEDNESDAY.");
break;
case 5: printf("THURSDAY.");
break;
case 6: printf("FRIDAY.");
break;
case 7: printf("SATURDAY.");
break;
break;
getch();
}
program to implement simple calculator using switch statement
#include<stdio.h>
#include<conio.h>
void main()
float a,b, r;
char op;
printf("Available Operations.\n");
printf("Which Operation?\n");
scanf("%c", &op);
switch(op)
case '+':
scanf("%f%f",&a, &b);
r = a+b;
break;
case '-':
scanf("%f%f",&a, &b);
r = a-b;
break;
case '*':
scanf("%f%f",&a, &b);
r = a*b;
break;
case '/':
scanf("%f%f",&a, &b);
if(b!=0)
r = a/b;
printf("%f/%f = %f",a,b,r);
else
break;
break;
getch();
Looping
Looping Statements in C execute the sequence of statements many times until the stated condition
becomes false. A loop in C consists of two parts, a body of a loop and a control statement. The
control statement is a combination of some conditions that direct the body of the loop to execute
until the specified condition becomes false. The purpose of the C loop is to repeat the same code a
number of times.
Types of Loops in C
Depending upon the position of a control statement in a program, looping statement in C is classified
into two types:
In an entry control loop in C, a condition is checked before executing the body of a loop. It is also
called as a pre-checking loop.
2. Exit controlled loop
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called
as a post-checking loop.
The specified condition determines whether to execute the loop body or not.
while Loop
A while loop is the most straightforward looping structure. While loop syntax in C programming
language is as follows:
Syntax
while (condition)
statements;
It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the
loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop
is executed then control again goes back at the beginning, and the condition is checked if it is true,
the same process is executed until the condition becomes false. Once the condition becomes false,
the control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately after the loop. The
body of a loop can contain more than one statement
Program to print 1 to 10
#include<stdio.h>
#include<conio.h>
void main()
int num=1;
while(num<=10)
{
printf("%d\n",num);
num++;
getch();
#include <stdio.h>
#include<conio.h>
void main() {
int n, i, sum = 0;
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
getch();
#include<stdio.h>
#include<conio.h>
void main()
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
printf("Sum is=%d",sum);
getch();
Revrese a number
#include<stdio.h>
#include<conio.h>
void main()
scanf("%d", &n);
while(n!=0)
rem=n%10;
reverse=reverse*10+rem;
n/=10;
getch();
#include<stdio.h>
#include<conio.h>
void main()
int n,r,sum=0,temp;
temp=n;
while(n>0)
r=n%10;
sum=sum*10 + r;
n=n/10;
if(temp==sum)
else
printf("not palindrome");
getch();
do-While loop in C
A do…while loop in C is similar to the while loop except that the condition is always executed after
the body of a loop. It is also called an exit-controlled loop.
Syntax
do
statements
} while (expression);
Here, the body is executed only if the condition is true. In some cases, we have to execute a body of
the loop at least once even if the condition is false. This type of operation can be achieved by using a
do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After the body is executed,
then it checks the condition. If the condition is true, then it will again execute the body of a loop
otherwise control is transferred out of the loop.
#include<stdio.h>
#include<conio.h>
void main()
int num=1;
do
printf("%d\n",2*num);
num++;
}while(num<=10);
getch();
for loop
A for loop is a more efficient loop structure in ‘C’ programming. The general structure of for loop
syntax in C is as follows:
Syntax
statements;
#include<stdio.h>
#include<conio.h>
void main()
int number;
for(number=1;number<=10;number++)
{
printf("%d\n",number);
getch();
#include <stdio.h>
#include<conio.h>
void main() {
int i, j;
int table = 2;
int max = 5;
printf("\n");
getch(); }
#include <stdio.h>
#include<conio.h>
void main()
int n, i, sum = 0;
scanf("%d", &n);
sum += i;
}
getch();
Factorial of a number
#include <stdio.h>
#include<conio.h>
void main()
int i,fact=1,number;
scanf("%d",&number);
for(i=1;i<=number;i++)
fact=fact*i;
getch();
Fibonacci series
#include<stdio.h>
#include<conio.h>
void main()
int n1=0,n2=1,n3,i,number;
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
getch();
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any
number of loops. The nesting level can be defined at n times.
Outer_loop
Inner_loop
The nested for loop means any type of loop which is defined inside the 'for' loop.
#include <stdio.h>
int main()
int n;
for(int i=1;i<=n;i++)
for(int j=1;j<=10;j++)
printf("%d\t",(i*j)); }
printf("\n");
The break; continue; and goto; statements are used to alter the normal flow of a program.
Loops perform a set of repetitive task until text expression becomes false but it is sometimes
desirable to skip some statement/s inside loop or terminate the loop immediately without checking
the test expression. In such cases, break and continue statements are used.
break Statement
Break statement is used to break the process of a loop (while, do while and for) and switch case.
Syntax:
break;
Example
while(test Expression)
Statements;
break;
}
Statements;
continue Statement
The Continue statement is used to continue the execution of the loop. It is used inside if condition.
We can use it with while, do while and for loop.
Syntax:
continue;
example
#include <stdio.h>
void main(){
int i;
for(i=1;i<=5;i++){
if(i==2)
continue;
printf("%d \n",i);
goto Statement
Goto statement is used to transfer the unconditional program control to a labeled statement.
Syntax:
Goto identifier;
Example:
#include <stdio.h>
void main(){
goto label;
printf("How Are You?"); // skipped
label: