0% found this document useful (0 votes)
8 views7 pages

C Programming Control Statements Explained

Uploaded by

muskansahani450
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

C Programming Control Statements Explained

Uploaded by

muskansahani450
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Explain For Loop statement with syntax and example program.

• A for loop is used to repeat a set of statements multiple times.


• It is used when the number of repetitions is known.
• Syntax: for (initialization; condition; increment/decrement) {}

Example Program:

#include <stdio.h>

int main() {

int i;

for (i = 1; i <= 5; i++) {

printf("%d", i);

} return 0;

Output : 1 2 3 4 5

Write a C program to find roots of quadratic equation.


#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2, realPart, imagPart;

printf("Enter coefficients a, b, c: ");

scanf("%f %f %f", &a, &b, &c);

discriminant = b*b - 4*a*c;

if (discriminant > 0) {

// Two real and distinct roots

root1 = (-b + sqrt(discriminant)) / (2*a);

root2 = (-b - sqrt(discriminant)) / (2*a);

printf("Roots are real and different:\n");

printf("Root1 = %.2f\nRoot2 = %.2f\n", root1, root2);

else if (discriminant == 0) {

// Two real and equal roots


root1 = root2 = -b / (2*a);

printf("Roots are real and equal:\n");

printf("Root1 = Root2 = %.2f\n", root1);

else {

// Complex roots

realPart = -b / (2*a);

imagPart = sqrt(-discriminant) / (2*a);

printf("Roots are complex:\n");

printf("Root1 = %.2f + %.2fi\n", realPart, imagPart);

printf("Root2 = %.2f - %.2fi\n", realPart, imagPart);

return 0;

Output :

Enter coefficients a, b, c: 1 -5 6

Roots are real and different:

Root1 = 3.00

Root2 = 2.00

Explain switch statement with example


A switch statement is used to execute one block of code from many options.

It is an alternative to multiple if-else statements.

Works with integer, char, or enum values (not float or double).

Program : Calculator

#include <stdio.h>

int main() {

int num1, num2, result;

char operator;

printf("Enter first number: ");

scanf("%d", &num1);
printf("Enter second number: ");

scanf("%d", &num2);

printf("Enter operator (+ or -): ");

scanf(" %c", &operator);

switch(operator) {

case '+':

result = num1 + num2;

printf("Result: %d", result);

break;

case '-':

result = num1 - num2;

printf("Result: %d", result);

break;

default:

printf("Invalid operator!");

} return 0;

Output : Input: 8 5 +

Result: 13

Explain break and continue statements with examples for each.


Break Statement

What it does: Immediately exits the loop.

• Stops the current loop even if the condition is still true.

Syntax: break;

Example : Print numbers from 1 to 5, but stop if number is 3:

#include <stdio.h>

int main() {

int i;

for(i = 1; i <= 5; i++) {

if(i == 3) {
break; ( exit the loop when i is 3)

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

} return 0;

Output: 1 2

Continue Statement

What it does: Skips the current iteration of the loop and moves to the next iteration.

• The loop does not end.

Syntax: continue;

Example : Print numbers from 1 to 5, but skip 3:

#include <stdio.h>

int main() {

int i;

for(i = 1; i <= 5; i++) {

if(i == 3) {

continue; (skip when i is 3)

printf("%d\", i);

} return 0;

Output: 1 2 4 5

Write a C program to find the largest three numbers using nested if statement.
#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);


// Nested if to find the largest

if (a > b) {

if (a > c) {

printf("Largest number = %d\n", a);

} else {

printf("Largest number = %d\n", c);

} else {

if (b > c) {

printf("Largest number = %d\n", b);

} else {

printf("Largest number = %d\n", c);

} } return 0;

Output :

Enter three numbers: 12 45 32

Largest number = 45

Explain different types of operators with example for each.


Arithmetic → for calculations (add, subtract, multiply, divide, remainder).

Operator Meaning Example (a=10,b=3) Result

+ Addition a+b 13

- Subtraction a-b 7

* Multiplication a*b 30

/ Division a/b 3

% Modulus a%b 1

Relational → for comparing two values (greater, smaller, equal, not equal).
Operator Meaning Example (a=5,b=10) Result

== Equal to a == b 0 (False)

!= Not equal to a != b 1 (True)

> Greater than a>b 0

< Less than a<b 1

>= Greater or equal a >= b 0

<= Less or equal a <= b 1

Logical → for checking conditions (AND, OR, NOT).

Operator Meaning Example

&& Logical AND (5>2 && 3>1) → 1

|| Logical OR (5>2 || 3>1) → 1

! Logical NOT !(5>2) → 0

Bitwise → works on bits (AND, OR, shift left/right).

Operator Meaning Example

& Bitwise AND 5&3→1

| Bitwise OR 5|2=7

^ Bitwise XOR 5^3→6

~ Bitwise NOT ~5 → -6

<< Left Shift 5 << 1 → 10

>> Right Shift 5 >> 1 → 2

Conditional → It is a shortcut for if-else statements.

It chooses one value based on a condition.

• Syntax: result = (condition) ? value_if_true : value_if_false;

• Example : “I will take an umbrella if it is raining, otherwise I won’t.”

o umbrella = (raining) ? 1 : 0;
o If raining = true → umbrella = 1

o If raining = false → umbrella = 0

You might also like