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

  1. Decision-making statements

  2. Selection statements

  3. Iteration statements

  4. 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(condition) { statements } else { statements }
  • 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

#include<stdio.h> int main() { int a; printf("\nEnter a number:"); scanf("%d",&a); if(a>0) { printf("\nThe number %d is positive.",a); } else { printf("The number %d is negative.",a); } return 0; }

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

if(condition1) { // statements } else if(condition2) { // statements } else if(conditionN) { // statements } else { // statements }
  • 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

#include<stdio.h> int main() { int a,b,c; a=6,b=5,c=10; if(a>b) { if(a>c) { printf("Greatest is:%d",a); } else if(c>a) { printf("Greatest is:%d",c); } } else if(b>c) { printf("Greatest is:%d",b); } else { printf("Greatest is:%d",c); } return 0; }

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

switch(expression) { case value1: code segment1; break; case value2: code segment2; break; case valueN: code segmentN; break; default: default code segment; }
  • 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

#include<stdio.h> int main() { int day; printf("\nEnter the number of the 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; default: printf("Invalid choice"); } return 0; }

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

for(initialization; testExpression; update) { statements }

Example: Sum of first n natural numbers

#include<stdio.h> int main() { int num,count,sum=0; printf("Enter a positive integer: "); scanf("%d",&num); for(count=1; count<=num; ++count) { sum+=count; } printf("Sum=%d",sum); return 0; }

while Loop

Checks condition before executing the loop body.

Syntax

while(testExpression) { codes }

Example: Factorial of a number

#include<stdio.h> int main() { int number; long factorial=1; printf("Enter an integer: "); scanf("%d",&number); while(number>0) { factorial*=number; --number; } printf("Factorial=%lld",factorial); return 0; }

do...while Loop

Executes the body at least once, then checks condition.

Syntax

do { codes } while(testExpression);

Example: Add numbers until user enters zero

#include<stdio.h> int main() { double number,sum=0; do { printf("Enter a number: "); scanf("%lf",&number); sum+=number; } while(number!=0.0); printf("Sum=%.2lf",sum); return 0; }

4. Jump Statements

Jump statements change the normal flow of execution.

Types

  • break

  • continue

  • goto


break Statement

Terminates the loop immediately.

Example

#include<stdio.h> int main() { int i; double number,sum=0.0; for(i=1;i<=10;++i) { printf("Enter n%d: ",i); scanf("%lf",&number); if(number<0.0) break; sum+=number; } printf("Sum=%.2lf",sum); return 0; }

continue Statement

Skips remaining statements in the loop and proceeds to next iteration.

Example

#include<stdio.h> int main() { int i; double number,sum=0.0; for(i=1;i<=10;++i) { printf("Enter n%d: ",i); scanf("%lf",&number); if(number<0.0) continue; sum+=number; } printf("Sum=%.2lf",sum); return 0; }

goto Statement

Alters program sequence by jumping to a labeled statement.

Syntax

goto label; ... label: statement;

Example

#include<stdio.h> int main() { const int maxInput=5; int i; double number,average,sum=0.0; for(i=1;i<=maxInput;++i) { printf("%d. Enter a number: ",i); scanf("%lf",&number); if(number<0.0) goto label; sum+=number; } label: average=sum/(i-1); printf("Sum=%.2f\n",sum); printf("Average=%.2f",average); return 0; }

Note:

  • Using goto may make programs hard to read and debug.

  • Use it only if it clearly simplifies the program; otherwise, avoid it.



Sample Programs( all university questions)
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

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays