PROGRAMMING IN C
C Flow of Control Statements
C has the following conditional statements:
Decision Control Statements
switch...case
if...else Statement
if
else
else if
Loop control statements
for Loop
while Loop
Do while loop
break and continue
Control statements allow programmers to control the flow of execution of their
programs.
They allow a programmer to specify conditions that determine which statements are
executed and which are skipped, to loop through statements until a condition is met,
or to jump to a different part of the program.
There are three types of control statements:
selection statements
iteration statements, and
jump statements.
characteristics:
Control statements serve to control the execution flow of a program.
Control statements in C are classified into selection statements, iteration
statements, and jump statements.
Selection statements serve to execute code based on a certain circumstance.
Iteration statements loop through a code block until a condition is met.
Jump statements serve to move control from one section of a program to
another.
The if statement, for loop, while loop, switch statement, break statement,
and continue statement are C's most widely used control statements
Usage:
Control statements are used extensively in programming, especially in more
complex programs.
By managing the flow of execution based on particular conditions, they
enable a programmer to design more efficient and understandable code.
Example of how control statements can
be used in C:
1. #include <stdio.h> 9. for (int i = 0; i < 5; i++) {
2. int main() { 10. printf("%d\n", i);
3. int num = 10; 11. }
4. if (num > 0) { 12. int i = 0;
5. printf("The number is positive\n"); 13. while (i < 5) {
6. } else { 14. printf("%d\n", i);
7. printf("The number is negative\n"); 15. i++;
8. } 16. }
Example of how control statements can
be used in C:…
17. switch (num) {
Output
18. case 0:
19. printf("The number is zero\n"); The number is positive
20. break; 0
21. case 10: 1
22. printf("The number is ten\n"); 2
23. break; 3
24. default: 4
25. printf("The number is not zero or ten\n"); 0
26. break; 1
27. } 2
28. return 0; 3
29. } 4
The number is ten
Advantages
Control statements allow programmers to control the flow of execution in
their programs, enabling it to be simple to develop efficient and
understandable code.
They allow programmers to handle different scenarios and conditions in their
programs.
Disadvantages
Overuse of control statements can make code difficult to read and maintain.
Complex control statements can be difficult to debug and can introduce bugs
in the code.
.
if Statement
Use the if statement to specify a block of code to be executed
if a condition is true
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Flowchart of if statement
Example: if statement
if (20 > 18) {
printf("20 is greater than 18");
}
Example: if statement…
1. #include<stdio.h>
2. void main()
3. {
4. int a = 15, b = 20;
5. if (b > a) {
6. printf("b is greater");
7. }
8. }
Example: if statement…
1. #include<stdio.h>
2. void main()
3. {
4. int number;
5. printf("Type a number:");
6. scanf("%d", &number);
7. if (number < 0) { // check whether the number is negative number.
8. number = -number; // If it is a negative then convert it into positive.
9. printf("The absolute value is %d\n", number);
10. }
11. }
The if else Statement or else Statement
Use the else statement to specify a block of code to be executed if the
condition is false.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Flowchart of if-else Statement
Example: else statement
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."
Example: if else statement…
#include<stdio.h>
void main()
{
int a, b;
printf("Please enter the value for a:");
scanf("%d", &a);
printf("\nPlease the value for b:");
scanf("%d", &b);
if (a > b) {
printf("\n a is greater");
} else {
printf("\n b is greater");
}
}
Example: if else statement…
#include<stdio.h>
void main() {
int num;
printf("Enter the number:");
scanf("%d", &num);
/* check whether the number is negative number */
if (num < 0)
printf("The number is negative.");
else
printf("The number is positive.");
}
Nested if else statements
The use of conditional statements inside another conditional statement.
Syntax
if(test_expression one)
{
if(test_expression two) {
//Statement block Executes when the boolean test expression two is true.
}
}
else
{
//else statement block
}
Example: Nested if else statement
#include<stdio.h>
void main()
{
int x=20,y=30;
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
}
Short Hand If Else
There is also a short-hand if else, which is known as the ternary operator
because it consists of three operands.
It can be used to replace multiple lines of code with a single line. It is often
used to replace simple if else statements:
Syntax
variable = (condition) ? expressionTrue : expressionFalse;
Example: Short Hand If Else
Instead of writing: You can simply write:
int time = 20; int time = 20;
if (time < 18) { (time < 18) ? printf("Good
printf("Good day."); day.") : printf("Good
} else { evening.");
printf("Good evening.");
}
The else if Statement
Use the else if statement to specify a new condition if the first condition is
false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Flowchart: else if statement
Example: else if statement…
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."
Example: else if statement…
int myNum = 10; // Is this a positive or negative number?
if (myNum > 0) {
printf("The value is a positive number.");
} else if (myNum < 0) {
printf("The value is a negative number.");
} else {
printf("The value is 0.");
}
Example: else if statement…
1. #include<stdio.h> 10. printf("\n a is greater than b");
2. void main() 11) }
3. { 12. else if (b > a)
4. int a, b; 13. {
5. printf("Please enter the value for 14. printf("\n b is greater than a");
a:");
15. }
6. scanf("%d", &a);
16. else
7. printf("\nPlease enter the value
17. {
for b:");
18. printf("\n Both are equal");
8. scanf("%d", &b);
19. }
9. if (a > b)
20. }
10. {
for Loop
For Loop
When you know exactly how many times you want to loop through a block of code, use the
for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Flowchart: for loop
Example: for loop statement…
int i; for (i = 0; i <= 10; i = i + 2)
{
for (i = 0; i < 5; i++) { printf("%d\n", i);
printf("%d\n", i); }
}
We ended
Example: for loop statement…
#include<stdio.h>
int main ()
{
/* local variable Initialization */
int n,times=5;;
/* for loops execution */
for( n = 1; n <= times; n = n + 1 )
{
printf("C for loops: %d\n", n);
}
return 0;
}
Nested for Loops
It is also possible to place a loop inside another loop. This is called a nested
loop.
The "inner loop" will be executed one time for each iteration of the "outer
loop":
Example: for loop Statement
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
while Loop
The while loop loops through a block of code as long as a specified condition is
true:
Syntax
while (condition) {
// code block to be executed
}
Flowchart: while Loop
Example: while Loop Statement
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
Example: while Loop Statement…
#include<stdio.h>
int main ()
{
/* local variable Initialization */
int n = 1,times=5;
/* while loops execution */
while( n <= times )
{
printf("C while loops: %d\n", n);
n++;
}
return 0;
}
do-while Loop
The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
Flowchart: do while Loop
Example: do while Loop Statement
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
Example: do while Loop Statement…
#include<stdio.h>
int main ()
{
/* local variable Initialization */
int n = 1,times=5;
/* do loops execution */
do
{
printf("C do while loops: %d\n", n);
n = n + 1;
} while( n <= times );
return 0;
}
break
Used to "jump out" of a switch statement.
The break statement can also be used to jump out of a loop.
The break statement ends the loop immediately when it is encountered.
Example: break in for loop
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
Example: break in while loop
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}
Example: break…
1. / Program to calculate the sum of 9. scanf("%lf", &number);
numbers (10 numbers max)
10. // if the user enters a negative
2. // If the user enters a negative number, break the loop
number, the loop terminates
11. if (number < 0.0) {
3. #include <stdio.h>
12. break;
4. int main() {
13. }
5. int i;
14. sum += number; // sum = sum
6. double number, sum = 0.0; + number;
7. for (i = 1; i <= 10; ++i) { 15. }
8. printf("Enter n%d: ", i); 16. printf("Sum = %.2lf", sum);
17. return 0;
18. }
continue
The continue statement skips the current iteration of the loop and continues
with the next iteration.
Example: continue in for loop
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
Example: continue in a while loop
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
printf("%d\n", i);
i++;
}
Example: continue…
1. #include <stdio.h> 8. if (number < 0.0) {
2. int main() { 9. continue;
3. int i; 10. }
4. double number, sum = 0.0; 11. sum += number; // sum = sum
+ number;
5. for (i = 1; i <= 10; ++i) {
12. }
6. printf("Enter a n%d: ", i);
13. printf("Sum = %.2lf", sum);
7. scanf("%lf", &number);
14. return 0;
15. }
switch...case Statement
Instead of writing many if..else statements, you can use the switch
statement.
The switch statement selects one of many code blocks to be executed:
Flowchart: switch case
switch...case Statement…
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops the execution
The default statement is optional, and specifies some code to run if there is no case match
Example: switch case
int day = 4; printf("Thursday");
break;
switch (day) { case 5:
case 1: printf("Friday");
printf("Monday"); break;
break; case 6:
case 2: printf("Saturday");
printf("Tuesday"); break;
break; case 7:
case 3: printf("Sunday");
printf("Wednesday"); break;
break; }
case 4:
// Outputs "Thursday" (day 4)
Example: switch case…
int day = 4;
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"
Try it Yourself »
goto Statement
The goto statement allows us to transfer control of the program to the
specified label.
Syntax of goto Statement
1. goto label;
2. ... .. ...
3. ... .. ...
4. label:
5. statement;
Example: goto Statement
#include<stdio.h>
void main()
{
int age;
g: //label name
printf("you are Eligible\n");
s: //label name
printf("you are not Eligible");
printf("Enter you age:");
scanf("%d", &age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
}