SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Loops and Conditional
Statements
SAAD SHAIKH
Conditional Statements
 In our daily life we do many things which depends on some kind of
conditions for e.g. If I study I will pass exams. If he is hungry he will eat etc.
 So in programming languages also we can perform different sets of
actions depending on the circumstances.
 There are three major decision making instructions:
 If statement.
 If else statement.
 Switch statement.
If statement
 If statement is the simplest conditional statement.
 If enables us to check condition is true or false.
 When the condition is true it will execute the statement and when false it
will not execute the statement.
 If syntax:
if(condition is true)
{
execute statements;
}
If Basic Working
Statement
Condition
true
False
If example
 Code:
main( )
{
int num ;
printf ( “Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( “Nice Choice !" ) ;
}
If else Statement
 If else is another useful statement.
 It executes statement 1 if the condition is true, and another if its false.
 The simplest form of if else statement:
if (expression)
{
statement 1;
}
else
{
statement 2;
}
If else basic working
Condition
1st Statement
2nd Statement
True
False
If else Example
 Checking a number if it is odd or even
string s = Console.ReadLine();
int number = int.Parse(s);
if (number % 2 == 0)
{
Console.WriteLine(“This number is even.”);
}
else
{
Console.WriteLine(“This number is odd.”);
}
Nested If Else
 We can write an entire if-else construct within either the body of the if
statement or the body of an else statement. This is called ‘nesting‘ of ifs.
 Syntax:
if (expression)
{
if (expression)
{
statement;
}
else
{
statement;
}
} else
statement;
Example
 Code:
if (first == second)
{ Console.WriteLine(“These two numbers are equal.”);
}
else
{
if (first > second)
{ Console.WriteLine(“The first number is bigger.”);
}
else
{
Console.WriteLine(“The second is bigger.”);
}
}
If .. Else if
 When you have multiple expressions to evaluate, you can use the if. Else if-
else form of the if statement.
Example
 Code:
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}
Switch
 Another form of selection statement is the switch statement, which executes a set of
logic depending on the value of a given parameter.
 It enables a program to select among several alternatives.
 It works like this: The value of an expression is successively tested against a list of
constants. When a match is found, the statement sequence associated with that
match is executed.
Switch
 Switch Syntax:
switch(expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}
Switch Example
static void Main()
{
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
Console.WriteLine("Well done");
break;
case 'F':
Console.WriteLine("Better try again");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
Console.WriteLine("Your grade is {0}", grade);
Console.ReadLine();
}
}
Nested Switch
 The syntax for a nested switch statement is as follows:
switch(ch1)
{
case 'A':
printf("This A is part of outer switch" );
switch(ch2)
{
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* inner B case code */
}
break;
case 'B': /* outer B case code */
}
Example
 Code:
static void Main(string[] args)
{
int a = 100;
int b = 200;
switch (a)
{
case 100:
Console.WriteLine("This is part of outer switch ");
switch (b)
{
case 200:
Console.WriteLine("This is part of inner switch ");
break;
}
break;
}
Console.WriteLine("Exact value of a is : {0}", a);
Console.WriteLine("Exact value of b is : {0}", b);
Console.ReadLine();
}
 Output:
This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200
What is loop?
 Loop is essential technique when writing a code – it is
basically the ability to repeat a block of code X times.
 A loop is a way to execute a piece of code
repeatedly.
 Go round and round until the condition is met.
While Loop
 A while loop will check a condition and then continues to execute a block of code as
long as the condition evaluates to a Boolean value of true or false.
 It’s syntax is as follows:
while (Condition)
{
statements ;
}
Condition
Basic Working of While Loop
Statement
true
false
While Loop Example
Code:
int counter = 0;
while (counter < 10)
{
Console.WriteLine("Number : {0}", counter);
counter++;
}
Output:
It will print Numbers from 0 to 9.
Nested While Loop
 The syntax for a nested while loop statement is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Nested While Loop Example
 Code:
int i = 0;
while (i < 2)
{
Console.WriteLine("Value of i: {0}", i);
int j = 1;
i++;
while (j < 2)
{
Console.WriteLine("Value of j: {0}", j);
j++;
}
}
 Output:
Value of i: 0
Value of j: 1
Value of i: 1
Value of j: 1
Do While Loop
 Another loop structure is Do While Loop
 A do loop is similar to the while loop, except that it checks its condition at the
end of the loop. This means that the do loop is guaranteed to execute at least
one time.
 The syntax of the do loop is
