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

La actualidad más candente (20)

Control statements in c
Control statements in cControl statements in c
Control statements in c
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping statement
Looping statementLooping statement
Looping statement
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Forloop
ForloopForloop
Forloop
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Destacado (6)

C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Looping in C
Looping in CLooping in C
Looping in C
 
conditional statements
conditional statementsconditional statements
conditional statements
 
Control statements
Control statementsControl statements
Control statements
 
Loops in C
Loops in CLoops in C
Loops in C
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 

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

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
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 WorkerThousandEyes
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
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
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

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