0% found this document useful (0 votes)
49 views67 pages

Unit 3

This document discusses various conditional and looping control statements in C programming. It covers simple if, if-else, else-if statements, switch-case statements, break, continue, goto statements, for, while, do-while loops and nested loops. Examples are provided for each statement type to illustrate their usage. Key aspects like conditions, syntax, and flow of execution are explained for different conditional and looping constructs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views67 pages

Unit 3

This document discusses various conditional and looping control statements in C programming. It covers simple if, if-else, else-if statements, switch-case statements, break, continue, goto statements, for, while, do-while loops and nested loops. Examples are provided for each statement type to illustrate their usage. Key aspects like conditions, syntax, and flow of execution are explained for different conditional and looping constructs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Sathyabama Institute of Science and Technology

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:

1) Check whether the given number is Odd

2) To check whether the given number is


Greater

3) To check whether the given number is


Smaller

4) To check whether the given number is


positive

5) To check whether the given number is


negative

6) To check whether the given number is


Control Statements Contd...
ii. If else statement
❑ Extension of basic if statement
❑ Takes care of True and False condition
❑ Number of Statement Blocks - 2
❑ Block 1 – True Condition
❑ Block 2 – False Condition
if (condition)
{
Statements;
}
Else
{
Statements;
}
/* if else –To check whether a number is Odd or
Even*/

#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:

1) To check whether the given number is Greater or


Smaller

2) To check whether the given number is +ve or -ve

3) To check whether two numbers are equal or not


2. 1 Control Statements Contd...
iii. Nested if else statement
❑ Used when a series of decisions
are involved
❑ Makes a choice between
several alternatives
❑ New if else statement block is
used within existing if else
statement block
/*Program for Nested if else */

#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

❑ if it true it will print 'login successful’


❑ else it will execute block statement 'Password is Incorrect,
Try again.‘.
❑ Step 2: If the first if condition is false then it executes last else
block thus printing 'Username is Incorrect, Try again.‘
Control Statements Contd...
❑ Step 3: In this above example we have use username as single
character to use multiple character username we need to
use string data type
else if Ladder
/*Program for if else ladder*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a Number: ");
scanf("%d",&a);
if(a > 0)
{
printf("Given Number
is Positive");
}
else if(a == 0)
{
printf("Given Number
is Zero");
}
else if(a < 0)
{
printf("Given Number
is Negative");
2. 1 Control Statements Contd...
b) Switch statement

❑ Allows to make decisions from a number of choices


❑ Also called as Switch-Case-Default Statement
❑ Faster than nested if else statement
❑ Easier to understand
❑ Rules for writing switch ( ) statement

❑ Expression in switch must be an integer value


or a character constant
❑ No real numbers used in Expression
Control Statements Contd...

❑ Each case block anddefault block must endwith


break
statements
❑ Default is
optional
❑ Case
keyword
must end
with colon
(:)
❑ Default
may be
Control Statements Contd...

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( );
}

Enter two numbers


20
10
Enter 1 for Addition
Enter 2 for Subtraction
Enter 3 for Multiplication
Enter 4 for Division
Enter your Choice: 3
Product is : 200
Control Statements Contd...
❑ Nested Switch statement
❑ Inner switch( ) can be a part of an outer switch( )
❑ Inner switch( ) and outer switch( ) case constants may be the

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”);

printf(“ 3. Find Odd or Even \n”);


printf(“ 4. Exit \n”);
printf(“ Enter your Choice”);
switch (choice)
{
case 1:
square = n * n;
printf(“TheSquare of the Given number is
%d\n”,
square);
break;
case 2:
for(i=1;i<=n;i++)
{
fact = fact * i;
}
printf(“The Factorial of a given number is %d\n”, fact);
break;
switch (n%2)
{
case 0:
printf(“Given Number is
Even\n”); case 1:
printf(“Given Number is Odd\n”);
}
case 3:
exit(0);

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

The break statement ends the loop


immediately when it is encountered.

Its syntax is: break;

The break statement is almost alwa ys


used with if...else statement inside th e
loop.
2.1 Continue
The continue statement skips the
current iteration of the loop and
continues with the next iteration.

Its syntax is: continue;

The continue statement is almost


always used with the if...else
statement.
2. 2 Looping Statements
❑ Loop – A segment of the program that is executed repeatedly
until a condition is satisfied
❑ Classification – Entry Controlled & Exit Controlled
❑ Types
a) while do loop
b) do while loop
c) for loop
i. Nested for loop
2. 2 Looping Statements Contd...
a) The While Loop

❑ Simplest looping structure in C


❑ Statementsin the program may to repeat for
need

many times. e.g., calculate the value of n!


❑ Loop consists of two segments
❑ Control Statement
❑ Body of the Loop
❑ How while loop works?
Initialize loop counter variable;
while (condition)
{
Statements;
increment / Decrement loop
counter variable;
}
/* Program to Add 3
Numbers*/

#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:

1) To print all even numbers from 1 to 100

2) To print all even numbers from 1 to n

3) To print table for any number

4) To calculate the sum of its digits

5) To check whether the entered number is Prime or not

6) To get a number as input and print it in reverse.


2. 2 Looping Statements Contd...
b) The Do While Loop
❑ The body of the loop is executed at least once
❑ Syntax
do
{
statements;
}
while (condition);
Initialize loop counter

variable; do

{
Statements;
increment / Decrement loop
counter variable;
}
while (condition)
2. 2 Looping Statements Contd...

While Do Loop Do While Loop

Entry Controlled Loop Exit Controlled Loop

Test condition is checked Test condition is checked after the


before body of the loop is executed body of the loop is executed

Loop will not be executed if Loop will be executed at least once


condition is false even if condition is false

Top tested loop Bottom tested loop


2. 2 Looping Statements Contd...
2. 2 Looping Statements Contd...
c) The for loop
❑ Most commonly and popularly used loop structure
❑ Structure of the for loop
❑ Initialize loop counter variable
❑ Check for condition
❑ Increment / Decrement the loop counter variable
❑ Syntax
for(initialization; condition; increment / decrement)
2. 2 Looping Statements Contd...
Looping Statements Contd...
❑ Examples
i. for(i = 0; i < n; i++)
{
Statements;
}
ii. for(count = 0; count > n; count--)
{
Statements;
}
/* Program to Add n Numbers using for loop
*/

#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:

1) To print all even numbers from 1 to 100

2) To print all even numbers from 1 to n

3) To print table for any number

4) To calculate the sum of its digits

5) To check whether the entered number is Prime or not

6) To get a number as input and print it in reverse.


THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy