0% found this document useful (0 votes)
2 views

Lesson 4 Control Flow Loops (1)

This document provides an overview of flow control statements in C programming, focusing on branching and looping. It explains various types of branching statements such as 'if', 'if-else', and 'switch', as well as looping constructs including 'for', 'while', and 'do-while' loops. Additionally, it includes syntax examples and flow diagrams to illustrate how these control structures operate.

Uploaded by

jeresladii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson 4 Control Flow Loops (1)

This document provides an overview of flow control statements in C programming, focusing on branching and looping. It explains various types of branching statements such as 'if', 'if-else', and 'switch', as well as looping constructs including 'for', 'while', and 'do-while' loops. Additionally, it includes syntax examples and flow diagrams to illustrate how these control structures operate.

Uploaded by

jeresladii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Lesson 4

C - Flow Control
Statements

1
Introduction
 C provides two styles of flow control:
 Branching
 Looping

 Branching is deciding what actions to take and


looping is deciding how many times to take a
certain action.

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.

 It takes an expression in parenthesis and a statement or block of


statements.

 If the expression is true then the statement or block of statements


gets executed otherwise these statements are skipped.

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 the condition returns false then


the statements inside “if ” are
skipped.

 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

Loops are used in programming to repeat a specific block


until some end condition is met.
There are three loops in C programming:
• For loop
• While loop
• Do while loop

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:

for (initialization Statement; test Expression; update Statement)


{
// codes
}

Or

for (initialization; condition test; increment or decrement)


{
//Statements to be executed repeatedly
}

17
Flow Diagram of For loop

18
How the for loop works
• The initialization statement is executed only once.

• Then, the test expression is evaluated.


• If the test expression is false (0), for loop is terminated.
• But if the test expression is true (nonzero), codes inside
the body of for loop is executed and the update
expression is updated.
• This process repeats until the test expression is false.

• The for loop is commonly used when the number of


iterations is known.

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 –

• 1) Here instead of num++, we use num=num+1 which is same as num++.

• 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:

• #include <stdio.h> Output:


• int main()
• { 0, 0
• for (int i=0; i<2; i++)
0, 1
• {
• for (int j=0; j<4; j++) 0, 2
• { 0, 3
• printf("%d, %d\n",i ,j); 1, 0
• } 1, 1
• } 1, 2
• return 0; 1, 3
• }

23
C – while loop
• Syntax of while loop:

• while (condition test)


• {
• //Statements to be executed repeatedly
• // Increment (++) or Decrement (--) Operation
• }

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

• #include <stdio.h> The program is an example of infinite


• int main() while loop.
• { Since the value of the variable var is
• int var=1; same (there is no ++ or – operator
• while (var <=2) used on this variable, inside the body
• { of loop) the condition var<=2 will be
• printf("%d ", var); true forever and the loop would
• } never terminate.
• }

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

Flow diagram of do while loop


• Syntax of 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.

 Meaning at least one output is produced in do while loop even if the


condition is false

33
The end
Thank You

34

You might also like