do
{
<statements>
}
while (<Boolean expression>);
Basic Working of Do While Loop
Statement
Condition
True
False
Do While Example
 Code:
class Program
{
static void Main(string[] args)
{
int table,i,res;
table=12;
i=1;
do
{
res = table * I;
Console.WriteLine("{0} x {1} = {2}", table, i, res);
i++;
}
while (i <= 10);
Console.ReadLine();
}
}
 Output:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 92
12 x 9 = 108
12 x 10 = 120
For Loop
 A for loop works like a while loop, except that the syntax of the for loop
includes initialization and condition modification.
 for loops are appropriate when you know exactly how many times you
want to perform the statements within the loop.
 The contents within the for loop parentheses hold three sections
separated by semicolons
for (<initializer list>; <Boolean expression>; <iterator list>)
{
<statements>
}
For Loop Structure
Initializer expression
for (int number = 0; ...; ...)
{ // Can use number here }
 Executed once, just
before the loop is entered.
 Usually used to declare a
counter variable.
Boolean Expression
for(int number = 0;number < 10;...)
{ // Can use number here }
 Evaluated before each
iteration of the loop
 If true, the loop body is
executed
 If false, the loop body is
skipped
 Also called test counter.
Iterator
for (int number = 0; number < 10;
number++)
{ // Can use number here }
 Executed at each iteration
after the body of the loop is
finished.
 Usually used to update the
counter
For Loop Example
 Code:
For( int i= 0; i<8 ; i++)
{
Console.WritleLine(i);
}
 Output:
0
1
2
3
4
5
6
7
Nested For Loop
 The syntax for a nested for loop statement in C# is as follows:
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
statement(s);
}
statement(s);
}
Example
 Code:
int n = int.Parse(Console.ReadLine());
for(int row = 1; row <= n; row++)
{
for(int column = 1; column <= row;
column++)
{
Console.Write("{0} ", column);
}
Console.WriteLine();
}
 Output:
1
1 2
....
1 2 3 ... n
Thank you!

Más contenido relacionado

La actualidad más candente

If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentationManeesha Caldera
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in cMuthuganesh S
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variablessangrampatil81
 
structure and union
structure and unionstructure and union
structure and unionstudent
 

La actualidad más candente (20)

If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
10. switch case
10. switch case10. switch case
10. switch case
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Looping in c language
Looping in c languageLooping in c language
Looping in c language
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
structure and union
structure and unionstructure and union
structure and union
 

Similar a Loops and conditional statements

Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional StatementsIntro C# Book
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 

Similar a Loops and conditional statements (20)

Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Loops
LoopsLoops
Loops
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 

Último

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Último (20)

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Loops and conditional statements

  • 2. Conditional Statements  In our daily life we do many things which depends on some kind of conditions for e.g. If I study I will pass exams. If he is hungry he will eat etc.  So in programming languages also we can perform different sets of actions depending on the circumstances.  There are three major decision making instructions:  If statement.  If else statement.  Switch statement.
  • 3. If statement  If statement is the simplest conditional statement.  If enables us to check condition is true or false.  When the condition is true it will execute the statement and when false it will not execute the statement.  If syntax: if(condition is true) { execute statements; }
  • 5. If example  Code: main( ) { int num ; printf ( “Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( “Nice Choice !" ) ; }
  • 6. If else Statement  If else is another useful statement.  It executes statement 1 if the condition is true, and another if its false.  The simplest form of if else statement: if (expression) { statement 1; } else { statement 2; }
  • 7. If else basic working Condition 1st Statement 2nd Statement True False
  • 8. If else Example  Checking a number if it is odd or even string s = Console.ReadLine(); int number = int.Parse(s); if (number % 2 == 0) { Console.WriteLine(“This number is even.”); } else { Console.WriteLine(“This number is odd.”); }
  • 9. Nested If Else  We can write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting‘ of ifs.  Syntax: if (expression) { if (expression) { statement; } else { statement; } } else statement;
  • 10. Example  Code: if (first == second) { Console.WriteLine(“These two numbers are equal.”); } else { if (first > second) { Console.WriteLine(“The first number is bigger.”); } else { Console.WriteLine(“The second is bigger.”); } }
  • 11. If .. Else if  When you have multiple expressions to evaluate, you can use the if. Else if- else form of the if statement.
  • 12. Example  Code: main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First division" ) ; else if ( per >= 50 ) printf ( "Second division" ) ; else if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "fail" ) ; }
  • 13. Switch  Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter.  It enables a program to select among several alternatives.  It works like this: The value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed.
  • 14. Switch  Switch Syntax: switch(expression) { case constant1: statement sequence break; case constant2: statement sequence break; case constant3: statement sequence break; . . . default: statement sequence break; }
  • 15. Switch Example static void Main() { char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent!"); break; case 'B': Console.WriteLine("Well done"); break; case 'F': Console.WriteLine("Better try again"); break; default: Console.WriteLine("Invalid grade"); break; } Console.WriteLine("Your grade is {0}", grade); Console.ReadLine(); } }
  • 16. Nested Switch  The syntax for a nested switch statement is as follows: switch(ch1) { case 'A': printf("This A is part of outer switch" ); switch(ch2) { case 'A': printf("This A is part of inner switch" ); break; case 'B': /* inner B case code */ } break; case 'B': /* outer B case code */ }
  • 17. Example  Code: static void Main(string[] args) { int a = 100; int b = 200; switch (a) { case 100: Console.WriteLine("This is part of outer switch "); switch (b) { case 200: Console.WriteLine("This is part of inner switch "); break; } break; } Console.WriteLine("Exact value of a is : {0}", a); Console.WriteLine("Exact value of b is : {0}", b); Console.ReadLine(); }  Output: This is part of outer switch This is part of inner switch Exact value of a is : 100 Exact value of b is : 200
  • 18. What is loop?  Loop is essential technique when writing a code – it is basically the ability to repeat a block of code X times.  A loop is a way to execute a piece of code repeatedly.  Go round and round until the condition is met.
  • 19. While Loop  A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a Boolean value of true or false.  It’s syntax is as follows: while (Condition) { statements ; }
  • 20. Condition Basic Working of While Loop Statement true false
  • 21. While Loop Example Code: int counter = 0; while (counter < 10) { Console.WriteLine("Number : {0}", counter); counter++; } Output: It will print Numbers from 0 to 9.
  • 22. Nested While Loop  The syntax for a nested while loop statement is as follows: while(condition) { while(condition) { statement(s); } statement(s); }
  • 23. Nested While Loop Example  Code: int i = 0; while (i < 2) { Console.WriteLine("Value of i: {0}", i); int j = 1; i++; while (j < 2) { Console.WriteLine("Value of j: {0}", j); j++; } }  Output: Value of i: 0 Value of j: 1 Value of i: 1 Value of j: 1
  • 24. Do While Loop  Another loop structure is Do While Loop  A do loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time.  The syntax of the do loop is do { <statements> } while (<Boolean expression>);
  • 25. Basic Working of Do While Loop Statement Condition True False
  • 26. Do While Example  Code: class Program { static void Main(string[] args) { int table,i,res; table=12; i=1; do { res = table * I; Console.WriteLine("{0} x {1} = {2}", table, i, res); i++; } while (i <= 10); Console.ReadLine(); } }  Output: 12 x 1 = 12 12 x 2 = 24 12 x 3 = 36 12 x 4 = 48 12 x 5 = 60 12 x 6 = 72 12 x 7 = 84 12 x 8 = 92 12 x 9 = 108 12 x 10 = 120
  • 27. For Loop  A for loop works like a while loop, except that the syntax of the for loop includes initialization and condition modification.  for loops are appropriate when you know exactly how many times you want to perform the statements within the loop.  The contents within the for loop parentheses hold three sections separated by semicolons for (<initializer list>; <Boolean expression>; <iterator list>) { <statements> }
  • 28. For Loop Structure Initializer expression for (int number = 0; ...; ...) { // Can use number here }  Executed once, just before the loop is entered.  Usually used to declare a counter variable. Boolean Expression for(int number = 0;number < 10;...) { // Can use number here }  Evaluated before each iteration of the loop  If true, the loop body is executed  If false, the loop body is skipped  Also called test counter. Iterator for (int number = 0; number < 10; number++) { // Can use number here }  Executed at each iteration after the body of the loop is finished.  Usually used to update the counter
  • 29. For Loop Example  Code: For( int i= 0; i<8 ; i++) { Console.WritleLine(i); }  Output: 0 1 2 3 4 5 6 7
  • 30. Nested For Loop  The syntax for a nested for loop statement in C# is as follows: for ( initialization; condition; increment ) { for ( initialization; condition; increment ) { statement(s); } statement(s); }
  • 31. Example  Code: int n = int.Parse(Console.ReadLine()); for(int row = 1; row <= n; row++) { for(int column = 1; column <= row; column++) { Console.Write("{0} ", column); } Console.WriteLine(); }  Output: 1 1 2 .... 1 2 3 ... n