Unit 2 - C Flow Control
Unit 2 - C Flow Control
if...else Statement
if
else
else if
while Loop
Do while loop
20. break; 0
23. break; 3
24. default: 4
26. break; 1
27. } 2
28. return 0; 3
29. } 4
The number is ten
Advantages
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
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);
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);
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
}
Short Hand If Else
Syntax
variable = (condition) ? expressionTrue : expressionFalse;
Example: Short Hand If Else
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…
We ended
Example: for loop statement…
#include<stdio.h>
int main ()
{
/* local variable Initialization */
int n,times=5;;
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;
int i = 0;
int i;
int i = 0;
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");
}
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
}