Class 9 | Control Flow Statement | C Programming Notes

Control flow statement

 

Control flow statements also referred as control structures are used to control the flow of execution of statements of a program. The use of control statements help users specify the order of execution of the instruction in a program. If there are no control statement, the statements are executed in a sequential way, one after another till end of the program.

 

The types of control statements are listed below:

 

A’ conditional/ decision-making statement:

 

It is used in C program to select appropriate output or input from a group of input/ output. The various selective statements are:

 

  • If statement:

 

The if statement is a conditional branch statement. If the condition is true the statement after the condition is the executed, otherwise, execution will skip the next statement.

 

Syntax:

 

If(condition){

 

//block of code

 

}

 

Flowchart

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EG:

 

#include<stdio.h>

 

#include<conio.h>

 

Int main()

 

{

 

Int a;

 

Printf(enter the value of a);

 

Scanf(%d, &a);

 

If(a>0)

 

{

 

Printf(the number is positive);

 

}

 

Return 0;

 

}

 

  • If-else statement:

 

If an if-else statement, the condition is evaluate first. If the condition is true the statement in the immediate block is executed. It is also called two way decision making statement.

 

Syntax:

 

If(condition)

 

{

 

//statement of body;

 

}

 

Else

 

{

 

//statement of body;

 

}

 

 

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include<stdio.h>

 

#include<conio.h>

 

Int main()

 

{

 

Int a;

 

Printf(enter the number:\n);

 

Scanf(%d, &a);

 

If(a>0)

 

{

 

Printf(a is positive);

 

}

 

Else

 

{

 

Printf(a is negative);

 

}

 

Return 0;

 

}

 

  • If-else-if ladder statement:

 

In if-else-if ladder statement, the condition expression is evaluated in order. If any of these expression found to be true ,the statement associated with it is executed and terminate the whole chain. If non of the expression are true than the statement associated with final else.

 

Syntax: If(condition 1)

 

{

 

//statement 1;

 

}

 

Else if(condition 2)

 

{

 

// statement 2;

 

}

 

.

 

.

 

.

 

Else if(condition n)

 

{

 

//statement n;

 

}

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include<stdio.h>

 

#include<conio.h>

 

Int main()

 

{

 

Int result;

 

Printf(enter the marks of student);

 

Scanf(%d, &result);

 

If(result>=75)

 

{

 

Printf(passed: grade A \n);

 

}

 

else if(result>=60)

 

{

 

Printf(passed: grade B \n);

 

}

 

else if(result.45)

 

{

 

Printf(passed: grade C\n);

 

}

 

Else

 

{

 

Printf(failed);

 

}

 

Return 0;

 

}

 

  • Switch-case statement:

 

This is the another form of multi way decision making if the condition is match. The statement associated with it will be executed. If the expression doesnt match any of the statement and there is a default statement, execution switches to default statement otherwise the switch statement end.

 

Syntax Switch(variable)

 

{

 

Case values:

 

{

 

//code to be executed; Break;

 

}

 

Case value 2:

 

{

 

//code to be executed; Break;

 

}

 

Default:

 

{

 

//code to be executed;

 

}

 

}

 

Eg:

 

#include<stdio.h>

 

#include<conio.h>

 

Int main()

 

{

 

Int day;

 

Printf(enter the days of week);

 

Scanf(%d, &days);

 

Switch(days)

 

{

 

Case 1:

 

Printf(Sunday \n);

 

Break;

 

Case 2:

 

Printf(Monday \n);

 

Break;

 

Case 3:

 

Printf(Tuesday \n);

 

Break;

 

Case 4:

 

Printf(Wednesday \n);

 

Break;

 

Case 5:

 

Printf(Thursday \n);

 

Break;

 

Case 6:

 

Printf(Friday \n);

 

Break;

 

Case 7:

 

Printf(Saturday \n);

 

Break;

 

Default=

 

Printf(invalid day);

 

}

 

Return 0;

 

}

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

B’ looping/ iteration statement:

 

Looping structure is also known as repetitive or iteration statement. In c programming, this type of loop which are used for looping purpose. The types are given below:

 

  • For loop statement:

 

A for loop is a repetition control structure that allows you to efficiently write a loop that need to execute a specific number of times.

 

Syntax:

 

For (initialization; condition; inc; dec;)

 

{

 

//statement;

 

}

 

Flow chart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include <stdio.h>

 

