0% found this document useful (0 votes)
9 views46 pages

Control Statements in C Programming

The document provides an overview of control statements in C programming, detailing their types, including conditional (if-else, switch-case), looping (for, while, do-while), and jump statements (break, continue, return, goto). It explains the syntax and functionality of each statement with examples, illustrating how they direct the flow of execution in a program. Additionally, it covers the differences between various loop types and includes sample programs demonstrating their use.

Uploaded by

bilkeesmamoc1
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)
9 views46 pages

Control Statements in C Programming

The document provides an overview of control statements in C programming, detailing their types, including conditional (if-else, switch-case), looping (for, while, do-while), and jump statements (break, continue, return, goto). It explains the syntax and functionality of each statement with examples, illustrating how they direct the flow of execution in a program. Additionally, it covers the differences between various loop types and includes sample programs demonstrating their use.

Uploaded by

bilkeesmamoc1
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

Module 3

bk
Control Statements in C
• Control statements direct the order of
execution of instructions in a C program.
Without them, a program would run
sequentially from top to bottom.
Types of Control Flow statements in C
Programming:
Control Flow Statements Type Control Flow Statement Description

Types of Control Flow statements


if-else in Programming:
Executes a block of code if a specified
condition is true, and another block if the
condition is false.
Conditional(Selection) Statements

switch-case Evaluates a variable or expression and executes


code based on matching cases.

for Executes a block of code a specified number of


times, typically iterating over a range of values.

while Executes a block of code as long as a specified


Looping Statements condition is true.

Executes a block of code once and then repeats


do-while the execution as long as a specified condition is
true.

Terminates the loop or switch statement and


break transfers control to the statement immediately
following the loop or switch.

continue Skips the current iteration of a loop and


continues with the next iteration.
Jump Statements
return Exits a function and returns a value to the
caller.

Transfers control to a labeled statement within


goto the same function. (Note: goto is generally
discouraged due to its potential for creating
unreadable and error-prone code.)
Conditional(Selection)Statements in Programming:

• Conditional statements in programming are


used to execute certain blocks of code based
on specified conditions.
• They are fundamental to decision-making in
programs.
• Here are some common types of conditional
statements:
1. If Statement in C Programming:

• Executes a block of code if a condition is true.


• Syntax:

if (condition)
{
// statements if condition is true
}
• Example:
int x = 10;
if (x > 0)
{
printf("Positive number");
}
[Link]...else Statement
• Executes one block if the condition is true, otherwise another
block.
• Syntax:
if (condition)
{
// if true
}
else
{
// if false
}
• Example:
int age = 18;
if (age >= 18)
{
printf("Eligible to vote");
}
Else
{
printf("Not eligible to vote");
}
Nested if-else
• A nested if-else means placing one if or else statement inside another.
It is used when you need to check multiple levels of conditions.
• Syntax:
if (condition1)
{
if (condition2)
{
// statements if both condition1 and condition2 are true
}
else
{
// statements if condition1 is true but condition2 is false
}
}
Else
{
// statements if condition1 is false
}
• Example 1 – Simple Nested if-else
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num >= 0) { // first condition


if (num % 2 == 0) { // second condition
printf("The number is positive and even");
} else {
printf("The number is positive and odd");
}
} else {
printf("The number is negative");
}

return 0;
}
• Example 2 – Student Grade Classification
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);

if (marks >= 50) {


if (marks >= 90) {
printf("Grade A");
} else if (marks >= 75) {
printf("Grade B");
} else {
printf("Grade C");
}
} else {
printf("Fail");
}

return 0;
}
Switch Statement
• The switch statement in C is a multi-way
branch statement.
• It allows you to execute one block of code out
of many options based on the value of an
expression.
– Syntax
switch (expression)
{
case constant1:
// code block
break;

case constant2:
// code block
break;

case constant3:
// code block
break;

default:
// code block (if no case matches)
}
• Example: Simple Calculator

