• A.razaq

T10- If , if..else statement

Updated: Jan 26

قبل البداية , الشرح باللغة الانجليزية بشكل مبسط ومدعم بالامثلة والصور

في حال رغبتكم بالشرح باللغة العربية ارجوا اعلامي بذلك من خلال ترك ملاحظة

جميع الدروس هي من شرحي ومجهود شخصي لذا ارجوا اعلامي بالملاحظات الاقتراحات او الطلبات

----------------------------------------

in java we have Three control statements that can specify order of statements.

  1. Sequence structure

  2. Selection structure

  3. Repetition structure

Java provides three selection structures.

  1. if

  2. If…else

  3. switch

and also provide three repetition structures

  1. for loop

  2. while loop

  3. do..while loop

------------

lets begin by if statement and its variations by asking ourselves

what is if statement ?

if statement performs action only when a condition is true, meaning

its a decision-making statement it executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.


----- example in pseudocode -----


( If student’s grade is greater than or equal to 60 Print “Passed )

the code will be



if (studentGrade >= 60)
 System.out.println("Passed"); 


as you can see , if statment will first check for the condition

if the student grade is 60 or more

it will execute our code because the condition is met


now what if the condition is not met ?

well, if you make the student grade less than 60 for example and run the program , nothing will happen

now this is a case were we want to have another alternative code

meaning

if this condition is met , execute this code

if not

execute this code instead

this is were we use (if... else) statement


if... else let you perform action only when condition is true

or perform different specified action when condition is false


lets take an example


----- example in pseudocode -----


(If student’s grade is greater than or equal to 60 Print

“Passed” •

Else

Print “Failed)


code will be



if (grade >= 60)
			System.out.println("Passed");
			 else
			System.out.println("Failed"); 

now lets see both cases when the condition is met and when its not



as we can see here grade is 65 and the condition is 60 or more so it will print passed

now lets make the grade lower than 60



int grade = 50;
		
		if (grade >= 60)
			System.out.println("Passed");
			 else
			System.out.println("Failed");



now we see the condition is not met and the grade is not 60 or more so the

else statement will execute the other code


-------

i hope it was clear and for any questions leave them in the comment section

or contact me.

8 views0 comments

Recent Posts

See All