Lesson 4 Control Flow Loops (1)
Lesson 4 Control Flow Loops (1)
C - Flow Control
Statements
1
Introduction
C provides two styles of flow control:
Branching
Looping
2
Branching
Branching is so called because the program chooses to follow one
branch or another.
if statement
This is the most simple form of the branching statements.
3
C – If statement
Syntax of if statement:
If statement Flow Diagram
The statements inside the body of
“if ” only execute if the given
condition returns true.
if (condition)
{
//Block of C statements here
//These statements will only execute if
the condition is true
}
4
C If else statement
Syntax of if else statement:
If condition returns true then the statements
inside the body of “if” are executed and the
statements inside body of “else” are skipped.
If condition returns false then the statements
inside the body of “if” are skipped and the
statements in “else” are executed.
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
5
Syntax of else..if statement
• if (condition1)
• {
• //These statements would execute if the condition1 is true
• }
• else if(condition2)
• {
• //These statements would execute if the condition2 is true
• }
• else if (condition3)
• {
• //These statements would execute if the condition3 is true
• }
• .
• .
• else
• {
• //These statements would execute if all the conditions return false.
• }
6
C Nested If..else statement
When an if else statement is present inside the body of another “if”
or “else” then this is called nested if else.
Syntax of Nested if else statement:
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
7
Example:-C – If statement
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
8
C If else statement Example
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
printf("You are eligible for voting");
else
printf("You are not eligible for voting");
return 0;
}
9
C - switch statement
• A switch statement allows a variable to be tested for equality against a list of values.
• Each value is called a case, and the variable being switched on is checked for each
switch case.
• Syntax
• The syntax for a switch statement in C programming language is as follows −
• switch(expression) {
•
• case constant-expression :
• statement(s);
• break; /* optional */
•
• case constant-expression :
• statement(s);
• break; /* optional */
•
• /* you can have any number of case statements */
• default : /* Optional */
• statement(s);
• }
10
Flow Diagram for switch statement
11
C - switch statement example
• // Following is a simple program to demonstrate
• #include <stdio.h>
• int main()
• {
• int x = 2;
• switch (x)
• {
• case 1: printf("Choice is 1");
• break; Output:
• case 2: printf("Choice is 2");
• break; Choice is 2
• case 3: printf("Choice is 3");
• break;
• default: printf("Choice other than 1, 2 and 3");
• break;
• }
• return 0;
• }
12
Types of Loops
13
Loops in C
• For example:
• Suppose we want to print “Hello World” 10 times.
This can be done in two ways using either a
manual means or using a for Loop:
14
Printing without a Loop
• iterative method to do this is to write the printf() statement 10 times.
• // C program to illustrate need of loops
• #include <stdio.h>
• int main()
• {
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• printf("Hello World\n");
• return 0;
• }
15
Printing with the for Loop
• // C program to illustrate for loop
• #include <stdio.h>
• int main()
• {
• int i;
• for (i = 1; i <= 10; i++)
• {
• printf(“Hello World\n“);
• }
•
• return 0;
• }
16
Syntax for the for loop
The syntax of for loop is:
Or
17
Flow Diagram of For loop
18
How the for loop works
• The initialization statement is executed only once.
19
Example1 of For loop
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 10; a <15; a = a + 1 ){
printf("value of a: %d\n", a);
}
return 0; OUTPUT
} value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
20
Example2 of For loop
• #include <stdio.h>
• int main()
• {
• int i;
•
• for(i=0;i<10;i++)
• {
• printf("%d ",i);
• } Output:
• 0123456789
• }
21
Various forms of for loop in C
• Example 1
• We can use variable num as the counter in all the following examples –
• Example 2
• for (num=10; num<20; num=num+1)
• 2) Initialization part can be skipped from loop as shown below, the counter
variable is declared before the loop.
• int num=10;
• for (;num<20;num++)
22
Nested For Loop in C
• Nesting of loop is also possible. Lets take an example to understand this:
23
C – while loop
• Syntax of while loop:
24
Flow Diagram of while loop
25
Example of while loop
Explanation
• #include <stdio.h>
step1: The variable count is initialized
• int main() with value 1 and then it has been
• { tested for the condition.
• int count=1; step2: If the condition returns true
• while (count <= 4) then the statements inside the body
• { of while loop are executed else
• printf("%d ", count); control comes out of the loop.
• count++;
step3: The value of count is
• }
incremented using ++ operator then
• return 0;
it has been tested again for the loop
• } condition.
Output:1 2 3 4
26
Infinite While Loop
27
Another infinite while loop
• #include <stdio.h>
• int main()
• {
• int var = 6; infinite loop: var will always
• while (var >=5) have value >=5 so the loop
• {
• printf("%d", var);
would never end.
• var++;
• }
• return 0;
• }
28
Logical operators & while Loops
• Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical
operators in while loop.
• The following scenarios are valid :
• while(num1<=10 && num2<=10)
• -using AND(&&) operator, which means both the conditions should be true.
• while(num1<=10||num2<=10)
• – OR(||) operator, this loop will run until both conditions return false.
• while(num1!=num2 &&num1 <=num2)
• – Here we are using two logical operators NOT (!) and AND(&&).
• while(num1!=10 ||num2>=num1)
29
Using logical operator with while Loops
• In this example we are testing multiple conditions using logical operator
inside while loop.
• #include <stdio.h>
• int main() Output:
• {
• int i=1, j=1;
• while (i <= 4 || j <= 3)
11
• { 22
• printf("%d %d\n",i, j); 33
• i++;
• j++;
44
• }
• return 0;
• }
30
C – do..while loop
• do
• {
• //Statements
• }while(condition test);
31
Example of do while loop
• #include <stdio.h>
• int main()
• {
• int j=0;
• do
• {
• printf("Value of variable j is: %d\n", j);
• j++;
• }while (j<=3); Output:
• return 0;
• } Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3
32
While Loop vs Do while loops
In C, do...while loop is very similar to while loop.
Only difference between these two loops is that, in while loops, test
expression is checked at first but, in do...while loop code is executed
at first then the condition is checked.
So, the code are executed at least once in do...while loops when the
condition is false.
33
The end
Thank You
34