#include <stdio.h>
int main()
{
int choice, a = 10, b = 5;

printf("Choose an operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\
n4. Division\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Result = %d\n", a + b);
break;
case 2:
printf("Result = %d\n", a - b);
break;
case 3:
printf("Result = %d\n", a * b);
break;
case 4:
printf("Result = %d\n", a / b);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
#include <stdio.h>

int main() {
int marks;
printf("Enter your marks (0-100): ");
scanf("%d", &marks);

// divide marks by 10 to get range (e.g., 85 -> 8, 92 -> 9, etc.)


switch (marks / 10) {
case 10: // for 100
case 9: // 90–99
printf("Grade: A\n");
break;

case 8: // 80–89
printf("Grade: B\n");
break;
case 7: // 70–79
printf("Grade: C\n");
break;

case 6: // 60–69
printf("Grade: D\n");
break;

case 5: // 50–59
printf("Grade: E\n");
break;

default: // below 50
printf("Grade: F (Fail)\n");
}

return 0;
}
Looping Statements in C

while
do…while
for
Looping(Iteration) Statements in C
• Iteration statements, commonly known as
loops, are statements in programming used to
execute part of code repeatedly based on
condition or set of conditions.
• There are mainly three types of iteration
statements:
• while Loop
• do-While Loop
• for Loop
Entry Controlled Loops in Programming

• Entry controlled loops in programming


languages allow repetitive execution of a block
of code based on a condition that is checked
before entering the loop.

• Types of Entry Controlled Loops:


• for loop
• while loop
Exit Controlled loop in C
• The loop body executes at least once before
the condition is checked.
while loop
• The while loop in C allows a block of code to
be executed repeatedly as long as a given
condition remains true.
• It is often used when we want to repeat a
block of code till some condition is satisfied.
• It is entry controlled loop.
syntax
while(condition)
{
// statements
}
• Example
#include <stdio.h>
int main()
{
int i = 1;
while(i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
#include <stdio.h>

int main() {
int i = 1;

// Condition for the loop


while (i <= 5) {
printf("C programming\n");

// Increment i after each iteration


i++;
}

return 0;
}
Working of while Loop
• We can understand the working of the while
loop by looking at the above flowchart:
• STEP 1: When the program first comes to the
loop, the test condition will be evaluated.
• STEP 2A: If the test condition is false, the body
of the loop will be skipped program will
continue.
• STEP 2B: If the expression evaluates to true, the
body of the loop will be executed.
• STEP 3: After executing the body, the program
control will go to STEP 1. This process will
continue till the test expression is true.
Sum of First N Natural Numbers using While loop
#include <stdio.h>
int main()
{
int sum = 0, i = 1;
while (i <= 10) {
// Add the current value of i to sum
sum += i;

// Increment i
i++;
}
printf("%d", sum);
return 0;
}
do...while Loop in C

• do..while loop is an exit control loop.


• do...while loop is a type of loop that executes a
code block until the given condition is satisfied.
• Unlike the while loop, which checks the
condition before executing the loop,
• the do...while loop checks the condition after
executing the code block, ensuring that the code
inside the loop is executed at least once, even if
the condition is false from the start.
#include <stdio.h>

int main() {

// Loop variable declaration and initialization


int i = 0;

// do while loop
do {
printf("Programmingin C\n");
i++;
} while (i < 3);

return 0;
}
Syntax

do
{
// Body of the loop
// Update expression
} while (condition);
Working of do...while Loop
1. When the program control comes to the do...while
loop, the body of the loop is executed first and then
the test condition/expression is checked, unlike other
loops where the test condition is checked first. Due to
this property, the do...while loop is also called exit
controlled or post-tested loop.
2. When the test condition is evaluated as true,
the program control goes to the start of the loop and
the body is executed once more.
3. The above process repeats till the test condition is true.
4. When the test condition is evaluated as false, the
program controls move on to the next
statements after the do...while loop.
Examples of do while Loop

#include <stdbool.h>
#include <stdio.h>

int main()
{

// Declaring a false variable


bool c = false;

do {
printf("This is loop body.");

// False condition
} while (c);

return 0;
}
Difference between while and do...while Loop

while Loop do...while Loop

The test condition is checked before The test condition is checked after
the loop body is executed. executing the body.

The body of the do...while loop is


When the condition is false, the body executed at least once even when the
is not executed not even once. condition is false.

It is a type of pre-tested or entry- It is a type of post-tested or exit-


controlled loop. controlled loop.

Semicolon is not required. Semicolon is required at the end.


for Loop

• It is an Entry controlled loop.


• The syntax of the for loop is:
for (initializationStatement; testExpression;
updateStatement)
{
// statements inside the body of loop
}
How for loop works?

• The initialization statement is executed only once.


• Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is
terminated.
• However, if the test expression is evaluated to
true, statements inside the body of the for loop
are executed, and the update expression is
updated.
• Again the test expression is evaluated.
for loop Flowchart
#include <stdio.h>

int main() {
int i;

// Print numbers from 1 to 10 using a for loop


for(i = 1; i <= 10; i++) {
printf("%d\n", i);
}

return 0;
}
Reverse counting from 10 to 1
#include <stdio.h>

int main() {
int i;

for(i = 10; i >= 1; i--) {


printf("%d\n", i);
}

return 0;
}
Sample Programs

PRACTICAL
Program: Declare and Initialize Variables
#include <stdio.h>
int main()
{
// Declaring and initializing variables of different data types
int age = 25; // integer
float height = 5.9; // float
char grade = 'A'; // character
double pi = 3.14159; // double (more precision)

// Printing values
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Value of Pi: %.5lf\n", pi);

return 0;
}
[Link] a C program to check whether a given number is even or odd

#include <stdio.h>

int main() {
int num;

// Input from user


printf("Enter a number: ");
scanf("%d", &num);

// Check even or odd using if


if (num % 2 == 0) {
printf("%d is Even\n", num);
}
else
{
printf("%d is Odd\n", num);
}

return 0;
}
Program: Day of the Week
#include <stdio.h>

int main() {
int day;

// Input a number (1–7)


printf("Enter a number (1-7): ");
scanf("%d", &day);

// Switch statement for days


switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:

printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input! Please enter 1–7.\n");
}

return 0;
}

You might also like