Control Statements in C
Control Statements in C
Control statements enable us to specify the flow of program control, i.e., the order in which the instructions in a program are executed. They make it possible to:
-
Make decisions
-
Perform tasks repeatedly
-
Jump from one section of code to another
Types of Control Statements in C
-
Decision-making statements
-
Selection statements
-
Iteration statements
-
Jump statements
1. Decision-Making Statement: The if–else Statement
The if–else statement is used to carry out a logical test and then take one of two possible actions depending on whether the outcome is true or false.
Syntax
-
If the condition evaluates to true, the statements inside the if-block execute.
-
If the condition is false, the else-block executes (if present).
-
Control then transfers to the statement immediately after the block.
-
The condition must always be inside parentheses.
-
It is good practice to use braces, even for a single statement.
Example: Check whether a number is positive or negative
Nested if and if–else Statements (if–else ladder)
We can embed or nest if–else statements within one another. This is useful when several different courses of action must be selected.
General Format
-
As soon as one condition becomes true, its block executes.
-
Remaining conditions are skipped.
-
If none are true, the else-block executes (if present).
Example: Greatest of three numbers
2. Selection Statement: The switch–case Statement
A switch statement is used for multiple-way selection based on the value of an expression or variable (must be integer type).
Syntax
-
The matching case executes.
-
default runs if no case matches.
-
break prevents execution from continuing into the next case.
Example: Print day of the week
Notes
-
switch can only test equality.
-
if–else can evaluate any Boolean expression.
-
switch is often faster for many choices.
-
Commonly used in menu-based programs.
3. Iteration Statements (Loops)
Loops repeat a block of code until a condition is met.
Types of loops in C
-
for loop
-
while loop
-
do...while loop
for Loop
Used when the number of iterations is known.
Syntax
Example: Sum of first n natural numbers
while Loop
Checks condition before executing the loop body.
Syntax
Example: Factorial of a number
do...while Loop
Executes the body at least once, then checks condition.
Syntax
Example: Add numbers until user enters zero
4. Jump Statements
Jump statements change the normal flow of execution.
Types
-
break
-
continue
-
goto
break Statement
Terminates the loop immediately.
Example
continue Statement
Skips remaining statements in the loop and proceeds to next iteration.
Example
goto Statement
Alters program sequence by jumping to a labeled statement.
Syntax
Example
Note:
-
Using
gotomay make programs hard to read and debug. -
Use it only if it clearly simplifies the program; otherwise, avoid it.
1.Print factors of a number
#include <stdio.h>
main()
{
int n,i;
printf("Enter the number...\n");
scanf("%d",&n);
printf("Factors of the number\n");
for(i=1;i<=n;i++)
if(n%i==0)
printf("%d\n",i);
}
Note: an efficient way is to go up to n/2
2.Sum of the digits of a number
#include <stdio.h>
main()
{
int n,i,s=0,d;
printf("Enter the number...\n");
scanf("%d",&n);
while(n!=0)
{d=n%10;
s=s+d;
n=n/10;
}
printf("Sum of the digits of the number=%d\n",s);
}
3.Check for perfect number ( Eg: 6,28,496 etc..)
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,s=0;
printf("Enter the number\n");
scanf("%d",&n);
for(i=1;i<=n/2;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
printf("%d is a pefect number \n",n);
else
printf("%d is a not a pefect number \n",n);
}
4.Check for prime ( Eg:2,3,5,7,11 etc...)
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,prime=1;
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{prime=0;break;}
}
if(prime)
printf("%d is a prime number \n",n);
else
printf("%d is a not a prime number \n",n);
}
5.Fibonacci series up to 100(0 1 1 2 3 5 8 13.....)
#include <stdio.h>
#include <string.h>
int main()
{
int a=0,b=1,c;
printf("Fibonacci series up to 100\n");
printf("%4d%4d",a,b);
while(1)
{
c=a+b;
if(c>100) break;
a=b;
b=c;
printf("%4d",c);
}
}
Comments
Post a Comment