0% found this document useful (0 votes)
30 views

Control Structures in C Programming

Uploaded by

melvinmels81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Control Structures in C Programming

Uploaded by

melvinmels81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CONTROL STRUCTURES IN C

PROGRAMMING
MELVIN R

1ST SEM BSC MATHS & CS

B SECTION

SFS COLLEGE

BANGALORE
CONTROL STATEMENTS TYPES USED IN C
LANGUAGE

If Statements
Switch Statement
Conditional Operator Statement
Goto Statement
• Loop Statements
THE IF STATEMENTS
The instruction sets will only get executed when the evaluation of the
condition turns out to be true. In case the evaluation of the condition is
false, there will be an execution of a different instruction set. These are
also known as decision control statements

Else if ladder

Nested if
• If… else
• Simple Else or Null
THE IF… ELSE STATEMENT
• When we use the if… else statement, there occurs an execution of two different types of
statements in a program. First, if the available condition in the program is true, then
there will be an execution of the first statement. The execution of the second condition
will only occur if the condition available to us is false
• The syntax for this statement is as follows:
• If (condition 1)
• {
• Statement 1 (s1);
• }
• else
• {
• Statement 2 (s2)
• }
THE NESTED IF STATEMENT
• In this case, the condition available in the next if statement (the second statement)
will only get evaluated if the evaluation of the condition available in the first
statement turns out to be true.

• The syntax for this statement is as follows:

• If (condition 1)

• {

• If (condition 2)

• {

• Statement 1 (s1);

• }

• Else

• {

• Statement 2 (s2)

• }

• }
THE ELSE IF LADDER
• In this statement, the execution of an array of instructions occurs only when the available condition is correct. The verification of the next
condition occurs when this first condition is incorrect.

• The syntax for this statement is as follows:

• If (condition 1)

• {

• Statement 1 (s1);

• }

• Else if (condition 2)

• {

• Statement 2 (s2);

• }

• else if (condition 3)

• {

• Statement 3 (s3)

• }

• …

• Else

• {
THE SIMPLE ELSE OR NULL ELSE
• This condition occurs when a programmer can skip or execute a set of
various instructions on the basis of the condition value. We select a one-
way, simple statement. When the available condition gets evaluated as
true, then a set of various statements will be carried out. In case the
condition is false, then the control here will proceed ahead in the
program with the declaration mentioned below, after the program’s if
declaration
• The syntax for this statement is as follows:
• If (condition1)
•{
• Statement 1 (s1);
•}
THE SWITCH STATEMENTS

• The C language offers its users with a selection statement in various


ways in case a program becomes difficult to read with an increased
number of conditions. A switch statement is a multi-way type of selection
statement that would resolve this issue. The switch declaration comes
into play when more than three alternatives (conditions) exist in a
program. This command then switches between all the available blocks
on the basis of the expression value. Then, each block has a
corresponding value with it.
Syntax
Switch(expression)

case value1: statement_1;

break;

case value2: statement_2;

break;

Case value_n: statement_n;

break;
THE CONDITIONAL OPERATOR
STATEMENTS

The C language also comes with a very unusual operator for its
programmers – the conditional operator.

The syntax of the conditional operator statements is as follows:

(condition 1)? Expression_1: expression_2


• Here, the execution of the expression_1 will only occur when the given
condition is valid. In case this statement is incorrect, then the execution
of the expression_2 will occur.
EXAMPLE
#include <stdio.h>

int main() {

int b;

int a = 2;

b = (a >= 6) ? 6 : a;/* Here, it is equivalent to: if (a >= 5) b = 5; else b = x; */

printf(“b =%d “,b);

return 0;}

The output obtained here would be:


b=2
THE GOTO STATEMENT
• The Goto statement is especially known in the case of jumping control
statements. We mainly use the goto statement when we want to transfer
a program’s control from any one block to another. Also, we use the goto
keyword for the declaration of the goto statement.
• Syntax1 | Syntax2
• ----------------------------
• goto label; | label:
•. | .
•. | .
•. | .
• Label: | goto label;
THE LOOP STATEMENTS
A programmer in C might want to repeat any set of instructions or certain
statements in the program to meet the necessary requirements. In such
instances, it becomes difficult to rewrite and repeat everything. And that is
exactly where we would like to create loops using the looping declarations.
Loop control statements help in such types of situations in C. We have the
following types of loops in C:

Do While Loop

While Loop
• For Loop
DO WHILE LOOP
• The do/while loop is a variant of the while loop. This loop will execute
the code block once, before checking if the condition is true
WHILE LOOP
• The while loop in C is used to evaluate a test condition and iterate over
the loop body until the condition returns True
FOR LOOP
• The for loop in C language is used to iterate the statements or a part of
the program several times.

You might also like