In this article we will learn about If-else control statement.
There are four types of if-else control statement.
if statement
Syntax
if(condition)
{
//code to be executed
}
Program
publicclassIf_Example{
publicstaticvoidmain(String args[]){
int age = 20;
if(age > 18)
{
System.out.println("You can give vote.");
}
}
}
Output
You can give vote.
Explanation
If statement executes the block if the condition becomes true.
if-else statement
Syntax
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Program
publicclassIfElse_Example{
publicstaticvoidmain(String args[]){
int age = 17;
if(age > 18)
{
System.out.println("You can give vote.");
}
else
{
System.out.println("You can't give vote.");
}
}
}
Output
You can't give vote.
Explanation
If condition becomes true then if block will be executed. otherwise else block will be executed.
nested if statement
Syntax
if (condition1)
{
// Executes when condition1 is trueif (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
}
Program
publicclassNested_If{
publicstaticvoidmain(String args[]){
int age = 20;
if(age > 18)
{
System.out.println("You can give vote.");
if(age % 2 == 0)
{
System.out.println("Your age is even.");
}
else
{
System.out.println("Your age is odd.");
}
}
}
}
Output
You can give vote.
Your age is even.
Explanation
If condition becomes true it executes statement of if block. if there is another if block exists it checks condition if condition becomes true then if block will be executed otherwise else block will be executed.
if-else-if ladder
Syntax
if(condition1){
//code to be executed if condition1 is true
}elseif(condition2){
//code to be executed if condition2 is true
}
elseif(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Program
publicclassIfElse_leader{
publicstaticvoidmain(String args[]){
int age = 22;
if(age < 12)
{
System.out.println("You are child.");
}
elseif(age > 12 && age < 20)
{
System.out.println("You are teenager.");
}
elseif(age > 20 && age < 50)
{
System.out.println("You are mature.");
}
else
{
System.out.println("You are old.");
}
}
}
Output
You are mature.
Explanation
Here, if condition becomes true then it's block will be executed and rest of the condition will be ignored. If all conditions become false then else block will be executed.
In this article we will see members of C# datatypes. Members of datatype Members of numerical datatype Experimenting with the intrinsic C# data types, understand that the numerical types of .NET support MaxValue and MinValue properties that provide information regarding the range a given type can store. In addition to the MinValue/MaxValue properties, a given numerical system type may define further useful members. For example, the System.Double type allows you to obtain the values for epsilon and infinity which might be of interest to those of you with a mathematical flare. Lets understand that by a simple program: Program using System; public class ConsoleApp { public static void Main(string[] args) { Console.WriteLine("Members of numerical datatype:"); Console.WriteLine("Max value of Int: {0}",int.MaxValue); Console.WriteLine("Min value of Int: {0}",int.MinValue); Console.WriteLine(...
Like any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values, and parameters. Unlike other programming languages, however, these keywords are much more than simple compiler- recognized tokens. Rather, the C# data type keywords are actually shorthand notations for full-blown types in the System namespace. Table lists each system data type, its range, the corresponding C# keyword, and the type’s compliance with the common language specification (CLS). C# keyword System type Range Meaning CLS complaint? bool System.Boolean true or false Represents truth or falsity Yes sbyte System.SByte -128 to 127 signed 8-bit number No byte System.Byte 0 to 255 Unsigned 8-bit ...
With the first introduction post of C# you can understand the return value of the Main() method, let’s examine the incoming array of string data. Assume that you now want to update your application to process any possible command-line parameters. One way to do so is using a C# for loop. We will see for loop in C# in later article. Accessing command line arguments with for loop Program using System; public class CLA { public static void Main(string[] args) { //this loop prints any number of given command line arguments for(int i=0; i<args.Length; i++) { Console.WriteLine("arg-" + i + " = " + args[i]); } } } Output F:\Code\Algorithm>csc CLA.cs F:\Code\Algorithm>CLA tangled java elaborately arg-0 = tangled arg-1 = java arg-2 = elaborately Explanation Here, you are checking to see whether the array of strings contains some number of items using the Length property of Sys...
Comments
Post a Comment