C# NESTED IF Statement Video Tutorial and Source code

C# NESTED IF Statement Video Tutorial and Source code

This lesson is all about the decision control structures in C#, specifically the NESTED IF statement. The lesson includes sample programs to better understand how nested if statement works in C#.

A nested if statement is a computer programming construct that allows for the evaluation of a condition using one or more other conditions. The first condition is evaluated, and if it is true, the statement(s) associated with it are executed. If the first condition is not true, the next condition is evaluated, and so on. If no condition evaluates to true, the statement(s) associated with the nested if statement are executed. In other words, a nested if statement can be thought of as a set of if statements that are all connected together by else clauses.

Objectives

By the end of this lesson, students should be able to:

  1. demonstrate essential understanding on the nested if statement
  2. create a program that implements the concept of nested if statement
  3. solve problems using the concept of nested if statement in C#.
C# NESTED IF Statement Video Tutorial and Source code
C# NESTED IF Statement Video Tutorial and Source code

Requirements

  1. Visual Studio IDE

The Visual Studio IDE (Integrated Development Environment) is a software program that lets you make software, websites, and online apps. It includes a code editor, a debugger, a project system, and a number of tools to aid software development.

Introduction

A nested if statement is one in which one if statement is nested within another if statement. This allows you to test multiple conditions and take different actions depending on the results of the tests. Nested if statements are often used to create complex conditional logic.

How it works

In general, the source code presented below is an example of C# program that uses nested if statement. This is an example of console program that will ask the user to enter a name. The if statement will then compare the user input to John. If the user input is not equal to John then it will jump off to the else statement and print the text Invalid name. Next, if the user input is John then it will proceed and execute the body of the if statement that includes another if statement. The nested if statement will then ask to enter a passcode and compare it to 1234. If the passcode is 1234 then it will print access granted else access denied.

Here is the example source code of if else if statement in C#. We will explain source code line by line for you to better understand how if statement works and function.

C# NESTED IF Statement - source code
C# NESTED IF Statement – source code

Line 1 – We utilize the using keyword to add namespaces and class files. Class, interface, and abstract class information is displayed on the current page. Console is a System member function or method. The console function is required to display and read user input, hence it must be included first.

Line 2 – the name of the program is CSharpNestedIfStatement.

Line 6 – main method is the entry point of all C# programs.

Line 8 – variable declaration, userInput is the variable name of string type. It simply means that the variable can accept alpha numeric values.

Line 9 – another variable has been declared, passcode is the variable name of string type.

Line 10 – this line will display the text Enter name:.

Line 11 – Console.ReadLine takes the value entered and stores it in the userInput variable that we have declared in line 8.

Line 13 – this is the start of if statement. It is where the conditional statement is being evaluated.

userInput == “John”

Line 19-24  – this is the nested if statement. This block of code will be executed if the condition of line 13 returns TRUE. It means that the nested statements will run if the value entered by the user is equal to John.

Line 19 – the nested if statement has also a conditional statement to be tested.

passcode == “1234”

Line 21 – this line will be executed if the statement in line 19 returns TRUE. It means that if the user has entered 1234. The console will display access granted.

Line 24 – this line will be executed if the condition in line 19 returns FALSE. It means that the user has not entered the value of 1234. The console will display access denied.

Line 26 – this is where the program will jump if the condition in line 13 returns FALSE. It means that the user has not entered John.

Line 27 – this is the line that will be executed if line 13 is FALSE. The console will display a text Invalid name.

Line 28 – the purpose of this line is to prevent the console from closing unless the user will press a key in the keyboard.

using System;
namespace CSharpNestedIfStatement
{
class Program
{
static void Main(string[] args)
{
string userInput;
string passcode; 
Console.WriteLine("Enter name:");
userInput = Console.ReadLine();

if (userInput == "John")
{
//nested if statement
//will only be executed if the statement above is TRUE
Console.WriteLine("Enter passcode:");
passcode = Console.ReadLine();
if (passcode == "1234")
{
Console.WriteLine("access granted");
}
else
Console.WriteLine("access denied");
}
else
Console.WriteLine("Invalid name");
Console.ReadKey();
}
}
}

Video Tutorial

FREE DOWNLOAD SOURCE CODE

Summary

There are many potential uses for nested if statements in C#. Some common scenarios in which they may be useful include:

– Checking multiple conditions in a single if statement

– Checking for multiple specific values in a single if statement

– Checking a condition and then performing a different action depending on the result

– Nesting if statements to check multiple conditions and then performing different actions based on those conditions

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

For the step by step tutorial on how to create the if else if statement console application in C#, please watch the video uploaded on our YouTube Channel.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

C# IF ELSE IF Statement Video Tutorial and Source code

C# IF ELSE Statement Video Tutorial and Source code

C# IF Statement Video Tutorial and Source code

Delete a MySQL table Using C# Tutorial and Source code

Insert or Save Data into MySQL Database using C#

, , , , , , , , , ,

Post navigation