0% found this document useful (1 vote)
392 views

Code Snippets (Conditional Statements)

This document discusses different types of conditional statements in C#, including if, if/else, if/else if, and switch statements. It provides the syntax for each statement type and examples of how to use them. The if statement allows code to execute based on a single condition being true. The if/else statement allows two paths - one for true and one for false. The if/else if statement checks multiple conditions in order. The switch statement compares a variable to multiple "case" values and executes the corresponding block.

Uploaded by

Arjay Orcasitas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
392 views

Code Snippets (Conditional Statements)

This document discusses different types of conditional statements in C#, including if, if/else, if/else if, and switch statements. It provides the syntax for each statement type and examples of how to use them. The if statement allows code to execute based on a single condition being true. The if/else statement allows two paths - one for true and one for false. The if/else if statement checks multiple conditions in order. The switch statement compares a variable to multiple "case" values and executes the corresponding block.

Uploaded by

Arjay Orcasitas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Conditional Statements

Prepared by: Eisen Sy

Conditional Statement
Conditional statements allow your program to make a decision based on a set of conditions. Conditional statements in C# may be statement. implemented using the If statement. Conditional statements have various structures which we will study in the next slide.

Conditional Statement Structures


If statement If Else statement If Else If statement Switch statement

If Statement
The first type of if statement allows our program to perform a set of instructions if a certain true. condition is true. This type of if statement simply proceeds to the next instruction should the condition of the if false. statement turned out to be false. This is the most basic form of if statement. statement.

If Statement
Syntax: if ( Boolean Condition ) { True embedded-statements; embedded} Note: Using the curly brackets allow us to write more than 1 statement of instructions.

If Statement Tip
Syntax: if ( Boolean Condition ) { True embedded-statements; embedded}
If Statement must not end with a semicolon ( ; ) Embedded-Statement however, must end with a semicolon ( ; )

More If Statement Tip


Syntax: if ( Boolean Condition ) { True embedded-statements; embedded}
Indentions is not required but is considered a good programming habit to promote code readability.

If Statement Example
Code: string password; Console.Write (Input Password : ); password = Console.ReadLine (); if ( password == wind ) { Console.Write (Access Granted!); } Console.ReadKey(true); Output: Input Password : wind Access Granted!

If Statement Example
Code: string password; Console.Write (Input Password : ); password = Console.ReadLine (); if ( password == wind ) { Console.Write (Access Granted!); } Console.ReadKey(true); Output: Input Password : earth

If Statement for 1 Embedded Statement


Syntax: if ( Boolean Condition ) True embedded-statements; embedded-

Note: Without the curly brackets, only one statement may be executed if the condition is true.

If Statement Example
Code: string name; Console.Write (Please write your name : ); name = Console.ReadLine (); if ( name == Eisen ) Console.Write (Hello Boss.\n); Boss.\ Console.Write (This is the Demo Program.); Console.ReadKey(true); Output: Please write your name : Eisen Hello Boss. This is the Demo Program.

If Statement Example
Code: string name; Console.Write (Please write your name : ); name = Console.ReadLine (); if ( name == Eisen ) Console.Write (Hello Boss.\n); Boss.\ Console.Write (This is the Demo Program.); Console.ReadKey(true); Output: Please write your name : Mike This is the Demo Program.

If

Else Statement

This type of If statement allows the program to execute 2 instructions, one set of instructions if the condition is true and false. another if the condition is false.

If

Else Statement

Syntax: if ( Boolean Condition ) { True embedded-statements; embedded} else { False embedded-statements; embedded} Note: The rule of curly brackets applies in a similar way.

If

Else Statement Example

Code: Console.Write (Input Grade : ); int grade = int.Parse ( Console.ReadLine() ); if ( grade >= 70 ) { Console.Write (You Passed); } else { Console.Write (You Failed); } Console.ReadKey(true); Output: Input Grade : 80 You Passed

If

Else Statement Example

Code: Console.Write (Input Grade : ); int grade = int.Parse ( Console.ReadLine() ); if ( grade >= 70 ) { Console.Write (You Passed); } else { Console.Write (You Failed); } Console.ReadKey(true); Output: Input Grade : 50 You Failed

If

Else Statement (Alternative)

Syntax: if ( Boolean Condition ) True embedded-statements; embeddedelse False embedded-statements; embeddedNote: Without the curly braces, only 1 embedded statement may be inserted per section of the If statement. statement.

If

Else Statement Example

Code: Console.Write (Input Grade : ); int grade = int.Parse ( Console.ReadLine() ); if ( grade >= 70 ) Console.Write (You Passed); else Console.Write (You Failed); Console.ReadKey(true); Output: Input Grade : 80 You Passed

