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

Chapter 2 - Basic of c - Control Structure in c

Chapter 2 covers the basics of control structures in C/C++, focusing on conditional statements such as if, if-else, and switch statements, as well as loop statements like for, while, and do-while. It explains the syntax and functionality of each structure, providing examples for clarity. Additionally, it discusses loop control statements like break and continue to modify the flow of execution.

Uploaded by

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

Chapter 2 - Basic of c - Control Structure in c

Chapter 2 covers the basics of control structures in C/C++, focusing on conditional statements such as if, if-else, and switch statements, as well as loop statements like for, while, and do-while. It explains the syntax and functionality of each structure, providing examples for clarity. Additionally, it discusses loop control statements like break and continue to modify the flow of execution.

Uploaded by

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

CHAPTER 2: BASIC OF C/C++

D ATA S T R U C T U R E a n d A L G O R I T H M S

M.E. LE THANH TUNG


2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ 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.
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If statements:
⚬ 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
⚬ The syntax of if statements in C:
if (condition)
{
statements;

}
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If statements:
⚬ 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 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.3.1 Conditional statements:


⚬ Example:
// C Program
#include <stdio.h>
int main()
{
// variable declaration:
int x1; // integer variable
fl oat x2; // fl oat variable

// variable declaration & initialization:


int a1 = 5; // integer variable
fl oat a2 = 5.5; // fl oat variable

return 0;
}
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If - else statements:
⚬ The if-else statement consists of two blocks, one for false
expression and one for true expression.
⚬ The syntax of if-else statements in C:
if (condition)
{
statements;
}
else
{
statements;
}
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If-else statements:
⚬ If the condition is true,
statement(s) inside the body
of if statement is executed and
statement(s) inside the body
of else statement is skipped from
execution.
⚬ If the condition is false,
statement(s) inside the body
of else statement is executed,
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If – else if statements:
⚬ 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.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If – else if statements:
⚬ The syntax of if-else if statements in C:
if (condition1)
{ statements;
}
else if (condition2)
{ statements;
}

else
{ statements;
}
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ If-else if statements:
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ switch statements:
⚬ 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.
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ switch statements:
⚬ The syntax of switch statements in C:
switch (expression)
{
case value1:
statements 1;
case value2:
statements 2;

default :
statements n;
}
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ switch statements:
2.3 CONTROL STRUCTURES:

• 2.3.1 Conditional statements:


⚬ Example:
// 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 (2*var) {
case 2:
printf("Case 1 is executed");
break;
case 4:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ 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 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ for statements:
⚬ The syntax of for statements in C:
for (initialize expression; test expression; update
expression)
{
statements;
}
In for loop, a loop variable is used to control the loop. Firstly we
initialize the loop variable with some value, then check its test
condition. If the statement is true then control will move to the body
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ for statements:
⚬ Initialization Expression: In this expression, we
assign a loop variable or loop counter to some
value. For example: int i=1;
⚬ Test Expression: In this expression, test
conditions are performed. If the condition evaluates
to true then the loop body will be executed and
then an update of the loop variable is done. If the
test expression becomes false then the control will
exit from the loop.
⚬ Update Expression: After execution of the loop
body loop variable is updated by some value it
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ for statements:
// C program to illustrate for loop
#include <stdio.h>

int main()
{
for (int i = 1; i <= 10; i++)
{
printf("Hello World\n");
}
return 0;
}
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ while statements:
⚬ The syntax of while statements in C:
while (condition)
{
statements;
}
In the while loop, the execution is terminated on the basis of the test
condition. If the test condition will become false then it will break
from the while loop else body will be executed.
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ while statements:
// 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 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ Do - while statements:
⚬ The syntax of do - while statements in C:
do
{
statements;
}
while (condition);
The do-while loop is similar to a while loop but 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
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ while statements:
// 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++;
} while (i < 1); // Test expression
return 0;
}
2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ Loop control statement: Loop control statements in C
programming are used to change execution from its normal
sequence.
Name Description

the break statement is used to terminate the switch and loop statement.
break It transfers the execution to the statement immediately following the loop
or switch.

continue statement skips the remainder body and immediately resets its
continue
condition before reiterating it.

goto goto statement transfers the control to the labeled statement.


2.3 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ Example:
// C program to illustrate break statement
#include <stdio.h>
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 CONTROL STRUCTURES:

• 2.3.2 Loop statements:


⚬ Example:
// 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;
}
DATA STRUCTURE &
ALGORITHMS
THANKS YO U

You might also like