0% found this document useful (0 votes)
9 views26 pages

3.control Structure

Chapter three discusses control structures in programming, which dictate the flow of control through sequence, selection, and iteration. It covers decision-making constructs such as if statements, if-else statements, and switch-case statements, as well as looping mechanisms like while, do-while, and for loops. Additionally, it explains the use of break and continue statements to manage loop execution.

Uploaded by

mahdiware
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)
9 views26 pages

3.control Structure

Chapter three discusses control structures in programming, which dictate the flow of control through sequence, selection, and iteration. It covers decision-making constructs such as if statements, if-else statements, and switch-case statements, as well as looping mechanisms like while, do-while, and for loops. Additionally, it explains the use of break and continue statements to manage loop execution.

Uploaded by

mahdiware
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/ 26

Chapter three:- Control

Structure
Control Structure

 Control structures are fundamental constructs in programming that


dictate the flow of control in a program.

 They enable a program to make decisions, repeat actions, and


execute code in a specific order. There are three primary types of
control structures: sequence, selection, and iteration.
 Sequence is the simplest form of control structure where instructions
are executed one after another in a linear order. This is the default
mode of execution in most programming languages. For example, in a
recipe, you might first combine the liquid ingredients and then add
the dry ones.
Control Structure

 Selection it allows a program to choose between different paths of


execution based on certain conditions. This is typically implemented
using if, if-else and switch-case statements. For example, you might
need to collect ripe watermelon in the cartoon so you should skip if
some of them are not ready yet.
 Iteration it is also known as looping, allows a program to repeat a
sequence of steps multiple times. This is achieved using loops such
as for, while, and do-while.
Iteration gives computers much of their power by
enabling them to perform repetitive tasks efficiently.
Decision making

 Decision making is critical to computer programming, so


there will be many situations when you will be given two
or more options and you will have to select an option
based on the given condition.
 Assume given marks are x for a student
If given marks are more than 95 then
Student is brilliant
If given marks are less than 30 then
Student is poor
If given marks are less than 95 and more than 30 then
Student is average
Decision making structure

conditio
n

If condition is true If condition is false


Conditiona
l
Code
Decision making example

int x = 45;
if( x > 95)
“Student is brilliant”

if( x < 30)


"Student is poor”

if( x < 95 && x > 30 )


"Student is average”
Types of decision making

 If Statement
An if statement consists of a boolean expression followed by one or
more statements.

 If …… else Statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false

 Switch Case Statemen

The switch statement is used to select one of many code blocks to be executed.
It is based on the execution of the conditional code referenced the value of the
variable specified in the switch statement.
so A switch statement allows a variable to be tested for equality against a list
of values.
If Statement

 If (boolean_expression)
 {
 /* statement(s) will execute if
the boolean expression is true */
 }
If … else statement

if(boolean_expression)
{
/* statement(s) will execute if the boolean
expression is true */
}
else
{
/* statement(s) will execute if the boolean
expression is false */
}
Switch Statement

 A switch statement allows a variable to be tested


for equality against a list of values. Each value is
called a case, and the variable being switched on
is checked for each switch case.
Switch Statement

 switch(expression){
 case constant-expression :
 statement(s);
 break; /* optional */
 case constant-expression :
 statement(s);
 break; /* optional */

 /* you can have any number of case statements */


 default : /* Optional */
 statement(s);
 }
Loops
Loops

 A loop statement allows us to execute a


statement or a group of statements multiple times
and following is the general from of a loop
statement in most of the programming languages.
Loops structure

Conditional
Code
If condition is true
conditio
n

If condition is false
While loop type

 While loop
 It repeats a statement or a group of statements while
a given condition is true. It tests the condition before
executing the loop body.

while(condition)
{
statement(s);
}
While loop Example
 using System;

 namespace Loops

 {

 class Program

 {

 static void Main(string[] args)

 {

 /* local variable definition */

 int a = 10;

 /* while loop execution */

 while (a < 20)

 {

 Console.WriteLine("value of a: {0}", a);

 a++;

 }

 Console.ReadLine();

 }

 }

 }
Do … While Loop type

 Do … While loop
 It is similar to a while statement, except that it tests
the condition at the end of the loop body.
do
{
statement(s);
} while( condition );
For Loop type

 For loop
 It executes a sequence of statements multiple times
and abbreviates the code that manages the loop
variable.

for ( init; condition; increment )


{
statement(s);
}
For loop flow control

 Here is the flow of control in a for loop:


1. The init step is executed first, and only once. This step allows you to declare
and initialize any loop control variables. You are not required to put a
statement here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is executed.
If it is false, the body of the loop does not execute and flow of control jumps
to the next statement just after the for loop.
3. After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop
control variables. This statement can be left blank, as long as a semicolon
appears after the condition.
4. The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
testing for a condition). After the condition becomes false, the for loop
terminates.
For Loop Example
 using System;
 namespace Loops
 {

 class Program
 {
 static void Main(string[] args)
 {
 /* for loop execution */
 for (int i = 10; i < 20; i = i+ 1)
 {
 Console.WriteLine("value of i: {0}", i);
 }
 Console.ReadLine();
 }
 }
 }
For each

The foreach loop in C# is used to iterate over elements in a collection,


such as an array or a list. It provides a simple and readable way to
traverse through all the elements without needing to manage the loop
counter.
The foreach loop is particularly useful because it abstracts away the
need for managing the loop counter and bounds checking, making the
code cleaner and less error-prone.

Example
 We declare a list of strings fruits.
 The foreach loop iterates over each element in the fruits list and
prints it to the console.
Example

 using System;
 class Program
 {
 static void Main()
 {
 string[] cars = { "Volvo", "BMW", "Ford", "Mazda" };
 foreach (string car in cars)
 {
 Console.WriteLine(car);
 }
 }
 }
Break and continue

Certainly! In C#, break and continue are control flow statements used within loops to alter
the flow of execution.
The break statement is used to exit from the nearest enclosing loop or switch statement.
When a break statement is encountered, the loop or switch terminates immediately, and
control is transferred to the statement following the terminated statement.
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit the loop when i is 5
}
Console.WriteLine(i);
}
// Output: 0 1 2 3 4
Break and continue

The continue statement skips the remaining code inside


the current iteration of the loop and proceeds with the
next iteration. It does not terminate the loop but rather
skips to the next iteration.
Example
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
continue; // Skip the rest of the loop body for even numbers
}
Console.WriteLine(i);
}
// Output: 1 3 5 7 9
Break vs continue

Break Statement
The break statement is used to terminate the closest
enclosing loop or switch statement. When
a break statement is encountered, the control is
transferred to the statement immediately following the
terminated loop or switch.
Continue Statement
The continue statement is used to skip the current
iteration of the closest enclosing loop and proceed with
the next iteration. When a continue statement is
encountered, the remaining code inside the loop for the
current iteration is skipped, and the loop proceeds with the
next iteration.
END

You might also like