Sunteți pe pagina 1din 4

C# 30

Improved C++ 1

C# 3.0
Chapter 16 Improved C++

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 2

A Better C/C++
C# is based on the C/C++ ssyntax
nta
This what makes it so appealing to the C++ developer

C & C++ have many pitfalls


d tto many unnoticed
ti d errors
Thi
This may llead
while(x=0){
while(x
0){
DoSomething();
}

Do you see the problem?


This is a valid C/C++ that compiles and do nothing
Unless theres a side effect of assigning zero to x

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 3

Control Flow Statements

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 4

The if Statement
The C# if statement executes a block of
p
evaluates to true
code if an expression
The expression must be a bool expression

The following legal C++ code:


Randomrand=newRandom();
int i =rand.Next();
if(i %2)
C
Console.WriteLine("Therandomnumberisodd");
l W it Li ("Th
d
b i dd")
else
Console WriteLine("Therandomnumberiseven");
Console.WriteLine(
Therandomnumberiseven );

Causes in C# the following compiler error:


Cannotimplicitlyconverttype'int'to'bool'
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 5

The switch Statement


Like in C/C++/Ja
C/C++/Java
a
The switch statement lets you branch based on a
value of a variable
The switch statement can evaluate onlyy a p
predefined
type, or enum

Unlike C/C++/Java
The C# switch can evaluate a string
Another difference is that any case must end with
break (return or goto case)
To get the same fall through C/C++/Java functionality,
we can use the goto case statement
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 6

The Switch Statement Example


string[]names={"Bill","George","Hillary","Barack",
string[]names
{ Bill , George , Hillary , Barack ,
"Chelsea","John"};
stringlastName;
foreach (stringnameinnames) {
switch(name)
{
case"Hillary":
goto case
Bill ;
case"Bill";
case"George":
lastName
ast a e ="Bush";
us ;
break;
case"Barack":
lastName ="Obama";
break;

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 7

The Switch Statement Example


case"Bill":
case
Bill :
case"Chelsea":
lastName ="Clinton";
Clinton ;
break;
default:
;
lastName =Doe";
break;
}
Console.WriteLine("{0}{1}",name,lastName);
}

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Improved C++ 8

Summary
Creating a ne
new lang
language
age is a good
pp
y to fix some p
pitfalls
opportunity
C# has changed and improved many mechanisms
such as Arrays, & Enums, Strings, OOP keywords

In this chapter we have seen two


important fixes:
f
Boolean expressions
p
must be Boolean
In the if condition, while loop condition, do-while loop
condition and in the for statement condition

The Switch statement is safer and can get a string as


a value
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

S-ar putea să vă placă și