Java Toturial 3 - Control Flow

 
  1. Conditional Statements(IF)

  • Conditional statements execute a block or set of other statements only if certain conditions are met.
    if (name == "Fred")
    {x = 4;}
    else
    {x = 20;};
    When the conditional if statement is used only to make an assignment to one variable, you can use the terse C conditional operator.
    x = (name == "Fred") ? 4 : 20;
  1. Loops (FOR,WHILE)
  • For statements allow a set of statements to be repeated or looped through a fixed number of times.
  • The round bracket contains initializer(s) ; conditional test ; incrementer(s). If more than one initializer or incrementer is used they are separated with commas. The test condition is checked prior to executing the block. The incrementing is completed after executing the block.
  • for (int i=1; i<=15; i++) { document.writeln("#"+i); };
  • While statements allow a set of statements to be repeated or looped until a certain condition is met.
  • The test condition is checked prior to executing the block
  • int i = 0;while (i<=5) { document.writeln("#"+i); i = i + 1; };
  • Do While statements allow a set of statements to be repeated or looped until a certain condition is met. The test condition is checked after executing the block.
  • int i = 1;do { document.writeln("#"+i); i = i + 1; } while (i<=5);
  1. The Switch Statement(switch case)
  • Switch (or case) statements are used to select which statements are to be executed depending on a variable's value matching a label. default is used for the else situation.

switch (selection)
{
case '1': System.out.println("You typed a 1"); break;
case '2': System.out.println("You typed a 2"); break;
case '3': System.out.println("You typed a 3"); break;
case '4': System.out.println("You typed a 4"); break;
default : System.out.println("Oops!, that was an invalid entry.");
};

  1. Continue, Break and Return
  • Continue statements are used in looping statements to force another iteration of the loop before reaching the end of the current one.
  • Break statements are used in looping and 'switch' statements to force an abrupt termination or exit from the loop or switch.
  • Return statements are used to force a quick exit from a method . They are also used to pass values back from methods.
  1. Command Line Arguments
  • Command line arguments are accessible through the args array.
  • The array does not include the interpreter java command or the class name and the count starts at 0.
  • Each argument is passed as a string but may be converted to other types

 

 

i wanna run away never say goodbey
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值