0% found this document useful (0 votes)
23 views37 pages

C Programming - Chapter 2 - Control Statement

The document discusses different control structures in C/C++ programming, including conditional statements (if, if-else, switch) and loops (for, while, do-while). Conditional statements allow for decision-making and changing the flow of execution based on conditions, while loops allow repeating blocks of code. The for loop initializes a counter variable, tests a condition, and updates the counter each iteration. Nested conditional statements and else-if ladders are also covered.

Uploaded by

Củ Hành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views37 pages

C Programming - Chapter 2 - Control Statement

The document discusses different control structures in C/C++ programming, including conditional statements (if, if-else, switch) and loops (for, while, do-while). Conditional statements allow for decision-making and changing the flow of execution based on conditions, while loops allow repeating blocks of code. The for loop initializes a counter variable, tests a condition, and updates the counter each iteration. Nested conditional statements and else-if ladders are also covered.

Uploaded by

Củ Hành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

COMPUTER

PROGRAMMING 2
C/C++ LANGUAGE

M.E. LE THANH TUNG


Chapter 2: Control Structures
WEEK 2

M.E. LE THANH TUNG


2.1 INTRODUCTION

• Control Structures are just a way to specify flow of control in programs.


• There are three basic types of logic, or flow of control, known as:
 Sequence logic, or sequential flow
 Selection logic, or conditional flow
 Iteration logic, or repetitive flow
2.1 INTRODUCTION

• 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.

 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, statement(s) inside the body

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:

for (initialize expression; test expression; update expression)


{
//
// body of for loop
//
}
2.3 L O O P S TAT E M E N T S
• 2.3.1 for loop:
• 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 and the
body of for loop will be executed.
 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 could
be incremented, decremented, multiplied, or divided by any value.
2.3 L O O P S TAT E M E N T S
• 2.3.1 for loop:
2 . 3 l o o p S TAT E M E N T S
// C program to illustrate for loop
#include <stdio.h>

int main()
{
int i = 0;

for (i = 1; i <= 10; i++)


{
printf("Hello World\n");
}
return 0;
}
2.3 L O O P S TAT E M E N T S
• 2.3.2 while loop:
• while loop does not depend upon the number of iterations. In for loop the number of iterations was
previously known to us but 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.
• Syntax of while Statement:

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++;

} while (i < 1); // Test expression


return 0;
}
2.3 L O O P S TAT E M E N T S
• 2.3.4 loop control statements:
• 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. 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.

goto goto statement transfers the control to the labeled statement.


2 . 3 l o o p S TAT E M E N T S
// 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 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!

M.E. LE THANH TUNG

You might also like