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

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if , switch

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: 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.

Switch case

Switch case is used where we required to make a choice between n number of alternatives.

switch (variable)
{
    case value:
        statement 1;
        statement 2;
    break;
    case value:
        statement 1;
        statement 2;
    break;
    default:
        statement 1;
        statement 2;
}

Default statement will execute when no case value is true.

  1. Input number between 1-7 and print day. ie 1-Monday, 2-Tuesday ... so on.
  2. Currency Converter: WAP to convert Dollar into Rupees and vice versa.