Anasayfa » C / C++

C / C++ Courses ? 8 (OR, AND and Ternary Conditional Operators, Error Types)

17 Aralık 2010 1.329 views Yorum Yok Kii

?OR? and ?AND? Conditional  Operators

In C, there is not any condition like that:  if(0<num<100). This is not a valid usage. We use ?&&? for ?and? and we use ?||? for ?or?.

&&

if(value<25 &&value>0)

statements;

this means if the value is between 0 and 25, then run these statements.

True && True= True

True && False= False

False && False = False

||

if(value<25 ||value>100)

statement;

This means if the value is less than 25 or larger than 100, then run these statements.

True || True= True

True || False= True

False || False = False

Ex:

Write a program that finds maximum and minimum value of 3 integers.

#include<stdio.h>

int main(){

int x,y,z;

printf(?Enter three numbers: ?);

scanf(?%d%d%d?,&x,&y,&z);

max=x;

min=x;

if(y>max)

max=y;

if(z>max)

max=z;

if(y<min)

min=y;

if(z<min)

min=z;

printf(?Maximum value of them is %d, minimum is %d.?,max,min);

return 0;

}

Control Structures

1-Sequential Structures

No if, no condition, no change in execution

2-Selection Structures

if, if/else, switch  (we didn?t learn switch until now;) )

3-Repetition Structures

while, for, do/while   (also didn?t learn all of these)

Ternary Conditional Operator (condition ? if_true: if_false )

These three options below are the same thing:

1-      printf( ?%s?, grade>=60 ? ?Passed? : ?Failed? );

2-      grade >= 60 ? printf( ?Passed?): printf( ?Failed?);

3-      if( grade>=60)

printf( ?Passed?);

else

printf( ?Failed?);

Error Types


Syntax Error: caught by compiler

Logic Error:

?Fatal Logic Error:

After compiling, when you execute, it gives this error. It terminates your program before it ends.

Non-fatal Logic Error:

It is in your output. Program runs without any error, but it has incorrect output.

Take care, by!

Yoruma Açığız! :)

Be nice. İçeriği çirkin olmadığı sürece her yoruma, öneriye ve soruya "evet" :)

Etiketler: , , , , , , , ,