Note on Control Structures

  • Note
  • Things to remember

INTRODUCTION

C programs are written in the linear fashion. The linear programs have almost no logical comparison. A realistic C program may require that a logical test is carried out at some particular point within the program. One of the several possible actions will then be carried out, depending on the outcome of the logical test.

The outcome obtained by making comparisons and moving further calculations is known as branching. Logical statements are tested from several available groups of statements within the program. The program which forms repetitions for a certain number of time is known as looping.

All these branching, selection, and looping are the fundamental necessities of the program without which one cannot make decisions and the program remains incomplete. Structures use different kinds of signs and symbols for constructing the control flow within the program. The logical expressions, relational operations, assignment operators are extensively used in the different program.

The common relational operators are: <, <=, >, >=, the equality operators are: = = and !=. The logical connectives are logical operators such as && (And), || (OR). The conditional operator ? : is also used in the expression in the program.

There are three different kinds of statements in C; expression statements, compound statements and control statements. An expression statement consists of an expression followed by the semicolon. A compound statement consists of a sequence of two or more consecutive statements enclosed in braces ({ and }).

The enclosed statements can be expression statements, other compound statements or control statements. Most control statements contain expression statements or compound statements including embedded compound statements.

Thus, control statements are two types They are:

Source: www.slideshare.net
Source: www.slideshare.net
  1. Decision-making statements
  • if statement
  • else statement
  • switch statement
  1. Looping statements
  • for loop
  • while loop
  • do-while loop

The if Statement

A single line if statement makes a decision about a single condition.

Syntax:

If (condition)

statement;

# Write a program to check whether a given number is divisible by 4.

#include <stdio.h>

void main()

{

int i;

printf(“\n Enter a number:”);

scanf(“%d”,&i);

if (i%4=0)

printf(“\n The number given is divisible by 4. \n”);

}

# Write a program to convert a given time number into seconds.

#include<stdio.h>

void main()

{

float years, secs;

int success;

printf(“\n Input Your Age in Years:”);

scanf(“%f,&years);

success = years;

if (successs ==0)

{

printf(“\n The input value is not floating point number.”);

}

if success !=0)

secs = years *365*24*60*60;

printf(“\n You have lived for %f seconds.\n”,secs);

}

In above program, if statement justify the same condition for two times. The program becomes longer. The judgment takes place two times and if you have such conditions repeatedly in the program it slows down the program.

# Write a program to determine the largest number among three input numbers.

#input <stdio.h>

void main()

{

float a, b, c, big;

printf(“\n Input First Number:”);

scanf(“%f”,&a);

printf(“\n Input Second Number:”);

scanf(“%f”,&b);

printf(“\n Input Third Number:”);

scanf(“%f”,&c);

/* making comparison and computing */

big = a;

if (b>big) big =b;

if (c>big) big =c;

printf(“\n Largest Number among three is %f \n”,big);

}

For a condition, the program need to execute more than one line should be placed compound or placed within the compound statements as:

If (condition)

{

Statement;

Statement;

}

The if..else Statement

The if..esle statement is the statement which compares the given condition. If the condition is true, the execution takes place immediately after the if clause. If the condition is false, the execution takes place immediately after the else clause. The statements within after if and else can be compounded. If the statements are not compounded, the first statement immediately after the clause will be executed.

It is a condition testing statement. As long as the statement 1; a group is not included in the compound statement, only the first line will be executed. If you want to execute multiple lines under the given condition use if clause or else clause.

# Write a program to test whether the given input is zero or non-zero.

#include<stdio.h>

void main()

{

int input;

printf(“\n Enter a number:”);

scanf(“%d”,&input);

if(input)

printf(“\n It is non-zero.\n”);

else

printf(“\n It is zero.\n”);

}

Compound Statement

Source: www.slideshare.net
Source: www.slideshare.net

A compound statement is set of statements enclosed within a pair of curly braces. The curly braces are:{ and}. The {braces is opening brace and } is closing brace. We have discussed these compound statements in our previous examples.

A compound statement is not simply sequences of executable statements. They can also contain variable declarations at the beginning. The variables declared explicitly within the compound statements have local scope.

Example: Test of variables within the compound statements

#include<stdio.h>

void main()

{

int i = 10;

printf(“\n In the main, i is %d.\n”, i);

/* beginning of a compound statement */

{

/* variable declared */

int i = 20;

printf(“\n Inside the compound statement. i is %d\n”,i);

i++;

printf(“\n After incrementing the value of i is %d\n”,i);

}

printf(“\n In the main() the value of i is %d. \n”i);

}

# Write a program to check whether a given number is a prime number.

/* check prime number */

#include<stdio.h>

#include<conio.h>

main ()

{

int a, b=2, r, n;

printf(“\n Enter a number:”);

scanf(“%d”,&a);

n = a;

if (a==1 | | a==2 | | a==3)

printf(“The num %d is prime number”,n);

while (b! = (a-1)

{

aa:

r = a%b;

b=b+1;

if ( r ==0)

{

printf(“\n The num %d is not a prime number.”, n);

break;

else if (b!= (a-1)

{

goto aa;

else

printf(“\n The num %d is a prime number.”, n);

}

}

# Write a program to check whether a given number is odd or even.

#include <stdio.h>

#include <conio.h>

main()

{

int a,r;

printf(“\n Enter a number:”);

scanf(“%d”, &a);

r=a%2;

if(r==0)

printf(“\n The given number %d is even number.”, a);

else

printf(“\n The given number %d is odd number.”, a);

getch();

}

# Write a program to check whether the given number is larger than 100.

#include <stdio.h>

#include <conio.h>

void main ()

{

int a;

printf(“\n Enter a number:”);

scanf(“%d”, &a);

if (a>=100)

printf(“\n The number is larger than 100”);

else

printf(“\n The number is smaller than 100”);
}

# Write a program to check whether a student can enroll in grade XI where the conditions are:

Students must score more than 70 in Math, Science and English.

#include <stdio.h>

#include <conio.h>

void main ()

{

clrscr();

int sc, math, eng;

printf(“\n Enter marks scored in Science:”);

scanf(“%d”,&sc);

printf(“\n Enter marks scored in Maths:”);

scanf(“%d”,&math);

printf(“\n Enter marks scored in English:”);

scanf(“%d”,&eng);

if (sc>=70 && math>=70 && eng>=70)

printf(“\n You are selected.”);

else

printf(“\n You are not selected.”);

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 164-170.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

  1. Branching is carried out after the selection of a choice.
  2. The logical expression, relational operations, assignment operators are extensively used in the comparison and branching with the control structures.
  3. If statement, if…else statement and switch statements are the examples of decision-making statements.
  4. A compound statement is set of statements enclosed within a pair of curly braces. The curly braces are:{ and}. The {braces is opening brace and } is closing brace.
  5. The if..esle statement is the statement which compares the given condition. If the condition is true, the execution takes place immediately after the if clause.
  6. If the condition is false, the execution takes place immediately after the else clause.
.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

You must login to reply

Forum Time Replies Report
Contril

Ask any queries on this note.


You must login to reply