About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

Control Statements in C (Part 1)

Control Statements in C

Control Statements in C

In this article, you will learn about Control statements (including Conditional Control Statement, Multiple Branching Control Statement, Loop Control Statement, and Jump Control Statement) in C programming with the help of examples.

Control Statement:
Control statements allow us to identify the flow of program control i.e, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks again and again (Iterative) or to jump from one part of code to another.

Control Statements are divided into four parts:

1. Decision Making Statement
2. Loop Control Statements
3. Jump Control Statements

1. Decision Making Statements:


In any programming language, there is a need to perform different tasks based on the user test expression. For example, consider an Air India website, when you input wrong username or password it display invalid username and password and when you input correct information then it will displays Booking page. So there must be a logic in place that checks the test expression (username and password) and if the test expression returns true it gives your desired output page else it performs a different task(invalid username and password).



Decision Control Statements is used to control the flow of program groups of statements are executed when the test expression is true. If test expression is false, the else statement is executed.



We have following types of Decision Control Statement:
1. if statement & Nested if
2. if-else, else-if statement
3. if-else ladder
4. switch case

1. if statement:

The if statement evaluates the condition inside the parenthesis ().
  • If the test condition is evaluated to true, statements inside the body of if statement) are executed.
  • If the condition is evaluated to false, statements inside the body of if are not executed.
Syntax: 


Example:


OUTPUT:
num1 and num2 are equal

2. (i) if-else statement:

If the condition is evaluated to true,
  • line of code inside the body of if are executed.
  • line of code inside the body of else are skipped from execution.
If the condition is evaluated to false,
  • line of code inside the body of else are executed
  • line of code inside the body of if are skipped from execution.
Syntax:


Example:


OUTPUT:
Both are not equal

(ii) else-if statement:

The else-if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else-if statement.

Syntax:


Example:


OUTPUT:
num1 is not equal to num2

(iii) Nested if-else:

When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.

Syntax: 


Example:


OUTPUT:
num1 is not equal to num2
num2 is greater than num1

(iv) if-else ladder: 

The if-else statement executes two different statements depending upon whether the condition is true or false. Sometimes, a choice has to be made from more than two possibilities. The if-else ladder allows you to check between multiple conditions and execute different statements.

Syntax:


Example:


OUTPUT:
15 < 25

4. Switch Statement:

Switch statement is useful when programmer need to evaluate multiple test expressions. The switch block defines an test expression and case contain a block of statements, based on the result of expression, corresponding case block statement gets executed. A switch can have any number of cases, however there should be only one default case.

Syntax:


Example:


OUTPUT:

Case3

2. Loop Control Statements:

Set of statements that can be executed again and again according to the specified condition or we can say that loop control statements  are used by the programmer to perform looping operation until the given condition is true. flow of control comes out of the loop when condition become false. 

There are three types of loop control statements:
1. For loop
2. While loop
3. Do-while loop 

1. For loop:

A for loop enable us to perform n number of steps together in one line. In for loop a variable is used to control the loop.

Working Methodology of for loop:
  • The initialization statement is executed only once
  • Then, the condition is evaluated. If the condition is evaluated to false, the for loop is terminated
  • If the condition is evaluated to true, statements inside the body of for loop are executed, and then increment/decrement is performed
  • Again the condition is evaluated.
This process goes on until the condition is false. When the condition is false, the for loop gets terminates.

Syntax:


Example:


OUTPUT:
1 2 3 4 5 6 7 8 9 10

2. while loop:

A while is a pre test loop. It first test a specified conditional expression and as long as the conditional expression is true, action taken while loop also known as entry control loop.

Working Methodology of while loop:
  • The while loop evaluates the condition inside the parenthesis ()
  • If the given condition is true, then the statements inside the body of while loop are executed. Then, the condition is evaluated again
  • The process goes on until the condition is evaluated to false
  • If the condition is false, the while loop gets terminates.
Syntax:


Example:


OUTPUT:
your name
your name
your name
your name
your name

3. Do-while loop:

The do-while loop is same as the while loop with one important difference. In while loop condition is evaluated at the starting of the loop body whereas In do while loop the condition is tested at the end of loop body,and do while loop is also known as exit control loop. 

Working Methodology of Do-while loop:
  • The statements inside the do-while loop is executed once. Only then, the condition is evaluated.
  • If the condition is true, the statements inside the loop is executed again and the condition  is evaluated
  • This process goes on until the condition becomes false
  • If the test condition is false, the do-while loop gets terminates.
Syntax:




Example:




OUTPUT:
Hello World










































Post a Comment

0 Comments