Unit 3
Unit 3
CHENNAI.
S11BLH11– Programming in C
Unit III
UNIT III
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if - Conditional
Statements : Switch case - Un-conditional Control Statements :
break, continue, goto - Looping Control Statements:for, while,
do..while - Looping Control Statements: nested for, nested
while
1. Control Statements
❑ Also called as Conditional Statement
❑ Decides order of execution based on conditions
❑ Helps repeat a group of statements
❑ Modifies control flow of program
❑ Decision Making
❑ Branching
2. 1 Control Statements Contd...
❑ Types of Branching Statements
a) if statement
i. Simple if
ii. if…else statement
iii. nested if…else statement
iv. else…if statement
b) switch statement
c) goto statement
1. 1 Control Statements Contd...
a) if statement
❑ Condition “True" - Statement block will be executed
❑ Condition “False“ - Statement block will not be
executed.
❑ Variations
i. Simple if
ii. if…else statement
iii. nested if…else statement
iv. else…if statement
2. 1 Control Statements Contd...
i. Simple if statement
❑ Basic if statement
❑ What is a condition?
❑ Executes statement block only if condition is true
❑ Syntax
if (condition)
{
Statements;
}
/* Simple if – Program to check whether a number is
Odd*/ #include<stdio.h>
int main( )
{
int number;
printf(“Enter the Number:
”); scanf(“%d,
&number);
if(number%2==0)
{
printf(“The Number is
Even”);
}
Output
return 0;
Enter a value :
}
10342 The
number is Even
Control Statements Contd...
❑ Try it Out Yourself ! Write a C program
to:
#include<stdio.h>
int main( )
{
int number;
printf(“Enter
the Number:
”);
scanf(“%d, &number);
if(number%2==0)
{
printf(“The
Number is Even”);
}
else
{
Output 1
Enter the Number : 10341
The number is Odd
Output 2
Enter the Number : 10342
The number is Even
Control Statements Contd...
❑ Try it Out Yourself ! Write a C program to:
#include <stdio.h>
void main( )
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password)
if(username=='a')
{
if(password==12345)
{
printf("Login successful");
}
else
{
printf("Password is incorrect, Try
again.");
}
}
else
{
printf("Username is incorrect, Try
again.");
}
return 0;
Output 1
Username: a
Password:
12345 Login
Successful
Output 2
Username: a
Password:
54321
Password is
incorrect,
Try again.
Output 3
Control Statements Contd...
❑ Step 1: First if condition will be true, if the user has typed 'a' as a
username then the program control moves to second if condition
and checks for the password
switch(variable or expression)
{
case constant 1:
statements;
break;
….
case constant N;
statements;
break;
default:
s
t
a
t
/* Program for Switch
Case*/
#include<stdio.h>
int main( )
{
int a, b,
choice;
printf(“\
nEnter Two
Numbers:
”); scanf(“%d
%d”, &a,&b);
printf(“\n Enter 1 for Addition”);
printf(“\n Enter 2 for
switch (choice)
{
case 1:
printf(“Sum is : %d”,
a+b); break;
case 2:
printf(“Difference is : %d”,
a-b); break;
case 3:
printf(“Multiplication is : %d”,
a*b); break;
case 4:
printf(“Difference is : %d”,
a/b); break;
default:
printf(“Invalid Choice:”);
}
getch( );
}
same
/* Program for Nested Switch
Case*/
#include<stdio.h>
int main( )
{
int square, i, n, fact = 1,choice;
printf(“\n Enter Any Number: ”);
scanf(“%d”, &n);
printf(“ 1. Square \n”);
printf(“ 2. Factorial
\n”);
default:
printf(“Invalid Choice. Please try
again\n”);
}
return 0;
}
Enter any number
5
1. Square
2. Factorial
3. Find Odd or Even
4. Exit
Enter your choice
2
The factorial of a
given number is:
120
2. 1 Control Statements Contd...
c) The goto statement
❑ Transfers control from one point to another
❑ Syntax
goto label;
statements;
……………
label
statements;
2.1 break
#include<stdio.h>
int main( )
{
int a, b, c,
sum;
printf(“\n
Enter the
Three
Numbers: ”);
scanf(“%d%d
%d”,
&a,&b,&c);
sum = a+b+c;
printf(“The
sum of 3
/* Program to Add n Numbers*/
#include<stdio.h>
int main( )
{
int i=1,n,
sum=0;
printf(“\n
Enter the
value for n: ”);
scanf(“%d”, &n);
while (i<=n)
{
sum = sum +
i;
i++;
}
Output
Enter the value for n: 5
The sum of n Numbers is: 15
2. 2 Looping Statements Contd...
❑ Try it Out Yourself ! Write a C program to:
variable; do
{
Statements;
increment / Decrement loop
counter variable;
}
while (condition)
2. 2 Looping Statements Contd...
#include<stdio.h>
int main( )
{
int i, n,
sum=0;
printf(“\n
Enter the
value for n: ”);
scanf(“%d”, &n);
for (i =1; i<=n; i++)
{
sum = sum + i;
}
printf(“The sum of n Numbers is: %d”, sum);
Output
Enter the value for n: 5
The sum of n Numbers is: 15
2. 2 Looping Statements Contd...
❑ nested for syntax
2. 2 Looping Statements Contd...
❑nested for Example Program
#include<stdio.h>
#include<conio.h>
main()
{
Int row, column;
for(row=1;row<=5;row++)
{
for(column=1;column<=ro
w;column++)
{
2. 2 Looping Statements Contd...
❑nested for Example Program
print(“*”);
}
print(“\n”);
}
getch();
Output:
*
**
***
****
*****
Looping Statements Contd...
❑nested for Example Program
#include<stdio.h>
#include<conio.h>
main()
{
Int ro,sp,st,no;
printf(“Enter no. of rows you want to print\n”);
scanf(“%d”, &no);
for(ro=1;ro<=no;ro++)
{
Looping Statements Contd...
❑nested for Example Program
for(sp=no-ro;sp>=1;sp--)
printf(“ “);
for(st=1;st<=2*ro-1;st++)
printf(“*”);
printf(“\n”);
}
for(ro=no-1;ro>=1;ro--)
{
for(sp=1;sp<=no-ro;sp++)
printf(“ “ );
2. 2 Looping Statements Contd...
❑nested for Example Program
for(st=1;st<=2*ro-1;st++)
printf(“\n”);
}
getch();
}
2. 2 Looping Statements Contd...
Output
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
2. 2 Looping Statements Contd...
❑ nested while loop
• Using while loop within while loop is said to be nested while
loop.
• It is used to execute a set of code for multiple times as long as
the condition is true.
• Once the condition becomes false the code block is no longer
executed.
Looping Statements Contd...
❑ Syntax
2. 2 Looping Statements Contd...
#include<stdio.h>
int main()
{
int row, j;
for(row=’A’;row<=’D’;row++)
{
for(j=’A’;j<=row;j++)
{
printf(“%c”, j);
}
printf(“\n);
}
return 0;
}
Looping Statements Contd...
❑ Output
A
AB
ABC
ABC
D
Looping Statements Contd...
#include<stdio.h>
int main()
{
int row, j;
for(row=1;row<=5
;row++)
{
for(j=1;j<=5;j+
+)
{
printf(“%
d”,j);
}
printf(“\n”);
}
2. 2 Looping Statements Contd...
Output:
12345
12345
12345
Looping Statements Contd...
❑ Try it Out Yourself ! Write a C program to: