SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Program Control Statements
Dr. Neeraj Kumar Pandey
Introduction
There are three types of program control statements:-
1. Selection Statements
 if
 switch
2. Iteration Statements
 for
 while
 do-while
 foreach
3.Jump Statements
 break
 goto
 continue
 return
 throw
Switch Statement
 The switch statement tests the value of a given variable (or expression) against a list of case values
and when match is found, a block of statements associated with that case is executed.
 The general form of switch statement is :-
switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-1
break
case value-n:
block-1
break
default:
default –block
break;
}
Switch Statement(Contd.)
using System;
class bulb
{
public static void Main()
{
Console.WriteLine("Select your choice");
Console.WriteLine("London");
Console.WriteLine("Bombay");
Console.WriteLine("Paris");
Console.WriteLine("Type your choice");
String name=Console.ReadLine();
switch(name)
{
case " Bombay":
Console.WriteLine("Bombay:Guide5");
break;
case " London":
Console.WriteLine("London:Guide10");
break;
case " Paris":
Console.WriteLine("Paris:Guide15");
break;
default:
Console.WriteLine("Invalid Choice");
break;
Fallthrough in Switch Statement
 In the absence of break statement in a case block, if the control moves
to the next case block without any problem, it is known as
‘FALLTHROUGH’.
 ‘FALLTHROUGH’ is permitted in C,C++ and JAVA. But C# doesn't
permit automatic fallthrough, if the case block contains executable code.
switch(m)
{
case 1:
x=y;
case 2:
x=y+m;
default :
x=y-m;
}
is an error in C#.
Fallthrough in Switch
Statement(Contd..)
 If we want two consecutive case blocks to be executed continuously,
we have to force the process by using the goto statement.
switch(m)
{
case 1:
x=y;
goto case 2;
case 2:
x=y+m;
goto default;
default:
x=y-m;
break;
}
Fallthrough in Switch
Statement(Contd..)
 The goto mechanisms enable us to jump backward and forward between
cases and therefore arrange labels arbitrarily:
Switch(m)
{
default:
x=y-m;
break;
case 2:
x=y+m;
goto default;
case 1:
x=y;
goto case 2;
}
Foreach statement
 The foreach statement is similar to the for statement but implemented
differently.
foreach(type variable in expression)
{
Body of the Loop
}
public static void Main()
{
foreach(string s in args)
{
Console.WriteLine(s);
}
}
We cannot change the iteration variable during execution.

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
rahulsahay19
 

Was ist angesagt? (20)

Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
C#.NET
C#.NETC#.NET
C#.NET
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
C# basics
 C# basics C# basics
C# basics
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Exception handling
Exception handlingException handling
Exception handling
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Characteristics of c#
Characteristics of c#Characteristics of c#
Characteristics of c#
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 
C# programming language
C# programming languageC# programming language
C# programming language
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
C# loops
C# loopsC# loops
C# loops
 

Ähnlich wie Program control statements in c#

C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptx
BansalShrivastava
 

Ähnlich wie Program control statements in c# (20)

Flow Control (C#)
Flow Control (C#)Flow Control (C#)
Flow Control (C#)
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptx
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptx
 

Mehr von Dr.Neeraj Kumar Pandey

Mehr von Dr.Neeraj Kumar Pandey (17)

Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Operators and expression in c#
Operators and expression in c#Operators and expression in c#
Operators and expression in c#
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Enumeration in c#
Enumeration in c#Enumeration in c#
Enumeration in c#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Cloud introduction
Cloud introductionCloud introduction
Cloud introduction
 
Role of cloud computing in scm
Role of cloud computing in scmRole of cloud computing in scm
Role of cloud computing in scm
 
Public cloud
Public cloudPublic cloud
Public cloud
 
cloud computing Multi cloud
cloud computing Multi cloudcloud computing Multi cloud
cloud computing Multi cloud
 
Ibm bluemix case study
Ibm bluemix case studyIbm bluemix case study
Ibm bluemix case study
 
Business cases for the need of cloud computing
Business cases for the need of cloud computingBusiness cases for the need of cloud computing
Business cases for the need of cloud computing
 
cloud computing:Types of virtualization
cloud computing:Types of virtualizationcloud computing:Types of virtualization
cloud computing:Types of virtualization
 
cloud computing: Vm migration
cloud computing: Vm migrationcloud computing: Vm migration
cloud computing: Vm migration
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Program control statements in c#

  • 1. Program Control Statements Dr. Neeraj Kumar Pandey
  • 2. Introduction There are three types of program control statements:- 1. Selection Statements  if  switch 2. Iteration Statements  for  while  do-while  foreach 3.Jump Statements  break  goto  continue  return  throw
  • 3. Switch Statement  The switch statement tests the value of a given variable (or expression) against a list of case values and when match is found, a block of statements associated with that case is executed.  The general form of switch statement is :- switch(expression) { case value-1: block-1 break; case value-2: block-1 break case value-n: block-1 break default: default –block break; }
  • 4. Switch Statement(Contd.) using System; class bulb { public static void Main() { Console.WriteLine("Select your choice"); Console.WriteLine("London"); Console.WriteLine("Bombay"); Console.WriteLine("Paris"); Console.WriteLine("Type your choice"); String name=Console.ReadLine(); switch(name) { case " Bombay": Console.WriteLine("Bombay:Guide5"); break; case " London": Console.WriteLine("London:Guide10"); break; case " Paris": Console.WriteLine("Paris:Guide15"); break; default: Console.WriteLine("Invalid Choice"); break;
  • 5. Fallthrough in Switch Statement  In the absence of break statement in a case block, if the control moves to the next case block without any problem, it is known as ‘FALLTHROUGH’.  ‘FALLTHROUGH’ is permitted in C,C++ and JAVA. But C# doesn't permit automatic fallthrough, if the case block contains executable code. switch(m) { case 1: x=y; case 2: x=y+m; default : x=y-m; } is an error in C#.
  • 6. Fallthrough in Switch Statement(Contd..)  If we want two consecutive case blocks to be executed continuously, we have to force the process by using the goto statement. switch(m) { case 1: x=y; goto case 2; case 2: x=y+m; goto default; default: x=y-m; break; }
  • 7. Fallthrough in Switch Statement(Contd..)  The goto mechanisms enable us to jump backward and forward between cases and therefore arrange labels arbitrarily: Switch(m) { default: x=y-m; break; case 2: x=y+m; goto default; case 1: x=y; goto case 2; }
  • 8. Foreach statement  The foreach statement is similar to the for statement but implemented differently. foreach(type variable in expression) { Body of the Loop } public static void Main() { foreach(string s in args) { Console.WriteLine(s); } } We cannot change the iteration variable during execution.