C Programming - Chapter 2 - Control Statement
C Programming - Chapter 2 - Control Statement
PROGRAMMING 2
C/C++ LANGUAGE
• Sequential Flow : In sequential flow of control, the statements of a program are executed from top
to bottom in order in which they are written including method calls.
2.1 INTRODUCTION
• Conditional Flow: are used to transfer execution control to the correct path based on comparison
result. The conditional constructs determine runtime that which statements need to be executed.
The structures which use these type of logic are known as Conditional Structures.
2.1 INTRODUCTION
• Iteration Logic (Repetitive Flow): The Iteration logic employs a loop which involves a repeat statement
followed by a module known as the body of a loop.
2.2 C O N D I T I O N A L S TAT E M E N T S
• The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are
used for decision-making purposes in C/C++ programs.
• They are also known as Decision-Making Statements and are used to evaluate one or more conditions and
make the decision whether to execute a set of statements or not
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.1 if in C/C++:
The if statement is the most simple decision-making statement.
It is used to decide whether a certain statement or block of statements will be executed or not
Syntax of if Statement :
if (condition)
{
// Statements to execute if condition is true
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.1 if in C/C++:
The if statement evaluates the test expression inside the parenthesis.
If the test expression is evaluated to true (nonzero), statement(s) inside the body of if is
executed. If the test expression is evaluated to false (0), statement(s) inside the body of if is
skipped from execution.
2 . 2 C O N D I T I O N A L S TAT E M E N T S
// C program to illustrate If statemen
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf(“%d is greater than 15“,i);
}
printf("I am Not in if");
return 0;
}
In this example, As the condition present in the if statement is false. So, the block of the if
statement is not executed.
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.2 if-else in C/C++:
The if-else statement consists of two blocks, one for false expression and one for true
expression.
Syntax of if-else Statement :
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.1 if in C/C++:
The condition after evaluation will be either true or false.
of if statement is skipped.
2 . 2 C O N D I T I O N A L S TAT E M E N T S
// C program to illustrate if-else statement
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15)
{
printf(“%d is smaller than 15“ ,i);
}
else
{
printf(“%d is greater than 15“,i);
}
return 0;
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.3 Nested if-else in C/C++:
A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement. C and C++ allow us to place an
if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.3 Nested if-else in C/C++:
2 . 2 C O N D I T I O N A L S TAT E M E N T S
// C program to illustrate nested-if statement
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15)
printf(“%d is smaller than 15\n“ ,i);
// Nested - if statement
if (i < 12)
printf(“%d is smaller than 12 too\n“ ,i);
else
printf(“%d is greater than 15“,i);
}
return 0;
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.4 if-else-if Ladder in C/C++:
The if else if statements are used when the user has to decide among multiple options.
The C if statements are executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if
ladder is similar to the switch statement.
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.4 if-else-if Ladder in C/C++:
if (condition1)
{
statement;
}
else if (condition2)
{
statement;
}
.
.
else
{
statement;
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.4 if-else-if Ladder in C/C++:
2 . 2 C O N D I T I O N A L S TAT E M E N T S
// C program to illustrate else if statement
#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
return 0;
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.5 switch Statement in C/C++:
The switch case statement is an alternative to the if else if ladder that can be used to execute the
conditional code based on the value of the variable specified in the switch statement. The switch block
consists of cases to be executed based on the value of the switch variable.
switch (expression)
{
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
2.2 C O N D I T I O N A L S TAT E M E N T S
• 2.2.5 switch Statement in C/C++:
2 . 2 C O N D I T I O N A L S TAT E M E N T S
// C Program to illustrate the use of switch statement
#include <stdio.h>
int main()
{ // variable to be used in switch statement
int var = 2;
// declaring switch cases
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed" );
break;
}
return 0;
}
2.3 L O O P S TAT E M E N T S
• Loops in programming are used to repeat a block of code until the specified condition is met.
• A loop statement allows programmers to execute a statement or group of statements multiple times
without repetition of code.
• There are 3 types of loop statements in C Programming:
for
while
do-while
2.3 L O O P S TAT E M E N T S
• 2.3.1 for loop:
for loop in C programming is a repetition control structure that allows programmers to write a
loop that will be executed a specific number of times. for loop enables programmers to perform n
number of steps together in a single line.
Syntax of for Statement:
int main()
{
int i = 0;
while (test_expression)
{
// body of the while loop
}
2 . 3 l o o p S TAT E M E N T S
// C program to illustrate while loop
#include <stdio.h>
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
2.3 L O O P S TAT E M E N T S
• 2.3.3 do - while loop:
• The do-while loop is similar to a while loop but the only difference lies in the do-while loop test
condition which is tested at the end of the body. In the do-while loop, the loop body will execute at
least once irrespective of the test condition.
• Syntax of do-while Statement:
do
{
// body of do-while loop
} while (test_expression);
2.3 L O O P S TAT E M E N T S
2 . 3 l o o p S TAT E M E N T S
// C program to illustrate do-while loop
#include <stdio.h>
int main()
{
// Initialization expression
int i = 2;
do
{
// loop body
printf( "Hello World\n");
// Update expression
i++;
Name Description
the break statement is used to terminate the switch and loop statement. It transfers the
break
execution to the statement immediately following the loop or switch.
continue statement skips the remainder body and immediately resets its condition before
continue
reiterating it.
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("Break at i = 5\n");
break;
}
printf("i = %d\n", i);
}
return 0;
}
2 . 3 l o o p S TAT E M E N T S
// C program to illustrate continue statement
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("Break at i = 5\n");
continue;
}
printf("i = %d\n", i);
}
return 0;
}
Thank you!