SlideShare a Scribd company logo
2
Most read
4
Most read
9
Most read
C#
Control Statements
PF 102
Control statements
Linear (sequential) program execution
Selection structure and repetition structure
Structured programming
◦ Controlled entry and exit out of a module.
◦ Avoid goto statements
Selection structures in C#
if
 single selection statement
if…else
double selection statement
switch
multiple – selection statement
Examples:
if (grade >= 60)
Console.writeline(“Passed!”);
If (grade >=0)
Console.writeline(“Passed!”);
else
Console.writeline(“Failed!”);
Conditional Operator
Console.writeline(grade >= 60 ?“Passed!” : “Failed!”);
Nested
if (grade >=90)
Console.writeline(“A”);
else if (grade >=80)
Console.writeline(“B!”);
…
else
Console.writeline(“F!”);
Repetiton Structure - while
While (condition){
statement 1;
statement 2;
statement 3;
…
}
Example
length = Convert.ToInt16(Console.ReadLine());
while (length > 0)
{
Console.Write("Enter Height of the Wall: ");
height = Convert.ToInt16(Console.ReadLine());
PaintAWall thisWall = new PaintAWall(length, height, pricePerGal);
thisWall.CalculateCost(ref paintCost,ref laborCost,ref galPaint, ref sqFt);
Console.Write("Enter Length and Height for Wall # : “ +
Convert.ToString(numWalls+1));
Console.Write("nEnter Length of the Wall (0 to quit): ");
length = Convert.ToInt16(Console.ReadLine());
}
Counter controlled vs
Sentinel controlled
A while loop, use LCV as a counter
Counter =1
While (counter <=10)
{
…
Counter ++
}//does it 10 times
Sentinel controlled is good when one does not know exact number of
times to execute a loop
Explicitly and Implicitly converting
between simple types
Integer and integer division yields integer result.
Suppose average is a floating point number:
Average = total/num. Average will only get an integer if
total and num are integers.
int sum = 200, num = 3;
float average;
average = sum / num;
Console.WriteLine(average);
Will print 66
Unary Cast Operator
int sum = 200, num = 3;
float av;
av = (float) sum / num;
Console.WriteLine(av);
Will print 66.6666
Float/float or float/int or int/float will yield a float.
C# implicitly promotes the one int to float.
Nested control statements
See example of a multiplication table generation
class MultiplicationTable{
static void Main(string[] args){
int i=2, j, k;
while (i <= 12){
for (j = 1; j <= 10; j++){
Console.WriteLine(i + " x " + j + " = " + i * j );
}
Console.WriteLine("n");
i++;
}
}
}
}
Switch Statements
Switch (expression) {
case value1: //Statements
break;
case value 2: //Statements
break;
case value 3: //Statements
case value n: //Statements
break;
default: //Statements
}

More Related Content

Similar to C# Control Statements, For loop, Do While.ppt (20)

DOCX
C# language basics (Visual studio)
rnkhan
 
PDF
Xamarin: Branching and Looping
Eng Teong Cheah
 
PDF
csharp repitition structures
Micheal Ogundero
 
PPT
Csphtp1 05
HUST
 
PPTX
Chapter3: fundamental programming
Ngeam Soly
 
PPTX
10control statement in c#
Sireesh K
 
PPTX
06.Loops
Intro C# Book
 
PPT
Lecture 3
Soran University
 
PDF
C sharp chap4
Mukesh Tekwani
 
PPT
For Beginners - C#
Snehal Harawande
 
PPTX
Flow Control and Exception Handling.pptx
Sheetal343198
 
PPTX
Control Structures in C
sana shaikh
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
C# 101: Intro to Programming with C#
Hawkman Academy
 
PPSX
C# - Part 1
Md. Mahedee Hasan
 
PDF
Programming methodology lecture07
NYversity
 
PPT
C Sharp Jn (3)
jahanullah
 
PPT
Csphtp1 04
HUST
 
PDF
CSharpCheatSheetV1.pdf
ssusera0bb35
 
PPT
C tutorial
Anurag Sukhija
 
C# language basics (Visual studio)
rnkhan
 
Xamarin: Branching and Looping
Eng Teong Cheah
 
csharp repitition structures
Micheal Ogundero
 
Csphtp1 05
HUST
 
Chapter3: fundamental programming
Ngeam Soly
 
10control statement in c#
Sireesh K
 
06.Loops
Intro C# Book
 
Lecture 3
Soran University
 
C sharp chap4
Mukesh Tekwani
 
For Beginners - C#
Snehal Harawande
 
Flow Control and Exception Handling.pptx
Sheetal343198
 
Control Structures in C
sana shaikh
 
Control statements in c
Sathish Narayanan
 
C# 101: Intro to Programming with C#
Hawkman Academy
 
C# - Part 1
Md. Mahedee Hasan
 
Programming methodology lecture07
NYversity
 
C Sharp Jn (3)
jahanullah
 
Csphtp1 04
HUST
 
CSharpCheatSheetV1.pdf
ssusera0bb35
 
C tutorial
Anurag Sukhija
 

More from Riannel Tecson (20)

PPT
Introduction to C# Language and Applications.ppt
Riannel Tecson
 
PPTX
PF - 102 Terminal Project Guidelines.pptx
Riannel Tecson
 
PPTX
Who is the most Influential Person in Your Life.pptx
Riannel Tecson
 
PPTX
Week 6 MIL Evaluating Information and Importance.pptx
Riannel Tecson
 
PPTX
Java Programs Debugging: Identifying Errors.pptx
Riannel Tecson
 
PPTX
Greedy-Algorithms with Applications.pptx
Riannel Tecson
 
