W e b t u t o r i a l s

Programing Approach

Algorithm, Flow Chart

Define C

Define Programming

C - Syntax

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

Control Structure

Control Structure - if

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Constant Variable

C - Escape Sequence

C - Arrays 1D, 2D

C - Sorting

ASCII Value

Character I/O Function

C - Strings

Ex: Strings

Array of Strings

C - Math Functions

C - Functions

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

Typedef File Handling

Command Line Argument

MCQ

Control Structure

Control structure holds the execution of a program by any condition or decision. Control structure specifies the order in which the various instructions in a program are to be executed by the computer. Control structure determines the flow of control in a program.

There are 3 types of control structures:

1. Decision Control Structure: Decision statement allow the computer to take a decision as which statement is to be execute next.

Decision Statement types:

  1. If - In this we specify the condition, if the condition is true only than the statement will be executed otherwise not.
Syntax:
if (condition)
{
	Statement 1;
	Statement 2;
	.
	.
	Statement n;
}

Example 1: Input user's age and check it is eligible for vote or not.

#include<stdio.h>
#include<conio.h>

void main()
{
  int n;
  clrscr();
  printf("Enter Age:");
  scanf("%d",&a);
  if(n>=18)
  {
    printf("User is eligible for vote");
  }
  getch();
}

Output: 1

Enter Age: 20
User is eligible for vote

Output: 2

Enter Age: 15

In Output 1, if user enters value 18 or above then the message will appear, 'User is eligible for vote'. In Output 2, if user enters value less than 18 no message will appear. If you want to show a message when user enters value less than 18, in that case use if-else condition.


  1. if–else: if else contain statements in 2 parts. Statements of one part will execute if the condition is true and the other part statements will execute if the condition is false. Both the part of statements will not be executed in either condition.
Syntax:
if (condition)
{
    Statement 1;
    Statement 2;
      .
      .
    Statement n;
}	
else
{
    Statement 1;
    Statement 2;
      .
      .
    Statement n;
}

Example 2: Re-write the above program using if-else condition.

#include<stdio.h>
#include<conio.h>

void main()
{
  int n;
  clrscr();
  printf("Enter Age:");
  scanf("%d",&a);
  if(n>=18)
  {
    printf("User is eligible for vote");
  }
  else
  {
    printf("User is not eligible for vote");
  }
  getch();
}

Output: 1

Enter Age: 20
User is eligible for vote

Output: 2

Enter Age: 15
User is not eligible for vote

  1. Nested if - usefull when multiple conditions are available.

Syntax: Nested if

if (condition 1)
{
	Statement 1;
	Statement 2;
	.
	.
	Statement n;
}
else if (condition 2)
{
	Statement 1;
	Statement 2;
	.
	.
	Statement n;
}
else
{
	Statement 1;
	Statement 2;
	.
	.
	Statement n;
}

In the above example, statement 1 is executed when condition 1 is true rest of the condition is not executed nor checked. Statement 2 is executed when condition 1 is false and condition 2 is true. If both the condition 1 and condition 2 is false than statement 3 is execute.

Updated: 13-Jul-18