If

Else Statement Example

Code: Console.Write (Input Grade : ); int grade = int.Parse ( Console.ReadLine() ); if ( grade >= 70 ) Console.Write (You Passed); else Console.Write (You Failed); Console.ReadKey(true); Output: Input Grade : 50 You Failed

If

Else If Statement

This type of If statement allows us to specify multiple conditions.

If

Else If Statement

Syntax: if ( Boolean Condition A ) { EmbeddedEmbedded-Statements for Condition A; A; } else if ( Boolean Condition B ) { EmbeddedEmbedded-Statements for Condition B; B; } else if ( Boolean Condition C ) { EmbeddedEmbedded-Statements for Condition C; C; } else { EmbeddedEmbedded-Statements for Conditions not met; met; } Note: You can have variable number of conditions when using this type of If statement. statement.

Sample Code: Console.Write (Input Age : ); int age = int.Parse ( Console.ReadLine() ); if ( age >= 0 && age <= 20 ) { Console.Write (Youre still young.); } else if ( age >= 21 && age <= 50 ) { Console.Write (Youre an adult.); } else if ( age >= 51 && age <= 90 ) { Console.Write (Youre old.); } else { Console.Write (Youre still alive?); } Console.ReadKey (true);

Output Sample 1: Input Age : 37 Youre an adult. Output Sample 2: Input Age : 20 Youre still young. Output Sample 3: Input Age : 90 Youre old.

Switch Statement
Switch statement is an alternative to using If Else If statement. Switch statement evaluate the value of a certain cases. variable using cases. A break statement allows the program to exit the entire switch statement section. Thus, a break statement prevents a case of a switch statement from moving to the next case.

Switch Statement
Syntax: switch (variable) { case A: Embedded statements if break; case B: Embedded statements if break; case C: Embedded statements if break; default: Statements if variable break; }

variable is A;

variable is B;

variable is C;

is other than A, B or C;

Switch Statement Tips


Syntax: switch (variable) { case A: Embedded statements if break; case B: Embedded statements if break; case C: Embedded statements if break; default: Statements if variable break; }

No Semi-colon ( ; )
variable is A;

variable is B;

variable is C;

is other than A, B or C;

Switch Statement Tips


Syntax: switch (variable) { case A: Embedded statements if break; case B: Embedded statements if break; case C: Embedded statements if break; default: Statements if variable break; }

Enclosed in Curly Braces { }


variable is A;

variable is B;

variable is C;

is other than A, B or C;

Switch Statement Tips


Syntax: switch (variable) { case A: Embedded statements if break; case B: Embedded statements if break; case C: Embedded statements if break; default: Statements if variable break; }

Use Colons for cases ( : )


variable is A;

variable is B;

variable is C;

is other than A, B or C;

Switch Statement Tips


Syntax: switch (variable) { case A: Embedded statements if break; case B: Embedded statements if break; case C: Embedded statements if break; default: Statements if variable break; }

Use break for all cases


variable is A;

variable is B;

variable is C;

is other than A, B or C;

Switch Statement
When using string data types, case values are enclosed in double quotes ( ). ). Cases are not enclosed when using numeric data types.

Code: Console.Write (Input A, B or C : ); string choice = Console.ReadLine (); switch (choice) { case A: Console.Write (You selected A.); break; case B: Console.Write (You selected B.); break; case C: Console.Write (You selected C.); break; default: Console.Write (Invalid Input!); break; } Console.ReadKey(true);

Output Sample 1: Input A, B or C : A You selected A. Output Sample 2: Input A, B or C : B You selected B. Output Sample 3: Input A, B or C : C You selected C. Output Sample 4: Input A, B or C : D Invalid Input!

Code: Console.Write (Input 1, 2 or 3 : ); int choice = int.Parse ( Console.ReadLine() ); switch (choice) { case 1: Console.Write (One!); break; case 2: Console.Write (Two!!); break; case 3: Console.Write (Three!!!); break; default: Console.Write (Invalid Input!!!!); break; } Console.ReadKey(true);

Output Sample 1: Input 1, 2 or 3 : 1 One! Output Sample 2: Input 1, 2 or 3 : 2 Two!! Output Sample 3: Input 1, 2 or 3 : 3 Three!!! Output Sample 4: Input 1, 2 or 3 : 4 Invalid Input!!!!

DEMO Programs for the Lesson


1. Avoiding division by zero in a divide operation 2. Checking textboxes for empty in a summation
program 3. Validating numerical entries in a problem similar to item 1 4. Retrieving user response in a message box 5. Check if username is not empty and password has minimum of 7 characters

The End. Thank you for listening.

You might also like