PPT
Linked list-stack-queue Data Structure .ppt
Riannel Tecson
 
PPT
List Data Structure, Linked List, Stacks.ppt
Riannel Tecson
 
PPTX
Computer Systems Servicing Network Cabling.pptx
Riannel Tecson
 
PPTX
introduction-to-functions-grade-11general-math.pptx
Riannel Tecson
 
PPTX
Week-9-UCSP-How-Society-is-Organized.pptx
Riannel Tecson
 
PPT
PC Components_Hardware_Software_CSS11.ppt
Riannel Tecson
 
PPTX
Writing the Design and Methodology in Research
Riannel Tecson
 
PPTX
capstone101 Requirements Methodology and
Riannel Tecson
 
PPTX
Writing the Review of Related Literature.pptx
Riannel Tecson
 
PPTX
Week 1 and 2 Getting started with DBMS.pptx
Riannel Tecson
 
PPTX
Network Tools, Materials and Equipment.pptx
Riannel Tecson
 
PPTX
Week 5 Update Anomalies.pptx
Riannel Tecson
 
PPT
Binary Search Tree Traversal.ppt
Riannel Tecson
 
PPT
Does technology shaed soceity.ppt
Riannel Tecson
 
Introduction to C# Language and Applications.ppt
Riannel Tecson
 
PF - 102 Terminal Project Guidelines.pptx
Riannel Tecson
 
Who is the most Influential Person in Your Life.pptx
Riannel Tecson
 
Week 6 MIL Evaluating Information and Importance.pptx
Riannel Tecson
 
Java Programs Debugging: Identifying Errors.pptx
Riannel Tecson
 
Greedy-Algorithms with Applications.pptx
Riannel Tecson
 
Linked list-stack-queue Data Structure .ppt
Riannel Tecson
 
List Data Structure, Linked List, Stacks.ppt
Riannel Tecson
 
Computer Systems Servicing Network Cabling.pptx
Riannel Tecson
 
introduction-to-functions-grade-11general-math.pptx
Riannel Tecson
 
Week-9-UCSP-How-Society-is-Organized.pptx
Riannel Tecson
 
PC Components_Hardware_Software_CSS11.ppt
Riannel Tecson
 
Writing the Design and Methodology in Research
Riannel Tecson
 
capstone101 Requirements Methodology and
Riannel Tecson
 
Writing the Review of Related Literature.pptx
Riannel Tecson
 
Week 1 and 2 Getting started with DBMS.pptx
Riannel Tecson
 
Network Tools, Materials and Equipment.pptx
Riannel Tecson
 
Week 5 Update Anomalies.pptx
Riannel Tecson
 
Binary Search Tree Traversal.ppt
Riannel Tecson
 
Does technology shaed soceity.ppt
Riannel Tecson
 
Ad

Recently uploaded (20)

PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
Different types of inheritance in odoo 18
Celine George
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Horarios de distribución de agua en julio
pegazohn1978
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
epi editorial commitee meeting presentation
MIPLM
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Controller Request and Response in Odoo18
Celine George
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Ad

C# Control Statements, For loop, Do While.ppt

  • 2. Control statements Linear (sequential) program execution Selection structure and repetition structure Structured programming ◦ Controlled entry and exit out of a module. ◦ Avoid goto statements
  • 3. Selection structures in C# if  single selection statement if…else double selection statement switch multiple – selection statement
  • 4. Examples: if (grade >= 60) Console.writeline(“Passed!”); If (grade >=0) Console.writeline(“Passed!”); else Console.writeline(“Failed!”); Conditional Operator Console.writeline(grade >= 60 ?“Passed!” : “Failed!”);
  • 5. Nested if (grade >=90) Console.writeline(“A”); else if (grade >=80) Console.writeline(“B!”); … else Console.writeline(“F!”);
  • 6. Repetiton Structure - while While (condition){ statement 1; statement 2; statement 3; … }
  • 7. Example length = Convert.ToInt16(Console.ReadLine()); while (length > 0) { Console.Write("Enter Height of the Wall: "); height = Convert.ToInt16(Console.ReadLine()); PaintAWall thisWall = new PaintAWall(length, height, pricePerGal); thisWall.CalculateCost(ref paintCost,ref laborCost,ref galPaint, ref sqFt); Console.Write("Enter Length and Height for Wall # : “ + Convert.ToString(numWalls+1)); Console.Write("nEnter Length of the Wall (0 to quit): "); length = Convert.ToInt16(Console.ReadLine()); }
  • 8. Counter controlled vs Sentinel controlled A while loop, use LCV as a counter Counter =1 While (counter <=10) { … Counter ++ }//does it 10 times Sentinel controlled is good when one does not know exact number of times to execute a loop
  • 9. Explicitly and Implicitly converting between simple types Integer and integer division yields integer result. Suppose average is a floating point number: Average = total/num. Average will only get an integer if total and num are integers. int sum = 200, num = 3; float average; average = sum / num; Console.WriteLine(average); Will print 66
  • 10. Unary Cast Operator int sum = 200, num = 3; float av; av = (float) sum / num; Console.WriteLine(av); Will print 66.6666 Float/float or float/int or int/float will yield a float. C# implicitly promotes the one int to float.
  • 11. Nested control statements See example of a multiplication table generation class MultiplicationTable{ static void Main(string[] args){ int i=2, j, k; while (i <= 12){ for (j = 1; j <= 10; j++){ Console.WriteLine(i + " x " + j + " = " + i * j ); } Console.WriteLine("n"); i++; } } } }
  • 12. Switch Statements Switch (expression) { case value1: //Statements break; case value 2: //Statements break; case value 3: //Statements case value n: //Statements break; default: //Statements }