int main() {

 

for (int i = 1; i <= 10; i++) {

 

printf(“%d “, i); // Printing the current value of i

 

}

 

printf(“\n”); // Printing a newline after the loop

 

return 0;

 

}

 

  • While loop statement:

 

A while loop in c programming repeatedly execute a target statement as long as a given condition is true. It check at first if it is found true then it execute the statement otherwise, it gets out of the loop. It is also called as entry control loop.

 

Syntax: Initialization While (condition)

 

{

 

  • statement;

 

Inc/dec

 

}

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include <stdio.h>

 

int main()

 

{

 

int i = 0;

 

while (i < 10) {

 

printf(“%d “, i);

 

i++;

 

}

 

return 0;

 

}

 

  • Do-while loop statement:

 

it executes program statements once at first then only condition is check. If condition is found to be true then it executes statements again, otherwise, it gets out for the loop. It is also called exist control room.

 

Syntax: Do

 

{

 

//statement;

 

}

 

While(condition);

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include <stdio.h>

 

int main()

 

{

 

int count = 1;

 

do {

 

printf(“Count: %d\n”, count);

 

count++;

 

}

 

while (count <= 5);

 

return 0;

 

}

 

  • Nested loop statement:

 

C programming allows to use loop inside another loop using nested loop statement. The following section shows a few example to illustrate the context.

 

Syntax:

 

For(initialization; condition; inc/dec)

 

{

 

For(initialization; condition; inc/dec)

 

{

 

//statement(R)

 

}

 

//statement(R)

 

}

 

Flowchart:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eg:

 

#include <stdio.h>

 

int main()

 

{

 

int rows, cols;

 

printf(“Enter the number of rows: “);

 

scanf(“%d”, &rows);

 

printf(“Enter the number of columns: “);

 

scanf(“%d”, &cols);

 

for (int i = 1; i <= rows; i++) {

 

for (int j = 1; j <= cols; j++) {

 

printf(“(%d, %d) “, i, j);

 

}

 

printf(“\n”);

 

}

 

return 0;

 

}

 

C’ jumping statement:

 

Jumping statement are used to jump the execution of program statement from one place to another place inside the program. There are four jumping statement, they are given below:

 

  • Break statement:

 

Break statement used to break the normal flow of program execution in loop and switch case statement. When the break is encountered in the program, remaining part part of the program statement is skipped and control will be passed to the next statement.

 

Syntax:

 

Break;

 

Eg:

 

#include <stdio.h> int main()

 

{

 

int i;

 

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

 

  • If i equals 5, exit the loop if (i == 5) {

 

break;

 

}

 

printf(“%d “, i);

 

}

 

return 0;

 

}

 

  • Continue statement:

 

It is used to continue the normal flow of program statement in loop. When continue is encountered in the program, the particular iteration is skipped and the loop will be continue with the next iteration

 

Syntax:

 

Continue;

 

Eg:

 

#include <stdio.h> int main()

 

{

 

int i;

 

for (i = 1; i <= 10; i++) { if (i % 2 != 0) {

 

continue;

 

}

 

printf(“%d “, i);

 

}

 

return 0;

 

}

 

  • Go to statement:

 

Go to statement is a jump statement, it is used to transfer the program control from one location to another location.

 

Syntax: Goto label; //statement; Label:

 

 

Eg:

 

#include <stdio.h>

 

int main()

 

{

 

int i = 0;

 

start:

 

printf(“%d\n”, i);

 

i++;

 

if (i < 5)

 

goto start;

 

return 0;

 

}

 

  • Return statement:

 

It is used at the end of function or program to end or terminate it with value or without value. If written value associated with it, that value becomes return value. The general form of return statement is return expression.

 

Syntax:

 

Return; Or

 

Return expression;

 

Eg:

 

#include <stdio.h> int add(int a, int b) { int sum = a + b;

 

return sum;

 

}

 

int main() { int result; result = add(5, 3); printf(“Sum: %d\n”, result); return 0;

 

 

}

 

Difference between syntax error and semantic error

 

Syntax error Semantic error
   
It is also called grammatical errors. It is also called logical errors.
   
It is concerned with structure. It is concerned with meaning.
   
Syntax error are easily traceable by Only experts are traces such type of errors.
beginners of a program.  
   
Computer cannot read and understand such Computer executes the program but output
type of errors. is wrong.
   
Eg: int a=5 (missing semicolon). Eg: c=a + b; (but we mistakenly placed c= a –
  b), then it gives wrong output in logically.
   

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *